专题二 1012
来源:互联网 发布:魅可mac加盟电话 编辑:程序博客网 时间:2024/06/05 16:26
一. 题目编号
专题二 1012
Rescue
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.)
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.
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
二. 简单题意
描述
给出一个n行m列的图,’#’代表墙,’.’代表守卫,’.’代表路,’a’代表angel,’r’代表angel的朋友。angel的朋友要接近angel,可以走’.’和’x’,每走一步需要1个单位的时间,当经过x所在的位置时,要杀死x才能继续往前走,但杀死x同样要消耗1个单位的时间。问从r到a最少需要多长时间;如果不能到达,输出”Poor ANGEL has to stay in the prison all his life.”。
重点
主人公的朋友可能有多个,这样我们必须从主人公出发去寻找他到所有朋友处的最小值,就是答案。
三. 解题思路形成过程
使用优先队列,每次从优先队列中取出步数最小的点,从这个点开始搜,直到找到a或者搜索结束。
如果不用优先队列,也可以在搜到一个x时,直接把这个点压入队列,不再往它的四个方向搜,能保证这个点能被再次搜到。
四. 感想
一定要读清题意!!!
不然错好几遍都不知道怎么错的,这不是单纯的简单BFS,得用BFS+优先队列才行
五. AC代码
#include <iostream>#include <queue>#include <cstring>using namespace std;struct node{ int x, y; int step;};queue<node> q;int N, M, prove, sx, sy, visit[202][202];char map[202][202];int dx[4] = {-1, 1, 0, 0};int dy[4] = {0, 0, 1, -1,};int check(int x, int y){ if(x < 0 || x >= N || y < 0 || y >= M) return 0; else return 1;}void BFS(){ while(!q.empty()) q.pop(); node s, e; memset(visit, 0, sizeof(visit)); s.step = 0; s.x = sx; s.y = sy; q.push(s); visit[s.x][s.y] = 1; while(!q.empty()) { s = q.front(); q.pop(); if(map[s.x][s.y] == 'a') { cout << s.step << endl; prove =1; } //如取出的是在有警卫的格子中,即杀掉警卫再次进入队列 if(map[s.x][s.y] == 'x') { map[s.x][s.y] = '.'; s.step += 1; q.push(s); continue; } for(int i = 0; i < 4; i++) { e.x = s.x + dx[i]; e.y = s.y + dy[i]; if(!check(e.x, e.y) || visit[e.x][e.y] || map[e.x][e.y] == '#') continue; e.step = s.step + 1; q.push(e); visit[e.x][e.y] = 1; } }}int main(){ while(cin >> N >> M) { for(int i = 0; i < N; i++) for(int j = 0; j < M; j++) { cin >> map[i][j]; if(map[i][j] == 'r') { sx = i; sy = j; } } prove = 0; BFS(); if(!prove) cout << "Poor ANGEL has to stay in the prison all his life." << endl; } return 0;}
- 专题二 1012
- 2016sdau课程练习专题二 1012
- 3G专题二
- LDAP专题(二)
- 毕业论文专题 (二)
- IPV6专题(二)
- 专题二-可复用单链表
- 专题二 · 1009
- 专题二1001
- 专题二1002
- 专题二1005
- 专题二 1001
- 专题二:搜索总结报告
- 专题二1008
- 专题二 · 1008
- 专题二1019
- 专题二 1008
- 专题二 1002
- The 13th Zhejiang Provincial Collegiate Programming Contest
- svn
- 案例分析:福彩双色球
- 第9周项目2——我的数组类
- [JZOJ4468][JSOI2016?]轻重路径
- 专题二 1012
- Space Shooter之子弹和敌人碰撞检测、爆炸特效以及敌人的运动
- putchar
- poj 1952buy low, buy lower
- hdoj 5671 Matrix 【思维】
- C++作业4
- 计算文本的实际大小
- 第9周项目3——人数不定的工资类
- hdoj 5672 String 【模拟】