HDU 1026 Ignatius and the Princess I (bfs + 优先队列 + 路径记录)
来源:互联网 发布:mac强制关机 编辑:程序博客网 时间:2024/05/02 05:02
HDU 1026
通过记录前驱来记录路径,不用担心所记录的路径不是用时最少的路径,因为优先队列每次出队的都是用时最少的元素,所以当不合适的元素出队时,正确的路径早已被标记完了。 另外,(0, 0)点确实是没有怪兽的。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;char map[110][110];int vis[110][110];int dir[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};int N, M;struct node {int x, y;int time;friend bool operator < (node a, node b) {return a.time > b.time;}};int pre[110][110];int t;int bfs() {int i, j;priority_queue<node> q;node now, next;now.x = 0, now.y = 0, now.time = 0;q.push(now);while(!q.empty()) {now = q.top();q.pop();if(now.x == N - 1 && now.y == M - 1) {return now.time;}vis[now.x][now.y] = 1;for(i = 0; i < 4; i++) {next.x = now.x + dir[i][0];next.y = now.y + dir[i][1];if(next.x >= 0 && next.x <= N - 1 && next.y >= 0 && next.y <= M - 1) {if(vis[next.x][next.y] == 0 && map[next.x][next.y] != 'X') {if(map[next.x][next.y] == '.') {next.time = now.time + 1;}else if(map[next.x][next.y] >= '1' && map[next.x][next.y] <= '9') {next.time = now.time + map[next.x][next.y] - '0' + 1;}vis[next.x][next.y] = 1;pre[next.x][next.y] = i;q.push(next);}}}}return -1;}void path(int x, int y) {if(x == 0 && y == 0) {return;}int tx = x - dir[pre[x][y]][0];int ty = y - dir[pre[x][y]][1];path(tx, ty);printf("%ds:(%d,%d)->(%d,%d)\n", t++, tx, ty, x, y);if(map[x][y] >= '1' && map[x][y] <= '9') {int i;for(i = 1; i <= map[x][y] - '0'; i++) {printf("%ds:FIGHT AT (%d,%d)\n", t++, x, y);}}}int main() {while(~scanf("%d %d", &N, &M)) {getchar();int i, j;for(i = 0; i < N; i++) {for(j = 0; j < M; j++) {scanf("%c", &map[i][j]);}getchar();}memset(vis, 0, sizeof(vis));memset(pre, 0, sizeof(pre));int step = bfs();if(step != -1) {printf("It takes %d seconds to reach the target position, let me show you the way.\n", step);t = 1;path(N - 1, M - 1);}else {printf("God please help our poor hero.\n");}printf("FINISH\n");} return 0;}
0 0
- 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 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 + 优先队列 + stack路径记录】 【经典题目】
- Hdu 1026 Ignatius and the Princess I (BFS 优先队列+路径输出)
- MVVM Light中EventToCommand的使用
- iOS开发UI篇—核心动画(转场动画和组动画)
- hdu 2553 N皇后问题(dfs)
- 跟着柴毛毛学Spring(3)——简化Bean的配置
- iOS开发UI篇—核心动画(UIView封装动画)
- HDU 1026 Ignatius and the Princess I (bfs + 优先队列 + 路径记录)
- iOS开发UI篇—核心动画简介
- ASP.NET网络编程学习第二步:简单理解ASP.NET
- 基于webp格式的图片转换问题与port的
- VE设计#4 表现层updatePanel界面
- ubtunt下的apt命令
- iOS开发UI篇—CAlayer(自定义layer)
- max double slice sum
- POJ2485 Highways(最小生成树)