Hdu 1026 Ignatius and the Princess I (BFS 优先队列+路径输出)
来源:互联网 发布:英格拉姆赛季数据 编辑:程序博客网 时间:2024/05/01 13:46
给一个 N*M 的四方向连通的图, 要从左上角走到右下角,. : 代表可以走,X : 表示是墙,不可以走,n : 代表这里有一个怪兽,打败怪兽用时 n,每走一步耗时 1 .如果能到达,则输出最小时间和每一步的走法。保证起点没有怪兽,终点不是墙,可能有怪兽。
#include<cstring>#include<cstdio>#include<queue>using namespace std;const int N=105;int dx[4]={0,0,-1,1};int dy[4]={1,-1,0,0};char g[N][N];bool vis[N][N];int n,m;struct Node{ int x,y,t; int id,pre; void Get (int _x,int _y,int _t,int _id,int _pre) { x=_x;y=_y;t=_t;id=_id;pre=_pre; } bool operator < (const Node a) const { return t>a.t; }}que[N*N];bool OK (int x,int y){ return x>=0 && x<n && y>=0 && y<m && g[x][y]!='X' && vis[x][y]==false;}int BFS (){ int x,y; priority_queue<Node> q; Node now; int cur=0; que[cur].Get(0,0,0,0,-1); q.push(que[cur++]); while (q.empty()==false) { now=q.top(); q.pop(); for (int i=0;i<4;i++) { x=now.x+dx[i]; y=now.y+dy[i]; if (OK(x,y)) { que[cur].Get(x,y,0,cur,now.id); que[cur].t=now.t+(g[x][y]=='.'?1:g[x][y]-'0'+1); if (x==n-1 && y==m-1) //到达 return cur; q.push(que[cur++]); vis[x][y]=true; } } } return -1;}void Output (int id){ int pre=que[id].pre; int x=que[id].x; int y=que[id].y; int px=que[pre].x; int py=que[pre].y; if (que[id].pre!=0) Output(pre); printf("%ds:(%d,%d)->(%d,%d)\n",que[pre].t+1,px,py,x,y); if (g[x][y]!='.') { int t=g[x][y]-'0'; for (int i=1;i<=t;i++) printf("%ds:FIGHT AT (%d,%d)\n",que[pre].t+i+1,x,y); }}int main (){ while (~scanf("%d%d",&n,&m)) { for (int i=0;i<n;i++) scanf("%s",g[i]); memset(vis,false,sizeof(vis)); int id=BFS(); if (id!=-1) { printf("It takes %d seconds to reach the target position, let me show you the way.\n",que[id].t); Output(id); printf("FINISH\n"); } else printf("God please help our poor hero.\nFINISH\n"); } return 0;}
0 0
- Hdu 1026 Ignatius and the Princess I (BFS 优先队列+路径输出)
- Ignatius and the Princess I (hdu 1026 优先队列+bfs+输出路径)
- HDU 1026 Ignatius and the Princess I【BFS+优先队列+栈路径输出】
- hdu 1026 Ignatius and the Princess I(优先队列+BFS)
- hdu 1026 Ignatius and the Princess I 优先队列 + bfs
- HDU 1026 Ignatius and the Princess I - BFS + 优先队列
- HDU 1026 Ignatius and the Princess I 优先队列 +BFS.
- HDU 1026 Ignatius and the Princess I(优先队列bfs)
- HDU 1026 Ignatius and the Princess I(BFS+优先队列)
- HDU 1026 Ignatius and the Princess I bfs优先队列
- HDU 1026 Ignatius and the Princess I BFS优先队列
- HDU 1026 Ignatius and the Princess I BFS 优先队列
- HDOJ 1026 Ignatius and the Princess I (BFS + 优先队列 + 输出路径)
- HDU--1026:Ignatius and the Princess I (BFS+优先队列+打印路径)
- HDU-#1026 Ignatius and the Princess I(BFS+优先队列+路径存储)
- HDU 1026 Ignatius and the Princess I 【BFS(优先队列)+路径存储】
- HDU ACM 1026 Ignatius and the Princess I -> BFS+优先队列+路径打印
- hdu 1026 Ignatius and the Princess I bfs 优先队列 路径记录
- HBase分布式数据库简介
- 铁轨
- I2C总线
- 数据结构 -- 单链表插入之不包含头节点
- Python+Ghost抓取动态网页图片,并模拟页面Get请求
- Hdu 1026 Ignatius and the Princess I (BFS 优先队列+路径输出)
- 断点续传的原理
- Hadoop集群_WordCount运行详解--MapReduce编程模型
- svn: Commit blocked by pre-commit hook (exit code 1) with output: Log format error 提交Log格式错误
- 向SharePoint页面中添加状态信息
- 11 Useful Code Snippets for PHP Developers
- ubuntu环境下重新编译内核
- 关于dp与px之间的转换
- [Leetcode] Recover Binary Search Tree (Java)