HDU/HDOJ 1242 Rescue 典型的迷宫广度优先搜索题
2013-05-31 19:22
387 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242
题目大意:在含有障碍的迷宫中求从一点到一点的最短时间或步数,这个题目有个坑爹的地方,天使的朋友不止一个,所以搜索的起点应该是从天使开始搜到朋友为止,最短路,还有一个坑爹的地方,题目求的是最短时间,不是最短步数,所以应该才用优先队列,小步数优先,结果我两种方法都试过,都能AC;
上代码:187MS 440K
题目大意:在含有障碍的迷宫中求从一点到一点的最短时间或步数,这个题目有个坑爹的地方,天使的朋友不止一个,所以搜索的起点应该是从天使开始搜到朋友为止,最短路,还有一个坑爹的地方,题目求的是最短时间,不是最短步数,所以应该才用优先队列,小步数优先,结果我两种方法都试过,都能AC;
上代码:187MS 440K
#include <iostream> #include <string> #include <cstdio> #include <cmath> #include <vector> #include <algorithm> #include <sstream> #include <cstdlib> #include <fstream> #include <queue> using namespace std; struct node{ int x,y,step; }; /* bool operator<(node a,node b){ return a.step > b.step; }*/ queue<node> Q; //priority_queue<node> Q; bool visit[210][210]; char maze[210][210]; int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}}; int sum,m,n,sx,sy,endx,endy; bool isbound(int a,int b){ if(a<1 || a>m || b<1 || b>n)return true; return false; } void bfs(){ node p,q; p.x=sx; p.y=sy; p.step=0; Q.push(p); while(!Q.empty()) { p=Q.front(); Q.pop(); if(p.x==endx&&p.y==endy){cout<<p.step<<endl;return;} for(int i=0;i<4;i++) { q.x=p.x+dir[i][0]; q.y=p.y+dir[i][1]; if(isbound(q.x,q.y))continue;//边界 if(maze[q.x][q.y]=='#')continue;//障碍 if(visit[q.x][q.y])continue; //判重 if(maze[q.x][q.y]=='.'||maze[q.x][q.y]=='r'){ //注意搜素方向 q.step=p.step+1; visit[q.x][q.y]=1; Q.push(q); } if(maze[q.x][q.y]=='x'){ q.step=p.step+2; //这是守卫的情况 visit[q.x][q.y]=1; Q.push(q); } } } cout<<"Poor ANGEL has to stay in the prison all his life."<<endl; } int main() { // ifstream fin; // fin.open("data1.txt"); while(cin>>m>>n) { while(!Q.empty())Q.pop(); for(int i=1;i<=m;i++){ for(int j=1;j<=n;j++){ cin>>maze[i][j]; if(maze[i][j]=='r'){ endx=i; endy=j; } if(maze[i][j]=='a'){ sx=i; sy=j; } } } memset(visit,0,sizeof(visit)); visit[sx][sy]=1; bfs(); } return 0; }
相关文章推荐
- 广度优先搜索nodeHDU/HDOJ 1242 Rescue 典型的迷宫广度优先搜索题
- HDU/HDOJ 1242 Rescue 典型的迷宫广度优先搜索题
- HDU/HDOJ 2102 A计划 广度优先搜索BFS
- HDU/HDOJ 2102 A计划 广度优先搜索BFS
- 广度优先搜索C++练习题HDU 1242 Rescue
- 【搜索之BFS + 优先队列】杭电 hdu 1242 Rescue
- 迷宫:广度优先搜索求最短路径
- (BFS)广度优先搜索例子:迷宫,寻找连块数
- hdoj-1242-Rescue【广搜+优先队列】
- poj 3984 迷宫问题 BFS 图的广度优先搜索
- 迷宫问题的求解方式:应用深度优先和广度优先的搜索
- 广度优先搜索求解迷宫问题
- HDOJ 1242 Rescue(BFS+优先对列)
- 搜索2(广度优先)Rescue
- 迷宫问题(广度优先搜索,输出最短路径)
- 广度优先搜索求解迷宫问题
- BFS-迷宫问题-用宽度(广度)优先搜索解决最优路径问题
- 写给妹妹的编程札记 5 - 搜索: 迷宫问题 - 广度优先搜索
- 迷宫问题(广度优先搜索BFS)
- 用广度优先搜索解迷宫问题