做的第一个迷宫bfs。
http://acm.hdu.edu.cn/showproblem.php?pid=2612
<span style="font-size:18px;">#include <stdio.h>
#include <string.h>
#include <queue>
#define MAX 205
#define INF 0xffffff
using namespace std;
int n,m,l;
int yix,yiy,mx,my;
int Y[MAX][MAX],M[MAX][MAX]; //分别存yi,mk在到各个kfc的时间</span>
int mark[MAX][MAX]; //在bfs中判断点是否走过</span>
int dirx[5]={0,0,1,0,-1},diry[5]={0,1,0,-1,0};
char s[MAX][MAX]; //存图</span>
int fmin(int a,int b)
{
return a<b?a:b;
}
typedef struct ac
{
int x,y,t;
}node;
void bfs(int x,int y,int sign) //sign判断走的人是谁
{
queue <node> q;
node next,temp;
memset(mark,0,sizeof(mark));
next.x=x;
next.y=y;
next.t=0;
q.push(next);
mark[x][y]=1;
while(!q.empty()) //标准bfs循环
{
next=q.front();
q.pop();
if('@'==s[next.x][next.y]) //M[][],Y[][]记录到@时间。</span><span style="white-space:pre"> </span>
{
if(sign==1)
Y[next.x][next.y]=next.t;
else
M[next.x][next.y]=next.t;
}
for(int i=1;i<=4;i++)
{
temp.x=next.x+dirx[i];
temp.y=next.y+diry[i];
temp.t=next.t+1;
if(temp.x<1 || temp.y<1 || temp.x>n || temp.y>m ) //走出界外
continue ;
if('#'==s[temp.x][temp.y] || mark[temp.x][temp.y])//走过或为墙</span>
continue ;
mark[temp.x][temp.y]=1;
q.push(temp);
}
}
}
int main()
{
int i,j;
while(scanf("%d %d",&n,&m)!=EOF)
{
l=0;
for(i=1;i<=n;i++)//存图,同时存Y,M出发位置
{
scanf("%s",&s[i][1]);
for(j=1;j<=m;j++)
{
if('Y'==s[i][j])
yix=i,yiy=j;
else if('M'==s[i][j])
mx=i,my=j;
}
}
memset(M,0,sizeof(M));
memset(Y,0,sizeof(Y));
bfs(yix,yiy,1);
bfs(mx,my,2);
int sum=INF;
for(i=1;i<=n;i++)//遍历求最小值</span>
for(j=1;j<=m;j++)
if(M[i][j] && Y[i][j])
sum=fmin(sum,M[i][j]+Y[i][j]);
printf("%d\n",sum*11);
}
return 0;
}
</span>