hdoj 1242 Rescue
来源:互联网 发布:python适合初学者的书 编辑:程序博客网 时间:2024/06/06 21:06
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21313 Accepted Submission(s): 7598
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代码:#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;elsevis[i][j]=1;} }bfs();}return 0;}
0 0
- hdoj 1242 Rescue(bfs)
- hdoj 1242 Rescue
- hdoj 1242 Rescue
- hdoj-1242-Rescue(DFS)
- HDOJ 1242 Rescue
- hdoj 1242 Rescue (BFS)
- 【hdoj 1242】Rescue
- HDOJ--1242--Rescue
- hdoj 1242 Rescue
- hdoj 1242 Rescue
- hdoj 1242 Rescue [BFS]
- HDOJ-1242-Rescue
- HDOJ 1242 Rescue【BFS】
- hdoj 1242 Rescue
- hdoj 1242 Rescue 【bfs】
- HDOJ 1242 Rescue
- hdoj 1242Rescue
- HDOJ-1242 Rescue
- poj2109Power of Cryptography 大数开根
- HDU 1728 逃离迷宫 (搜索)
- cocos2d-js 取plist 数据
- web service 的概述(一)
- 设置软件开机自启动(C#实例演示)
- hdoj 1242 Rescue
- UVa 515 King (差分约束+线性规划+判负圈)
- Symmetric Tree
- Python伪装浏览器请求爬虫豆瓣小组
- Prim算法
- C++中的组合模式
- Linux - Shell变量的配置守则
- 通过服务器实现客户端的相互通话。(需要导入jar包)论保存的重要性
- nginx 只允许指定IP访问