hdu 1242 Rescue
#include<stdio.h> #include<string.h> #include<queue> using namespace std; struct node { int x,y; int time; friend bool operator<(node a,node b) { if(a.time>b.time)return true;//越小越优先 return false; } }in,im; int f[4][2]= {{-1,0},{1,0},{0,-1},{0,1}}; int n,m; int startx,starty,ans; char a[210][210]; int visit[210][210]; priority_queue<node>pe; int bfs() { memset(visit,0,sizeof(visit)); while(!pe.empty())pe.pop(); in.x=startx; in.y=starty; //printf("%d %d\n",in.x,in.y); in.time=0; visit[in.x][in.y]=1; pe.push(in); while(!pe.empty()) { im=pe.top(); pe.pop(); for(int i=0; i<4; i++) { in.x=im.x+f[i][0]; in.y=im.y+f[i][1]; if(in.x>=0&&in.x<n&&in.y>=0&&in.y<m&&visit[in.x][in.y]==0&&a[in.x][in.y]!='#')//未加a[in.x][in.y]!='#'判断时就Wrong了 { visit[in.x][in.y]=1; if(a[in.x][in.y]=='r')return im.time+1; else if(a[in.x][in.y]=='.') { //printf("%d %d\n",in.x,in.y); in.time=im.time+1; pe.push(in); } else if(a[in.x][in.y]=='x') { //printf("%d %d\n",in.x,in.y); in.time=im.time+2; pe.push(in); } } } } return -1; } int main() { while(scanf("%d %d",&n,&m)!=EOF) { getchar(); for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { scanf("%c",&a[i][j]); if(a[i][j]=='a') { startx=i; starty=j; } } getchar(); } ans=bfs(); if(ans!=-1)printf("%d\n",ans); else printf("Poor ANGEL has to stay in the prison all his life.\n"); } return 0; }