hdoj 1242 Rescue
2015-08-06 21:41
459 查看
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21313 Accepted Submission(s): 7598
[align=left]Problem Description[/align]
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.)
[align=left]Input[/align]
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.
[align=left]Output[/align]
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."
[align=left]Sample Input[/align]
7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........
[align=left]Sample Output[/align]
13 代码: [code]#include<stdio.h> #include<string.h> #include<queue> #include<algorithm> using namespace std; struct record { int x,y; int time; }; int n,m,x1,y1; int vis[220][220]; int dx[4]={-1,1,0,0}; int dy[4]={0,0,-1,1}; char s[220][220]; queue<record>q; void bfs() { record e,temp; while(!q.empty()) { q.pop(); } vis[x1][y1]=1; e.x=x1; e.y=y1; e.time=0; q.push(e); while(!q.empty()) { e=q.front(); q.pop(); for(int i=0;i<4;i++) { temp=e; temp.x+=dx[i]; temp.y+=dy[i]; if(s[temp.x][temp.y]=='x') { temp.time=e.time+2; } else { temp.time=e.time+1; } if(temp.x>=0&&temp.x<n&&temp.y>=0&&temp.y<m&&!vis[temp.x][temp.y]) { if(s[temp.x][temp.y]=='r') { printf("%d\n",temp.time); return ; } vis[temp.x][temp.y]=1; q.push(temp); } } } printf("Poor ANGEL has to stay in the prison all his life.\n"); } int main() { int i,j; while(scanf("%d%d",&n,&m)!=EOF) { for(i=0;i<n;i++) { scanf("%s",s[i]); } for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(s[i][j]=='a') { x1=i,y1=j; vis[i][j]=0; } else if(s[i][j]=='r') { vis[i][j]=0; } else if(s[i][j]=='.') vis[i][j]=0; else if(s[i][j]=='x') vis[i][j]=0; else vis[i][j]=1; } } bfs(); } return 0; }
[/code]
相关文章推荐
- UE4 符号调试器
- [IOS]今天开始学UI---UIProgressView
- Round #311-Equivalent Strings**
- UE4 iOS游戏开发
- poj 1458 Common Subsequence(最大公共子序列)
- UE4 移动平台游戏开发
- UE4 iOS设备的兼容性
- [译]Front-end-Developer-Interview-Questions
- 解释java中StringBuilder、StringBuffer、String类之间的关系
- HDU 1047 Integer Inquiry
- iOS UIActivityViewController 接入WeChatSDK 微信 朋友圈分享
- 登陆界面(LTView,UITextField,UIButton)
- Easyui使用记录
- ios UIView
- Android Pitfall - Fragment.startActivityForResult(), requestCode == 65536
- easyUI的iframe子页面操作父页面元素
- BadParcelableException:Parcelable protocol requires a Parcelable 错误
- iOS7中UITableViewCell的draw rect方法,不能画边框的问题
- (素材源码)swanzhu学IOS(一)UI之_雪花下落
- UITextField