HDOJ1242 基础宽搜题
来源:互联网 发布:中文安卓编程 编辑:程序博客网 时间:2024/06/04 20:03
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 32204 Accepted Submission(s): 11245
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
Author
CHEN, Xue
Source
ZOJ Monthly, October 2003
Recommend
Eddy | We have carefully selected several similar problems for you: 1240 1072 1372 1175 1312
思路很简单:宽搜,遇到警卫要特殊处理,把地图上的'x'变成'.' ,队列元素时间加1,并重新入队(这是关键)
#include <iostream>#include <queue>#include <stdio.h>#include <algorithm>#include <string.h>using namespace std;const int maxn = 205;int n,m,ex,ey;char map[maxn][maxn];bool used[maxn][maxn];int dx[]={-1,0,1,0};int dy[]={0,-1,0,1};struct node{ int x,y,t;};queue<node>Q;node q,p;bool islegal(int x ,int y){ if (x<1 || y<1 || x>n || y>m || map[x][y]=='#') return 0; return 1;}int bfs(){ while (!Q.empty()){ p=Q.front(); Q.pop(); if (p.x== ex && p.y == ey) return p.t; if (map[p.x][p.y]=='x'){ //警卫特殊处理 map[p.x][p.y]='.'; //修改地图 p.t++; //队列元素加一 Q.push(p); //重入队列 continue; } for (int i=0;i<4;i++){ q=p; q.x+=dx[i]; q.y+=dy[i]; q.t++; if (islegal(q.x,q.y) && !used[q.x][q.y]) { used[q.x][q.y]=1; Q.push(q); } } } return -1;}int main(){ while (cin>>n>>m){ for (int i=1;i<=n;i++){ for (int j=1;j<=m;j++){ cin>>map[i][j]; if (map[i][j]=='r'){ p.x=i; p.y=j; p.t=0; Q.push(p); } if (map[i][j]=='a'){ ex=i; ey=j; } } } memset(used,0,sizeof(used)); int ans = bfs(); if (ans==-1) printf("Poor ANGEL has to stay in the prison all his life.\n"); else printf("%d\n",ans); } return 0;}
阅读全文
0 0
- HDOJ1242 基础宽搜题
- BFS--hdoj1242
- hdoj1242 Rescue
- HDOJ1242(BFS)
- Rescue<hdoj1242>
- hdoj1242-【Rescue】
- 【记忆化DFS】HDOJ1242 Rescue
- 迷宫问题:hdoj1242--BFS基本应用
- 基础
- 基础
- 基础
- 基础
- 基础
- 基础
- 基础
- 基础
- 基础
- 基础
- [Python]深拷贝效率
- android 4.0以上横竖屏切换不销毁activity,不重新走生命周期,只调用onConfigurationChanged
- 第 4 章 模型
- 算法——最短路径——Dijkstra算法
- javax.ws.rs.BadRequestException: HTTP 400 Bad Request错误处理
- HDOJ1242 基础宽搜题
- 最长子字符串-无重复字符
- js事件
- 快速排序和归并排序的实现
- 工具解析|杀毒引擎惨遭打脸,黑帽大会爆惊天免杀工具
- 事件
- [Python]重载运算符
- 文件系统编程的应用
- murmur2