广度优先搜索C++练习题HDU 1242 Rescue
2013-10-19 19:08
447 查看
RescueTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10194 Accepted Submission(s): 3728 Problem DescriptionAngel 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.) InputFirst 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. OutputFor 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 Input7 8#.#####.#.a#..r.#..#x.....#..#.##...##...#.............. Sample Output13 AuthorCHEN, Xue SourceZOJ Monthly, October 2003 RecommendEddy解题思路:记忆化的BFS,利用一个数组来维护到固定点的最小值,但是这一题有一个坑,就是主人公的朋友可能有多个,这样我们必须从主人公出发去寻找他到所有朋友处的最小值,就是答案。[cpp]
#include<iostream> #include<cstring> #include<queue> using namespace std; #define INF 99999 char maze[205][205]; //迷宫的二维数组 long len[205][205]; //从a点到任意可行点的最短距离 int mov[4][2]={{1,0},{-1,0},{0,1},{0,-1}};//R,L,D,U,四种走法 void BFS(int xa,int ya) //以(xa,ya)为根节点开始进行BFS { int x0,y0,j,p,q; //(x0,y0)存储每次的可扩展点坐标 queue<int> x;queue<int> y; //x,y队列分别保存可扩展点的x,y坐标 x.push(xa);y.push(ya); //第一步先把根节点放入扩展队列中 while(!x.empty()&&!y.empty()) //可扩展点表为空时,搜索结束 { x0=x.front();y0=y.front(); //每次取出可扩展点,准备进行扩展 x.pop();y.pop(); for(j=0;j<4;j++) { //以下带有一种剪枝,即只在有更短路径的时候将这一路径加入队列进行搜索
//如果搜索到的这一点的原先路径比从搜索点走更远,或者该点尚未搜索到,就更改到这一点的最短距离 p=x0+mov[j][0];q=y0+mov[j][1]; if(maze[p][q]=='.'||maze[p][q]=='r'||maze[p][q]=='x') { if(maze[p][q]=='x')//遇到守卫,直接干掉看守比绕路更快 { if(len[x0][y0]+2<len[p][q]||len[p][q]==0) //若按照当前走到(p,q)的方式的耗散值比原先到达(p,q)的耗散值低,(p,q)还没被设置耗散值 { len[p][q]=len[x0][y0]+2; x.push(p);y.push(q); } } else if(len[x0][y0]+1<len[p][q]||len[p][q]==0) { len[p][q]=len[x0][y0]+1; x.push(p);y.push(q); } } } } } int main() { int m,n,i,j,a,b; long ans; while(cin>>m>>n) { ans=INF; memset(maze,'*',sizeof(maze)); memset(len,0,sizeof(len)); for(i=1;i<=m;i++) for(j=1;j<=n;j++) { cin>>maze[i][j]; if(maze[i][j]=='a') { a=i;b=j; } } BFS(a,b);
//选择这样的点:值r,且到这个点的最小距离len[i][j]最小
//该点的len值为答案 for(i=1;i<=m;i++) for(j=1;j<=n;j++) www.2cto.com if(maze[i][j]=='r'&&ans>len[i][j]&&len[i][j])//len为0说明走不到这一点 ans=len[i][j]; if(ans==INF)//ans未能更新说明主人公碰不到他的朋友(们) cout<<"Poor ANGEL has to stay in the prison all his life."<<endl; else cout<<ans<<endl; } return 0; }
相关文章推荐
- 【搜索入门专题1】 hdu1242 J - Rescue c++ stl容器之优先队列+BFS
- 广度优先搜索nodeHDU/HDOJ 1242 Rescue 典型的迷宫广度优先搜索题
- 【搜索之BFS + 优先队列】杭电 hdu 1242 Rescue
- HDU/HDOJ 1242 Rescue 典型的迷宫广度优先搜索题
- 《算法导论》中广度优先搜索的实现 c++
- HDU 1242 Rescue(BFS+【优先队列】)
- 搜索专题: HDU1242 Rescue
- hdu 1242 Rescue(BFS,优先队列,基础)
- hdu 1242 Rescue(优先队列 && 广搜BFS)
- 图的深度、广度优先搜索——C/C++
- hdu 1242 Rescue(优先队列 && 广搜BFS)
- HDU 1242 Rescue (搜索 DFS)
- hdu 1072 广度优先搜索编程方法 细致的讲解哦
- hdu 1242 Rescue 搜索 bfs 优先队列
- hdu 1242 Rescue(优先队列 && 广搜BFS)
- HDU 1242 Rescue (搜索)水题
- hdu-1242Rescue(bfs搜索+优先队列)
- hdu-1242-Rescue(优先队列 BFS)
- hdu 1242 Rescue BFS搜索
- 广度优先搜索:A strange lift HDU - 1548