Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29263 Accepted Submission(s): 10342
Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.
You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.
Process to the end of the file.
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<queue> 7 #include<map> 8 #include<set> 9 #include<vector> 10 #include<cstdlib> 11 #include<string> 12 #define eps 0.000000001 13 typedef long long ll; 14 typedef unsigned long long LL; 15 using namespace std; 16 const int N=1000+10; 17 int vis[N][N]; 18 int n,m; 19 char mp[N][N]; 20 int _x[4]={0,1,-1,0}; 21 int _y[4]={1,0,0,-1}; 22 struct node{ 23 int x,y; 24 int time; 25 friend bool operator < (node a,node b){ 26 return a.time>b.time; 27 } 28 }a,b; 29 priority_queue<node>q; 30 int judge(int x,int y){ 31 if(x<0||x>=n||y<0||y>=m)return 0; 32 if(vis[x][y]==1)return 0; 33 if(mp[x][y]=='#')return 0; 34 return 1; 35 } 36 int BFS(int x,int y){ 37 a.time=0; 38 a.x=x;a.y=y; 39 q.push(a); 40 vis[x][y]=1; 41 while(!q.empty()){ 42 b=q.top(); 43 q.pop(); 44 for(int i=0;i<4;i++){ 45 int xx=b.x+_x[i]; 46 int yy=b.y+_y[i]; 47 if(judge(xx,yy)){ 48 vis[xx][yy]=1; 49 if(mp[xx][yy]=='x')a.time=b.time+2; 50 else if(mp[xx][yy]=='r')return (b.time+1); 51 else 52 a.time=b.time+1; 53 a.x=xx; 54 a.y=yy; 55 q.push(a); 56 } 57 } 58 } 59 return -1; 60 } 61 int main(){ 62 while(scanf("%d%d",&n,&m)!=EOF){ 63 while(!q.empty())q.pop(); 64 memset(vis,0,sizeof(vis)); 65 int x,y; 66 for(int i=0;i<n;i++) 67 for(int j=0;j<m;j++){ 68 cin>>mp[i][j]; 69 if(mp[i][j]=='a'){ 70 x=i;y=j; 71 //a.x=x;a.y=y;a.time=0; 72 } 73 } 74 int ans=BFS(x,y); 75 if(ans==-1) 76 printf("Poor ANGEL has to stay in the prison all his life. " ); 77 else 78 printf("%d ",ans); 79 } 80 }