Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 32983 Accepted Submission(s):
11542
Problem Description
Angel was caught by the MOLIGPY! He was put in prison
by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There
are WALLs, ROADs, and GUARDs in the prison.
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.)
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.)
Input
First line contains two integers stand for N and
M.
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.
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.
Output
For each test case, your program should output a single
integer, standing for the minimal time needed. If such a number does no exist,
you should output a line containing "Poor ANGEL has to stay in the prison all
his life."
Sample Input
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
Sample Output
13
注意:可能有多个r
AC代码(BFS+QUEUE)
版本:a往多个r方向
1 #include<iostream> 2 #include<fstream> 3 #include<queue> 4 #include<string> 5 #include<cstring> 6 #include<vector> 7 #include<limits> 8 using namespace std; 9 const int MAX=203; 10 struct pot 11 { 12 int x,y; 13 }; 14 struct data 15 { 16 int time; char ch; 17 }; 18 data arr[MAX][MAX]; 19 int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}}; 20 int n,m; 21 int anglex,angley; 22 int BFS() 23 { 24 int mintime=INT_MAX,flag=0; 25 pot beg={anglex,angley}; 26 queue<pot> que; 27 que.push(beg); 28 while(!que.empty()) 29 { 30 pot cur=que.front(); 31 que.pop(); 32 if(arr[cur.x][cur.y].ch=='r') 33 { 34 if(arr[cur.x][cur.y].time<mintime) 35 { 36 flag=1; 37 mintime=arr[cur.x][cur.y].time; 38 } 39 //cout<<"sdsd:: "<<cur.x<<","<<cur.y<<" :tie "<<arr[cur.x][cur.y].time<<endl; 40 continue; 41 } 42 for(int i=0;i<4;i++) 43 { 44 pot next={cur.x+dir[i][0],cur.y+dir[i][1]}; 45 if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&arr[next.x][next.y].ch!='#') 46 { 47 int zhi; 48 if(arr[next.x][next.y].ch=='x') zhi=2; 49 else zhi=1; 50 if(flag==1) 51 { 52 if(arr[cur.x][cur.y].time+zhi<mintime&&(arr[next.x][next.y].time==0||arr[next.x][next.y].time>arr[cur.x][cur.y].time+zhi)) 53 { 54 arr[next.x][next.y].time=arr[cur.x][cur.y].time+zhi; 55 que.push(next); 56 } 57 } 58 else 59 { 60 if(arr[next.x][next.y].time==0||arr[next.x][next.y].time>arr[cur.x][cur.y].time+zhi) 61 { 62 arr[next.x][next.y].time=arr[cur.x][cur.y].time+zhi; 63 que.push(next); 64 } 65 } 66 } 67 } 68 } 69 return mintime; 70 } 71 int main() 72 { 73 //ifstream in("data.txt"); 74 while(cin>>n>>m) 75 { 76 int i,j; 77 for(i=0;i<n;i++) 78 for(j=0;j<m;j++) 79 { 80 cin>>arr[i][j].ch; 81 arr[i][j].time=0; 82 if(arr[i][j].ch=='a') 83 { anglex=i; angley=j;} 84 } 85 int result=BFS(); 86 if(result<INT_MAX) 87 cout<<result<<endl; 88 else 89 cout<<"Poor ANGEL has to stay in the prison all his life."<<endl; 90 } 91 return 0; 92 }
AC代码(BFS+QUEUE)
版本:多个r往a方向BFS
1 #include<iostream> 2 #include<fstream> 3 #include<queue> 4 #include<string> 5 #include<cstring> 6 #include<vector> 7 #include<limits> 8 using namespace std; 9 const int MAX=203; 10 struct pot 11 { 12 int x,y; 13 }; 14 struct data 15 { 16 int time; char ch; 17 }; 18 data arr[MAX][MAX]; 19 vector<pot> vec; 20 int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}}; 21 int n,m; 22 int anglex,angley; 23 int BFS() 24 { 25 int j=-1; 26 int mintime=INT_MAX,flag=0; 27 while(++j<vec.size()) 28 { 29 pot beg=vec[j]; 30 queue<pot> que; 31 que.push(beg); 32 while(!que.empty()) 33 { 34 pot cur=que.front(); 35 que.pop(); 36 if(cur.x==anglex&&cur.y==angley) 37 { 38 if(arr[cur.x][cur.y].time<mintime) 39 { 40 flag=1; 41 mintime=arr[cur.x][cur.y].time; 42 } 43 continue; 44 } 45 for(int i=0;i<4;i++) 46 { 47 pot next={cur.x+dir[i][0],cur.y+dir[i][1]}; 48 if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&arr[next.x][next.y].ch!='#'&&arr[next.x][next.y].ch!='r') 49 { 50 int zhi; 51 if(arr[next.x][next.y].ch=='.'||arr[next.x][next.y].ch=='a') zhi=0; 52 else zhi=1; 53 if(flag==1) 54 { 55 if(arr[cur.x][cur.y].time+zhi+1<mintime&&(arr[next.x][next.y].time==0||arr[next.x][next.y].time>arr[cur.x][cur.y].time+zhi+1)) 56 { 57 arr[next.x][next.y].time=arr[cur.x][cur.y].time+zhi+1; 58 que.push(next); 59 } 60 } 61 else 62 { 63 if(arr[next.x][next.y].time==0||arr[next.x][next.y].time>arr[cur.x][cur.y].time+zhi+1) 64 { 65 arr[next.x][next.y].time=arr[cur.x][cur.y].time+zhi+1; 66 que.push(next); 67 } 68 } 69 } 70 } 71 } 72 } 73 return mintime; 74 } 75 int main() 76 { 77 //ifstream in("data.txt"); 78 while(cin>>n>>m) 79 { 80 int i,j; 81 for(i=0;i<n;i++) 82 for(j=0;j<m;j++) 83 { 84 cin>>arr[i][j].ch; 85 arr[i][j].time=0; 86 if(arr[i][j].ch=='a') 87 { anglex=i; angley=j;continue;} 88 if(arr[i][j].ch=='r') 89 { 90 pot fri={i,j}; 91 vec.push_back(fri); 92 } 93 } 94 int result=BFS(); 95 if(result<INT_MAX) 96 cout<<result<<endl; 97 else 98 cout<<"Poor ANGEL has to stay in the prison all his life."<<endl; 99 vec.clear(); 100 } 101 return 0; 102 }
AC代码(BFS+优先队列)
版本:从a开始BFS
1 #include<iostream> 2 #include<fstream> 3 #include<queue> 4 #include<string> 5 #include<cstring> 6 using namespace std; 7 const int MAX=203; 8 struct pot 9 { 10 int x,y; 11 }; 12 struct data 13 { 14 int x,y,time; 15 friend bool operator<(const data &a,const data &b) 16 { 17 return a.time>b.time; 18 } 19 }; 20 char arr[MAX][MAX]; 21 int vis[MAX][MAX]; 22 int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}}; 23 int n,m; 24 int anglex,angley; 25 int BFS() 26 { 27 data beg={anglex,angley,0}; 28 priority_queue<data> que; 29 que.push(beg); 30 vis[anglex][angley]=1; 31 while(!que.empty()) 32 { 33 data cur=que.top(); 34 que.pop(); 35 if(arr[cur.x][cur.y]=='r') 36 return cur.time; 37 for(int i=0;i<4;i++) 38 { 39 data next={cur.x+dir[i][0],cur.y+dir[i][1],0}; 40 if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&arr[next.x][next.y]!='#'&&vis[next.x][next.y]==0) 41 { 42 if(arr[next.x][next.y]=='x') 43 next.time=cur.time+2; 44 else 45 next.time=cur.time+1; 46 vis[next.x][next.y]=1; 47 que.push(next); 48 } 49 } 50 } 51 return 0; 52 } 53 int main() 54 { 55 //ifstream in("data.txt"); 56 while(cin>>n>>m) 57 { 58 int i,j; 59 for(i=0;i<n;i++) 60 for(j=0;j<m;j++) 61 { 62 cin>>arr[i][j]; 63 if(arr[i][j]=='a') 64 { anglex=i; angley=j;} 65 } 66 memset(vis,0,sizeof(vis)); 67 int result=BFS(); 68 if(result>0) 69 cout<<result<<endl; 70 else 71 cout<<"Poor ANGEL has to stay in the prison all his life."<<endl; 72 } 73 return 0; 74 }
//a往r方向BFS