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.
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
4 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 Y..@. .#... .#... @..M. #...#
66 88 66
这个题最开始的想法就是对分别对各个kfc的位置进行bfs,意料之中的TLE了,然而就看了一个博客,对每个位置都标记步数,然后对各个@位置求总步数的最小值,这个最小值如果初始化为0时,要把步数为0 的结果值去掉,原因1步数不可能为0 原因2为0要不表示没有路要不表示到不了这个地方,所以要去除为0的情矿
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cmath>
#define maxx 210
using namespace std;
int n,m;
int book_y[maxx][maxx],book_m[maxx][maxx];
char ma[maxx][maxx];
struct note
{
int x,y,step;
}st,ed;
void bfs_y()
{
int i;
int next[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
queue<note>Q;
Q.push(st);
while(!Q.empty())
{
st=Q.front();
Q.pop();
for(i=0;i<4;i++)
{
ed.x=st.x+next[i][0];
ed.y=st.y+next[i][1];
if(book_y[ed.x][ed.y]==0&&(ma[ed.x][ed.y]=='.'||ma[ed.x][ed.y]=='@')&&ed.x>=0&&ed.y>=0&&ed.x<n&&ed.y<m)
{
ed.step=st.step+1;
Q.push(ed);
book_y[ed.x][ed.y]=ed.step;;
}
}
}
}
void bfs_m()
{
int i;
int next[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
queue<note>Q;
Q.push(st);
while(!Q.empty())
{
st=Q.front();
Q.pop();
for(i=0;i<4;i++)
{
ed.x=st.x+next[i][0];
ed.y=st.y+next[i][1];
if(book_m[ed.x][ed.y]==0&&ma[ed.x][ed.y]!='#'&&ed.x>=0&&ed.y>=0&&ed.x<n&&ed.y<m)
{
ed.step=st.step+1;
Q.push(ed);
book_m[ed.x][ed.y]=ed.step;;
}
}
}
}
int main()
{
char c;
int i,j,yy,yx,mx,my;
while(~scanf("%d %d",&n,&m))
{
memset(ma,0,sizeof(ma));
for(i=0;i<n;i++)
{
getchar();
for(j=0;j<m;j++)
{
scanf("%c",&c);
ma[i][j]=c;
if(c=='Y')
{
yx=i;yy=j;
}
else if(c=='M')
{
mx=i;my=j;
}
}
}
memset(book_y,0,sizeof(book_y));
memset(book_m,0,sizeof(book_m));
st.x=yx,st.y=yy,st.step=0;book_y[yx][yy]=1;
bfs_y();
st.x=mx,st.y=my,st.step=0;book_m[mx][my]=1;
bfs_m();
int ans=0x3f3f3f3f;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(ma[i][j]=='@'&&book_y[i][j]!=0&&book_m[i][j]!=0)
{
ans=min(ans,book_y[i][j]+book_m[i][j]);
}
}
}
printf("%d\n",ans*11);
}
return 0;
}
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cmath>
#define maxx 210
#define inf 0x3f3f3f3f
using namespace std;
int n,m;
int visit[maxx][maxx],book[maxx][maxx];
char ma[maxx][maxx];
struct note
{
int x,y,step;
}st,ed;
void bfs(int num)
{
int i;
int next[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
memset(visit,0,sizeof(visit));
visit[st.x][st.y]=1;
queue<note>Q;
Q.push(st);
while(!Q.empty())
{
st=Q.front();
Q.pop();
if(ma[st.x][st.y]=='@')
{
if(num==1)
{
book[st.x][st.y]=st.step;
}
else if(book[st.x][st.y]!=-1)
{
book[st.x][st.y]+=st.step;
}
}
for(i=0;i<4;i++)
{
ed.x=st.x+next[i][0];
ed.y=st.y+next[i][1];
if(visit[ed.x][ed.y]==0&&ma[ed.x][ed.y]!='#'&&ed.x>=0&&ed.y>=0&&ed.x<n&&ed.y<m)
{
ed.step=st.step+1;
Q.push(ed);
visit[ed.x][ed.y]=1;
}
}
}
}
int main()
{
char c;
int i,j,yy,yx,mx,my;
while(~scanf("%d %d",&n,&m))
{
memset(ma,0,sizeof(ma));
for(i=0;i<n;i++)
{
getchar();
for(j=0;j<m;j++)
{
scanf("%c",&c);
ma[i][j]=c;
if(c=='Y')
{
yx=i;yy=j;
}
else if(c=='M')
{
mx=i;my=j;
}
}
}
memset(book,-1,sizeof(book));
st.x=yx,st.y=yy,st.step=0;
bfs(1);
st.x=mx,st.y=my,st.step=0;
bfs(2);
int ans=inf;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(ma[i][j]=='@'&&book[i][j]!=-1&&visit[i][j]==1)//第一个标注Y一定路过,第二个表示第二个一定路过
{
ans=min(ans,book[i][j]);
}
}
}
printf("%d\n",ans*11);
}
return 0;
}