博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Rescue
阅读量:5973 次
发布时间:2019-06-19

本文共 3136 字,大约阅读时间需要 10 分钟。

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

题解:广搜,每次找时间最短的一个,因为遇到士兵时间多1,此时这个点应该排在后面。所以用优先队列排序。

#include 
#include
#include
#include
using namespace std;struct Node{ int x; int y; int time; Node(int a,int b,int c) { x = a; y = b; time = c; } bool operator< (Node t) const { return time > t.time; }};int sx,sy;int n,m;int ans;char map[205][205];int d[4][2] = {
{0,-1},{0,1},{-1,0},{1,0}};bool bfs(){ priority_queue
q; q.push(Node(sx,sy,0)); ans = 0; map[sx][sy] = '#'; while(!q.empty()) { Node p = q.top(); q.pop(); for(int i = 0;i < 4;i++) { int x = p.x + d[i][0]; int y = p.y + d[i][1]; if(x >= 0 && x < n && y >= 0 && y < m && map[x][y] != '#') { if(map[x][y] == 'a') { ans = p.time + 1; return true; } if(map[x][y] == 'x') { q.push(Node(x,y,p.time + 2)); } else { q.push(Node(x,y,p.time + 1)); } map[x][y] = '#'; } } } return false; }int main(){ while(scanf("%d%d",&n,&m) != EOF) { getchar(); for(int i = 0;i < n;i++) { for(int j = 0;j < m;j++) { scanf("%c",&map[i][j]); if(map[i][j] == 'r') { sx = i; sy = j; } } getchar(); } if(bfs()) { printf("%d\n",ans); } else { printf("Poor ANGEL has to stay in the prison all his life.\n"); } } return 0;}

转载地址:http://xcbox.baihongyu.com/

你可能感兴趣的文章
多种DOM事件处理程序分析及跨浏览器的事件解决方案
查看>>
easyui Tree 全选节点
查看>>
Java 内存分配及垃圾回收机制初探
查看>>
iOS设计模式(代码分析系列2:简单工厂模式)
查看>>
2、zookeeper原理理解
查看>>
spring mvc interceptor拦截器拦截请求
查看>>
奇偶链表(奇数节点位于偶数节点之前) Odd Even Linked List
查看>>
Mysql中的 的 Cascade ,NO ACTION ,Restrict ,SET NULL
查看>>
我所理解的分布式调度
查看>>
git windows 保存用户名和密码
查看>>
高性能MYSQL读书要点摘录_2_性能刨析工具_pt-query-digest
查看>>
邮箱、手机号、QQ号正则表达式
查看>>
Python 字符串与unicode对象 关于与区别 encode、decode
查看>>
node 知识点
查看>>
mybatis获取当前插入记录的id
查看>>
如何使用Terminalizer用于记录您的Linux终端会话并生成动画gif图像
查看>>
Spring3 MVC Hello World(3)使用JSON进行前后台数据交互
查看>>
WSDL实例解析
查看>>
用netbeans开发Swing程序,添加自定义控件
查看>>
LINUX Daemon程序设计
查看>>