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
这个题思路一般人都能想到,就是先确定Y,M,@,的位置,如果一个一个的找@的话肯定会超时,所以就一次广搜把所有的@都找出来并记录步数,再比较一下步数的大小,确定答案。
不过我找bug找了两小时还没找出来,最后还是让学长帮忙找出来的。(真是的,不符合现实啊!)
第一个是错误的代码
#include<queue>//队列
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int a[40005];//纯粹用来排序的,并无它用
char map[205][205];//地图
int vis[205][205];//标记
int go[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int yi,yj,mi,mj,k;//两个人的起点和kfc的数量
int n,m;
struct node
{
int si,sj,ans;
};
struct node1
{
int ki,kj,finans;//咖啡店的位置和两个人走到相同kac所需要的步数和
}num[40000];
void bfs(int si,int sj)
{
node now,next;//压入队列的起点位置
queue<node>s;
now.si=si,now.sj=sj,now.ans=0;
s.push(now);
while(!s.empty())
{
now=s.front();
for(int q=0;q<k;q++)
{
if(now.si==num[q].ki&&now.sj==num[q].kj)//看看是否到达kfc的
{
num[q].finans=num[q].finans+now.ans;//记录两人共同的步数
break;
}
}
for(int u=0;u<4;u++)//四个方向广搜
{
int x=now.si+go[u][0],y=now.sj+go[u][1];
if(!vis[x][y]&&map[x][y]!='#'&&x>=0&&x<n&&y>=0&&y<m)
{
vis[x][y]=1;
next.si=x,next.sj=y,next.ans=now.ans+1;
s.push(next);
}
}
s.pop();
}
}
int main()
{
while(cin>>n>>m)
{
memset(a,0,sizeof(a));
memset(vis,0,sizeof(vis));
int i,j;
k=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cin>>map[i][j];
if(map[i][j]=='Y')//y的位置
{
yi=i;
yj=j;
}
if(map[i][j]=='M')//m的位置
{
mi=i;
mj=j;
}
if(map[i][j]=='@')//kfc的位置
{
num[k].ki=i;
num[k].kj=j;
num[k].finans=0;
k++;
}
}
}
bfs(yi,yj);
memset(vis,0,sizeof(vis));
bfs(mi,mj);
for(int p=0;p<k;p++)
{
a[p]=num[p].finans;//把步数全都存到a数组中
}
sort(a,a+k);//对a数组排序,得到最小步数(???)
cout<<11*a[0]<<endl;//(???)
}
return 0;
}
你提交一下会发现是错的,那么为什么呢?
3 5
..M##
@..#@
..Y##
试一下这组数据,你会发现,错的,那么问题就显而易见了,就是没考虑kfc被墙全都围住的情况(那有这样的kfc,气死我了,竟然让我找俩小时bug)。
下面这个是对的,就改了最后的输出
#include<queue>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int a[40005];
char map[205][205];
int vis[205][205];
int go[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int yi,yj,mi,mj,k;
int n,m;
struct node
{
int si,sj,ans;
};
struct node1
{
int ki,kj,finans;
}num[40000];
void bfs(int si,int sj)
{
node now,next;
queue<node>s;
now.si=si,now.sj=sj,now.ans=0;
s.push(now);
while(!s.empty())
{
now=s.front();
for(int q=0;q<k;q++)
{
if(now.si==num[q].ki&&now.sj==num[q].kj)
{
num[q].finans=num[q].finans+now.ans;
break;
}
}
for(int u=0;u<4;u++)
{
int x=now.si+go[u][0],y=now.sj+go[u][1];
if(!vis[x][y]&&map[x][y]!='#'&&x>=0&&x<n&&y>=0&&y<m)
{
vis[x][y]=1;
next.si=x,next.sj=y,next.ans=now.ans+1;
s.push(next);
}
}
s.pop();
}
}
int main()
{
while(cin>>n>>m)
{
memset(a,0,sizeof(a));
memset(vis,0,sizeof(vis));
int i,j;
k=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cin>>map[i][j];
if(map[i][j]=='Y')
{
yi=i;
yj=j;
}
if(map[i][j]=='M')
{
mi=i;
mj=j;
}
if(map[i][j]=='@')
{
num[k].ki=i;
num[k].kj=j;
num[k].finans=0;
k++;
}
}
}
bfs(yi,yj);
memset(vis,0,sizeof(vis));
bfs(mi,mj);
for(int p=0;p<k;p++)
{
a[p]=num[p].finans;
}
sort(a,a+k);
for(int l=0;l<k;l++)
{
if(a[l]!=0)//不为0事输出
{
cout<<11*a[l]<<endl;
break;
}
}
}
return 0;
}
ok了!!!