走迷宫值路径记录
来源:互联网 发布:网络代理合同范本 编辑:程序博客网 时间:2024/05/16 14:58
利用bfs走迷宫是最基础的方法,记录访问过的每个点然后回溯最短路径的点
首先把问题简单化一下,只有墙壁和路,代码比较简单,只要会bfs基本上看得懂
// 走迷宫记录路径// 概述:有一个N*M大小的迷宫,其中'#'代表墙壁无法通过,'.'代表路,每走一步都将花费1s的时间,求从起点(0,0)到终点(N-1,M-1)的最短路径,并将路径打印#include <iostream>#include <queue>#include <cstring>using namespace std;const int INF = 1000005;const int MAX_N = 105;struct Point{ int x; int y;}first, next,pre[MAX_N][MAX_N];char pic[MAX_N][MAX_N];int N, M;int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};int value[MAX_N][MAX_N];bool vis[MAX_N][MAX_N];int bfs(){ queue<Point> que; que.push(first); while(que.size()) { first = que.front(); que.pop(); if(first.x == N - 1 && first.y == M -1) return value[N - 1][M - 1]; for(int i = 0; i < 4; i++) { next.x = first.x + dx[i]; next.y = first.y + dy[i]; if(next.x >= 0 && next.x < N && next.y >= 0 && next.y < M && pic[next.x][next.y] != '#' && vis[next.x][next.y]) { value[next.x][next.y] = value[first.x][first.y] + 1; pre[next.x][next.y].x = first.x; pre[next.x][next.y].y = first.y; vis[next.x][next.y] = false; que.push(next); } } } return INF;}void print_pre(int i, int j){ if(pre[i][j].x == -1) return; print_pre(pre[i][j].x, pre[i][j].y); printf("%ds:(%d,%d)->(%d,%d)\n",value[i][j], pre[i][j].x, pre[i][j].y, i, j);}int main(){ while(cin >> N >> M) { for(int i = 0; i < N; i++) cin >> pic[i]; first.x = 0; first.y = 0; memset(vis, true, sizeof(vis)); // true表示该点未访问,false表示该点已访问 vis[0][0] = false; pre[0][0].x = pre[0][0].y = -1; int ans = bfs(); if(ans == INF) printf("sorry,can't out!\n"); else { printf("It takes %d seconds to reach the target position, let me show you the way.\n", ans); print_pre(N - 1, M - 1); } } return 0;}
hangd1026题就是这种类型的题目,只是多了一个条件,路上可能会有怪物,每个怪物有N点生命值(1<=n<=9),要花费n秒杀死这个怪物。
题目链接
#include <iostream>#include <queue>#include <cstring>using namespace std;const int INF = 100005;const int MAX_N = 105;struct Node{ int x; int y; int num; bool operator<(const Node &a) const { return num > a.num; }}first, nextNode;struct Point{ int x; int y;}pre[MAX_N][MAX_N];char pic[MAX_N][MAX_N];int value[MAX_N][MAX_N];bool vis[MAX_N][MAX_N];int N, M;int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};int bfs(){ priority_queue<Node> pque; pque.push(first); while(pque.size()) { first = pque.top(); pque.pop(); if(first.x == N - 1 && first.y == M - 1) return value[N - 1][M - 1]; for(int i = 0; i < 4; i++) { nextNode.x = first.x + dx[i]; nextNode.y = first.y + dy[i]; if(nextNode.x >= 0 && nextNode.x < N && nextNode.y >= 0 && nextNode.y < M && pic[nextNode.x][nextNode.y] != 'X' && vis[nextNode.x][nextNode.y]) { if(pic[nextNode.x][nextNode.y] == '.') { nextNode.num = first.num + 1; } else if(isdigit(pic[nextNode.x][nextNode.y])) { nextNode.num = first.num + 1 + (int)(pic[nextNode.x][nextNode.y] - '0'); } pre[nextNode.x][nextNode.y].x = first.x; pre[nextNode.x][nextNode.y].y = first.y; value[nextNode.x][nextNode.y] = nextNode.num; vis[nextNode.x][nextNode.y] = false; pque.push(nextNode); } } } return INF;}void print_pre(int i, int j){ if(pre[i][j].x == -1) return; print_pre(pre[i][j].x, pre[i][j].y); if(pic[i][j] == '.') printf("%ds:(%d,%d)->(%d,%d)\n",value[i][j], pre[i][j].x, pre[i][j].y, i, j); else { int t = pic[i][j] - '0'; int kase = value[pre[i][j].x][pre[i][j].y]; printf("%ds:(%d,%d)->(%d,%d)\n",++kase, pre[i][j].x, pre[i][j].y, i, j); for(int k = 0; k < t; k++) { printf("%ds:FIGHT AT (%d,%d)\n", ++kase, i, j); } }}int main(){ while(cin >> N >> M) { for(int i = 0; i < N; i++) cin >> pic[i]; memset(vis, true, sizeof(vis)); first.x = 0; first.y = 0; first.num = 0; value[0][0] = 0; vis[0][0] = false; pre[0][0].x = pre[0][0].y = -1; int ans = bfs(); if(ans != INF) { printf("It takes %d seconds to reach the target position, let me show you the way.\n", ans); print_pre(N - 1, M - 1); } else printf("God please help our poor hero.\n"); printf("FINISH\n"); } return 0;}
1 0
- 走迷宫值路径记录
- 广搜 基础 走迷宫 记录路径
- 自动走迷宫_记录走的路径
- 走迷宫(广搜路径记录(路径唯一))
- 走迷宫--图的搜索(bfs)并记录路径
- 走迷宫记录所走最短路径的步数(最容易理解的)
- bfs走迷宫记录最短路径(求大神指…
- SDUT 1269走迷宫(DFS+打印路径)
- 老鼠走迷宫问题,求出所有路径
- 【java】走迷宫最短路径
- DFS走迷宫问题(非最短路径)
- SDUT1269走迷宫(记录轨迹)
- 走迷宫
- 走迷宫
- 走迷宫
- 走迷宫
- 走迷宫
- 走迷宫
- 【MySQL】插入数据 创建和操纵表
- 设计模式 —— 工厂模式
- [POJ]1001 乘方计算
- RPL UDP simulated by Cooja
- 如果设计中使用了非标准的字体,你该如何去实现?
- 走迷宫值路径记录
- ”WinForm上位机+OV7670摄像头+STM32+蓝牙“图像采集系统(二)PC-MCU蓝牙通信及WinForm上位机开发
- A标签使用javascript:伪协议
- IOS开发UI篇--使用CAShapeLayer实现复杂的View的遮罩效果
- heatmap.js 一个用canvas画热力图的利器
- 【MySQL】安全管理 数据库维护 改善性能
- hdu 5249 KPI
- git 下载安装和相关介绍
- EasyMusicPlayer Privacy policy