思路还是BFS,有些人用双向队列了,
然而弱不会用,
弱只能用一个BFS函数暴力两个搜索;
然后比较;
Description
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.
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
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
AC代码如下:
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int n,m,count1[210][210],count2[210][210],visited[210][210];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
char str[210][210];
struct node{
int x;
int y;
};
bool check(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<m&&str[x][y]!='#'&&visited[x][y]==0)
return true;
else
return false;
}
void bfs(int flag,int x,int y) //*这里是遍历到达所有点的时间,然后main函数将到达KFC的时间相加比较
{
int f=0,r=0,t1,t2;
node p,temp;
queue<node>q;
p.x=x;
p.y=y;
q.push(p);
if(flag==1)
{
while(!q.empty())
{
p=q.front();
q.pop();
for(int i=0;i<4;i++)
{
t1=p.x+dir[i][0];
t2=p.y+dir[i][1];
if(check(t1,t2))
{
visited[t1][t2]=-1;
temp.x=t1;
temp.y=t2;
q.push(temp);
count1[t1][t2]=count1[p.x][p.y]+11;
}
}
}
}
else
{
while(!q.empty())
{
p=q.front();
q.pop();
for(int i=0;i<4;i++)
{
t1=p.x+dir[i][0];
t2=p.y+dir[i][1];
if(check(t1,t2))
{
visited[t1][t2]=-1;
temp.x=t1;
temp.y=t2;
q.push(temp);
count2[t1][t2]=count2[p.x][p.y]+11;
}
}
}
}
}
int main()
{
int i,j,x1,y1,x2,y2,a[40005][2],num,flag,min;
while(scanf("%d%d",&n,&m)!=EOF)
{
getchar();
memset(a,0,sizeof(a));
memset(count1,0,sizeof(count1));
memset(count2,0,sizeof(count2));
num=0;
min=100000000;
for(i=0;i<n;i++)
scanf("%s",str[i]);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(str[i][j]=='Y')
{
x1=i;
y1=j;
}
else if(str[i][j]=='M')
{
x2=i;
y2=j;
}
else if(str[i][j]=='@')
{
a[num][0]=i;
a[num][1]=j;
num++;
}
}
}
memset(visited,0,sizeof(visited));
flag=1;
bfs(flag,x1,y1);
memset(visited,0,sizeof(visited));
flag=2;
bfs(flag,x2,y2);
for(i=0;i<num;i++)
{
if(count1[a[i][0]][a[i][1]]!=0&&count2[a[i][0]][a[i][1]]!=0)//*必须是Y和M都能到达的
{
if(min>(count1[a[i][0]][a[i][1]]+count2[a[i][0]][a[i][1]]))
min=count1[a[i][0]][a[i][1]]+count2[a[i][0]][a[i][1]];
}
}
printf("%d\n",min);
}
return 0;
}