HDOJ HDU 1026 Ignatius and the Princess I
2017-09-06 09:30
387 查看
HDOJ 1026 Ignatius and the Princess I
本题是第一部分
走迷宫 大小 n * m
迷宫构成
起点 (0, 0)
终点 (n, m)
‘.’ 空白位 可以走
‘X’ 墙 不能行走
‘数字 a’ 怪兽 Ignatius 需要在此花费 a 时间
但是 此题不同在于打怪兽需要花费时间 节点不能同步进行
所以每一步需要记录时间信息 用优先队列决定最先需要移动的点
倒序bfs比较方便顺序输出过程
题目
点此查看 HDOJ 1026 Ignatius and the Princess I分类
bfs题意
英雄 Ignatius 救 公主 的故事背景本题是第一部分
走迷宫 大小 n * m
迷宫构成
起点 (0, 0)
终点 (n, m)
‘.’ 空白位 可以走
‘X’ 墙 不能行走
‘数字 a’ 怪兽 Ignatius 需要在此花费 a 时间
题解
bfs求最短路 并输出过程但是 此题不同在于打怪兽需要花费时间 节点不能同步进行
所以每一步需要记录时间信息 用优先队列决定最先需要移动的点
技巧
题目需要输出过程倒序bfs比较方便顺序输出过程
代码
#include <iostream> #include <cstring> #include <queue> #define max 110 using namespace std; int dir[5][2] = {0,0,-1,0,1,0,0,-1,0,1}; char map[max][max]; int vis[max][max]; //结点结构体 struct pst { int x; int y; int t; pst() { x = 0; y = 0; t = 0; } pst(int nx,int ny,int nt) { x = nx; y = ny; t = nt; } pst(const pst & b) { x = b.x; y = b.y; t = b.t; } bool operator < (const pst & b) const { if(t > b.t) return true; else return false; } //重载<用于优先队列 pst & operator = (const pst & b) { x = b.x; y = b.y; t = b.t; return * this; } }; int main() { int m,n,nx,ny,rst,tms; priority_queue<pst> q; pst pn,lst; int stx,sty,ltx,lty; while(cin >> n >> m) { rst = 0; for(int i = 0;i < n;i++) { for(int j = 0;j < m;j++) cin >> map[i][j]; } memset(vis,0,sizeof(vis)); pst start(n-1,m-1,0); vis[n-1][m-1] = 6; q.push(start); while(!q.empty()) { lst = q.top(); // cout << "lst " << lst.x << " " << lst.y << " " << lst.t << " rst " << rst << endl; if(lst.x == 0 && lst.y == 0) { rst = lst.t; q.pop(); break; } q.pop(); for(int i = 1;i < 5;i++) { pn.x = lst.x + dir[i][0]; pn.y = lst.y + dir[i][1]; if(pn.x < 0 || pn.y < 0 || pn.x > n - 1|| pn.y > m - 1) continue; if(vis[pn.x][pn.y] || map[pn.x][pn.y] == 'X') continue; // cout <<"nxy " << pn.x << " " << pn.y << endl; vis[pn.x][pn.y] = i; // cout <<"vis " << vis[pn.x][pn.y] << endl; //记录上一节点的信息 map[pn.x][pn.y] == '.' ? pn.t = lst.t + 1 : pn.t = lst.t + (map cc68 [pn.x][pn.y] - '0') + 1; // '.' : 时间 + 1 | 数字 : 时间 + 数字 q.push(pn); } } // cout << "rst " << rst << endl; if(rst) { if(map[n-1][m-1] != '.' && map[n-1][m-1] != 'X' ) rst += (map[n-1][m-1] - '0'); cout << "It takes " << rst << " seconds to reach the target position, let me show you the way." << endl; stx = 0; sty = 0; ltx = 0; lty = 0; tms = 1; if(map[0][0] != '.' || map[0][0] != 'X' ) for(int i = 0;i < (map[0][0] - '0');i++) cout << tms++ << "s:FIGHT AT (0,0)" << endl; while(stx != n-1 || sty != m-1) { // cout << "st " << stx << " " << sty << endl; switch(vis[stx][sty]) { case 1 : stx++; break; case 2 : stx--; break; case 3 : sty++; break; case 4 : sty--; break; } cout << tms++ << "s:(" << ltx << "," << lty << ")->(" << stx << "," << sty << ")" << endl; if(map[sty][sty] != '.') for(int i = 0;i < (map[stx][sty] - '0');i++) cout << tms++ << "s:FIGHT AT (" << stx << "," << sty << ")" << endl; ltx = stx; lty = sty; } cout << "FINISH" << endl; } else cout << "God please help our poor hero." << endl << "FINISH" << endl; while(!q.empty()) { q.pop(); } } return 0; }
相关文章推荐
- 【HDU】 1026 Ignatius and the Princess I
- 杭电 hdu 1026 Ignatius and the Princess I(BFS+优先队列+墨迹人的输出)
- [置顶] HDOJ 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 广度优先搜索
- HDU 1026 Ignatius and the Princess I(BFS+优先队列)
- HDU 1026 Ignatius and the Princess I
- HDU 1026 Ignatius and the Princess I
- hdu1026——Ignatius and the Princess I()
- 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 (搜索-广度优先搜索)
- hdu1026 Ignatius and the Princess I(广搜+记录路径)
- HDU 1026 Ignatius and the Princess I
- hdu 1026 Ignatius and the Princess I(广搜+优先队列)
- HDU 1026 Ignatius and the Princess I
- HDOJ 1026 Ignatius and the Princess I
- HDU 1026 Ignatius and the Princess I