Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest.
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
Input
The input contains multiple test cases.
Each test case include, first two integers n, m. (2<=n,m<=200).
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’ express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF
Output
For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.
Sample Input
4 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 Y..@. .#... .#... @..M. #...#
Sample Output
66 88 66
一开始一道水题被我莫名的wa后来发现bug有可能你找到的@永远找不到于是输出0正确是另一个的最小值
这是我对拍程序
wa的可以用来对拍
ac代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
int n,m;
char a[1009][1009];int flag =0;
int book1[1009][1009],book[1009][1009],book2[1009][1009];
struct node{
int x,y,step;
node(){
}
node(int xx,int yy,int sstep):x(xx),y(yy),step(sstep){
}
};
const int rr[4][2] = {1,0,-1,0,0,-1,0,1};
void bfs(int x,int y,int step)
{
queue<node>A;
A.push(node(x,y,step));
while(!A.empty())
{
node u = A.front();
A.pop();
for(int i = 0;i < 4;++i)
{
int xx = u.x + rr[i][0];
int yy = u.y + rr[i][1];
int ss = u.step + 1;
if(xx < 1 || yy < 1 || xx > n || yy > m || book[xx][yy] == 1 || a[xx][yy] == '#')
{
continue;
}
if(a[xx][yy] == '@' )
{
if(flag == 0)
{
book1[xx][yy] = ss;
}
else{
book2[xx][yy] = ss;
}
}
book[xx][yy] = 1;
A.push(node(xx,yy,ss));
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
string b;
memset(a,0,sizeof(a));
memset(book1,0,sizeof(book1));
memset(book2,0,sizeof(book2));
int x1,y1,x2,y2;
for(int i = 1;i <= n;++i)
{
cin>>b;
for(int j = 1;j <= m;++j)
{
a[i][j] = b[j-1];
if(a[i][j] == 'Y')
{
x1 = i;
y1 = j;
}
if(a[i][j] == 'M')
{
x2 = i;
y2 = j;
}
}
}flag =0;
memset(book,0,sizeof(book));
book[x1][y1] = 1;
bfs(x1,y1,0);
memset(book,0,sizeof(book));
book[x2][y2] = 1;flag =1;
bfs(x2,y2,0);
int min1 = 0x3f3f3f3f;
for(int i = 1;i <= n;++i)
{
for(int j = 1;j <= m;++j)
{
if(a[i][j] == '@' && book1[i][j] != 0 && book2[i][j] != 0)
{
min1 = min(min1,book1[i][j]+book2[i][j]);
}
}
}
printf("%d\n",min1*11);
}
return 0;
}
/*
5 3
Y#@
#.#
..@
...
.#M
*/
对拍程序:
#include<iostream>
#include<windows.h>
#include<ctime>
using namespace std;
int main()
{
int t=100,falg=0;
while(t--)
{
system("数据生成.exe > 数据生成.txt"); //生成数据
system("ac.exe < 数据生成.txt > ac.txt"); //获得正确代码答案
system("wa.exe < 数据生成.txt > wa.txt"); //获得未ac代码答案
if(system("fc wa.txt ac.txt"))//不对跳出
{
falg=1;
break;
}
}
if(falg)
cout<<"error"<<endl;//输出错误
else
cout<<"no error"<<endl; //没错误输出
return 0;
}
数据生成:
#include<cstdio>
#include<ctime>
#include<cstdlib>
int main()
{
srand((unsigned)time(NULL));//按时间生成
int n,m;
n = rand()%10+1,m = rand()%10+1;
printf("%d %d\n",n,m);
for(int i = 1;i <= n;++i)
{
for(int j = 1;j <= m;++j)
{
if(i == 1 && j == 1)
{
printf("Y");
continue;
}
if(i == n && j == m)
{
printf("M");continue;
}
if(i == 1 && j == m)
{
printf("@");continue;
}
int y = rand()%7;
if(y == 1 || y == 3)
{
printf("#");
}
else if(y == 2)
{
printf("@");
}
else{
printf(".");
}
}
printf("\n");
}
}