hdu 1242 bfs
来源:互联网 发布:dojo 加载js 编辑:程序博客网 时间:2024/05/15 07:25
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
题解:
此题坑点在于有多个朋友。其他bfs优先队列求最短路即可。
代码:
#include <iostream>#include <algorithm>#include <cstdio>#include <queue>using namespace std;typedef pair<int,int> P;const int maxn = 200+10;const int INF = 0x3f3f3f3f;char matrix[maxn][maxn];int d[maxn][maxn];int dx[4]={1,0,-1,0};int dy[4]={0,1,0,-1};int N,M;int sx,sy;int gx,gy;void bfs(){ queue<P> que; for(int i=0;i<N;i++) for(int j=0;j<M;j++) { d[i][j]=INF; if(matrix[i][j]=='a') {sx=i;sy=j;} if(matrix[i][j]=='r') {gx=i;gy=j;} } que.push(P(sx,sy)); d[sx][sy]=0; while(que.size()) { P p = que.front();que.pop(); if(p.first==gx&&p.second==gy) break; for(int i=0;i<4;i++) { int nx = p.first+dx[i],ny = p.second+dy[i]; if(0<=nx&&nx<N&&0<=ny&&ny<M&&matrix[nx][ny]!='#'&&d[nx][ny]==INF) { que.push(P(nx,ny)); d[nx][ny]=d[p.first][p.second]+1; if(matrix[nx][ny]=='x') d[nx][ny]++; } } }}int main(){ while(scanf("%d%d",&N,&M)!=EOF) { for(int i=0;i<N;i++) scanf("%s",matrix[i]); bfs(); int ans = d[gx][gy]; if(ans==INF) { cout<<"Poor ANGEL has to stay in the prison all his life."<<endl; } else { cout<<ans<<endl; } } return 0;}
- hdu 1242 bfs
- HDU-1242(BFS新思路)
- HDU 1242 简单bfs
- hdu 1242 BFS
- hdu 1242 BFS
- HDU 1242(bfs)
- hdu 1242 Rescue (bfs)
- HDU 1242 Rescue BFS
- HDU--1242:Rescue (BFS)
- hdu 1242 bfs
- hdu~1242(bfs)
- HDU 1242:Rescue 【bfs】
- HDU 1242 BFS
- hdu 1242 Rescue(bfs)
- HDU-1242-Rescue【BFS】
- HDU 1242 BFS-Rescue
- hdu 1242 bfs+priority_queue
- 【BFS】hdu 1242 Rescue
- ElasticSearch Java API中的and、or和not
- 阿里云热修复技术Sophix的快速接入
- C/C++内存管理
- 【数论】教堂
- python爬取百度新闻所有的新闻的前1页 标题和URL地址
- hdu 1242 bfs
- Android动画分析
- openerp-server.con详解f配置
- PPT组件Spire.Presentation教程:在C#中将PPT文档转换为EMF图像
- Linux安装jdk
- Oralce增量同步方案系列2-Kettle增量模型
- 使用OpenGL ES进行高效文字渲染
- Mysql SQL优化&执行计划
- Java Sever Pages