ZOJ - 1649 Rescue
来源:互联网 发布:信天翁 爱情 知乎 编辑:程序博客网 时间:2024/06/03 22:41
题目:
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.
//x是guard,r不一定只有1个,#是墙
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.
//x是guard,r不一定只有1个,#是墙
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
这个题目在OJ里面的掉了一些内容,我已经补上了。
广度优先搜索代码:
#include<iostream>#include<queue>#include<string.h>using namespace std;int n, m;int list[200][200];int list2[200][200];//list2只用来判断是不是敌人xqueue<int>q;void f(int a, int b,int c,int d){if (a < 0 || a >= n)return;if (b < 0 || b >= m)return;if (list[a][b] == -1){list[a][b] = list[c][d] + 1;q.push(a * 201 + b);}else if (list[a][b] == -3){list[a][b] = list[c][d] + 2;q.push(a * 201 + b);}else if (list[a][b] > list[c][d] + 1)//因为没有用优先队列,所以有些格子需要重新进入队列{list[a][b] = list[c][d] + 1;if (list2[a][b])list[a][b]++;q.push(a * 201 + b);}}int main(){int a, b;char c;int x, y;while (cin>>n>>m){while (q.size())q.pop();memset(list2, 0, sizeof(list2));for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){cin >> c;if (c == '.')list[i][j] = -1;else if (c == '#')list[i][j] = -2;else if (c == 'r'){list[i][j] = 0;q.push(i * 201 + j);}else if (c == 'x'){list[i][j] = -3;list2[i][j] = 1;}else{x = i;y = j;list[x][y] = -1;}}}while (!q.empty())//即使list[x][y]>0也不能结束循环,必须队列为空才能得到list[x][y]的最小值{a = q.front() / 201;b = q.front() % 201;q.pop();f(a + 1, b, a, b);f(a - 1, b, a, b);f(a, b + 1, a, b);f(a, b - 1, a, b);}if (list[x][y]>0)cout << list[x][y];else cout << "Poor ANGEL has to stay in the prison all his life.";cout << endl;}return 0;}
3 0
- zoj 1649 Rescue
- ZOJ 1649 Rescue
- zoj 1649 Rescue
- ZOJ 1649 Rescue (BFS)
- zoj 1649 rescue (bfs)
- ZOJ-1649-Rescue
- zoj 1649 Rescue
- ZOJ:1649 Rescue
- ZOJ 1649 Rescue
- ACM ZOJ 1649 (Rescue)
- ZOJ(1649)Rescue
- ZOJ 1649 Rescue
- zoj 1649 Rescue
- zoj 1649 Rescue
- ZOJ 1649 : Rescue - BFS
- ZOJ 1649 Rescue
- zoj 1649 Rescue
- zoj 1649 Rescue bfs
- JS原型题集
- HDU 1827 3072强联通分量
- HDU 5761 Rower Bo (数学题,积分)
- 欢迎使用CSDN-markdown编辑器
- 工厂模式
- ZOJ - 1649 Rescue
- LeetCode总结
- 人生经验 我想在大学中学一门技术,我该怎么办?
- Java操作Excel导出数据到Excel文档
- 最普通的java for循环
- Linux页表初始化
- JAVA7新特性:数字下划线分隔符
- linux shell数据重定向(输入重定向与输出重定向)详细分析
- static全局变量、局部变量、函数