zoj1649 BFS (二)

2014-11-24 02:21:12 · 作者: · 浏览: 4
t;
char map[205][205];
int mt[205][205];//记录最少时间
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int n,m;

bool path(int x,int y)
{
if(x>=0&&x=0&&y return true;
return false;
}

int BFS(int sx,int sy)
{
priority_queueq;
cur.x=sx;
cur.y=sy;
cur.time=0;
mt[sx][sy]=0;
q.push(cur);
while(!q.empty())
{
cur=q.top();
q.pop();
if(map[cur.x][cur.y]=='a')
return cur.time;
for(int i=0;i<4;i++)
{
next.x=cur.x+dir[i][0];
next.y=cur.y+dir[i][1];
next.time=cur.time+1;
if(map[next.x][next.y]=='x')
next.time++;
if(path(next.x,next.y)&&next.time {
mt[next.x][next.y]=next.time;
q.push(next);
}
}
}
return -1;
}

int main()
{
int i,j,sx,sy;
while(~scanf("%d%d",&n,&m))
{
for(i=0;i scanf("%s",map[i]);
for(i=0;i for(j=0;j {
if(map[i][j]=='r')
{
sx=i;
sy=j;
}
mt[i][j]=100000000;
}
int res=BFS(sx,sy);
if(res==-1)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",res);
}
return 0;
}