标签:des style blog http io ar color os sp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16886 Accepted Submission(s): 6120
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstdlib> 5 #include<cstring> 6 #include<queue> 7 using namespace std; 8 9 int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; 10 char map[205][205]; 11 int visit[205][205]; 12 int a,b; 13 14 struct node{ 15 int x; 16 int y; 17 int time; 18 friend bool operator < (const node &a,const node &b) 19 { 20 return a.time>b.time; 21 } 22 }; 23 24 int border(int x,int y) 25 { 26 if( (x>=1&&x<=a) && (y>=1&&y<=b) && (map[x][y]!=‘#‘) ) 27 return 1; 28 return 0; 29 } 30 int bfs(int x,int y) 31 { 32 int i; 33 node abc,q; 34 priority_queue<struct node>p; 35 memset(visit,0,sizeof(visit)); 36 abc.x=x; 37 abc.y=y; 38 abc.time=0; 39 visit[abc.x][abc.y]=1; 40 p.push(abc); 41 while(!p.empty()) 42 { 43 abc=p.top(); 44 p.pop(); 45 if(map[abc.x][abc.y]==‘r‘) 46 { 47 return abc.time; 48 } 49 for(i=0;i<4;i++) 50 { 51 q.x=abc.x+dir[i][0]; 52 q.y=abc.y+dir[i][1]; 53 if( border(q.x,q.y) && !visit[q.x][q.y]) 54 { 55 visit[q.x][q.y]=1; 56 if(map[q.x][q.y]==‘x‘) 57 q.time=abc.time+2; 58 else 59 q.time=abc.time+1; 60 p.push(q); 61 } 62 } 63 } 64 return -1; 65 } 66 67 int main() 68 { 69 //freopen("in.txt","r",stdin); 70 int x,y,ans; 71 int i,j; 72 while(~scanf("%d%d",&a,&b)) 73 { 74 for(i=1;i<=a;i++) 75 { 76 getchar(); 77 for(j=1;j<=b;j++) 78 { 79 scanf("%c",&map[i][j]); 80 if(map[i][j]==‘a‘) 81 { 82 x=i; 83 y=j; 84 } 85 } 86 } 87 ans=bfs(x,y); 88 if(ans==-1) 89 printf("Poor ANGEL has to stay in the prison all his life.\n"); 90 else 91 printf("%d\n",ans); 92 93 } 94 95 return 0; 96 }
标签:des style blog http io ar color os sp
原文地址:http://www.cnblogs.com/xuesen1995/p/4107272.html