HDU/HDOJ 1242 Rescue 典型的迷宫广度优先搜索题
来源:互联网 发布:edu域名如何注册 编辑:程序博客网 时间:2024/06/05 09:09
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242
题目大意:在含有障碍的迷宫中求从一点到一点的最短时间或步数,这个题目有个坑爹的地方,天使的朋友不止一个,所以搜索的起点应该是从天使开始搜到朋友为止,最短路,还有一个坑爹的地方,题目求的是最短时间,不是最短步数,所以应该才用优先队列,小步数优先,结果我两种方法都试过,都能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;}
- HDU/HDOJ 1242 Rescue 典型的迷宫广度优先搜索题
- HDU 1242 Rescue (BFS(广度优先搜索))
- 广度优先搜索C++练习题HDU 1242 Rescue
- Rescue(广度优先搜索)
- HDU/HDOJ 1254 推箱子 嵌套的广度优先搜索
- HDU/HDOJ 1253 胜利大逃亡 广度优先搜索算法 迷宫寻路
- Hdu1242 - Rescue - 广度优先搜索
- 迷宫之广度优先搜索
- 迷宫问题-广度优先搜索
- 迷宫问题-广度优先搜索
- 广度优先搜索:迷宫问题
- hdu-1242Rescue(bfs搜索+优先队列)
- hdu 1242 Rescue 搜索 bfs 优先队列
- 搜索2(广度优先)Rescue
- HDU/HDOJ 2102 A计划 广度优先搜索BFS
- poj 3984 迷宫问题 BFS 图的广度优先搜索
- 广度优先搜索迷宫路径算法的实现
- 广度优先搜索迷宫的最短路径走法!
- Windows Phone实现对SQL Server 2008的增删改查
- hdu 1210 简单数学模拟
- Linux 可加载内核模块剖析
- IE8中弹出窗口 隐藏地址栏
- vc api_获取系统信息和用户信息
- HDU/HDOJ 1242 Rescue 典型的迷宫广度优先搜索题
- Oracle 数据库表空间容量调整(表空间缩容脚本)脚本
- hdu 1213 并差集
- hdu-4112-Break the Chocolate
- 浣溪沙——纳兰性德
- hdu 1421 dp
- 关于java中<T>问题
- 一个servlet实现GPS坐标偏移接口功能
- hdu 1568 斐波那契性质