poj 3984 迷宫问题【dfs】
来源:互联网 发布:mac关闭后台程序快捷键 编辑:程序博客网 时间:2024/06/05 00:42
迷宫问题
Sample Input
0 1 0 0 00 1 0 1 00 0 0 0 00 1 1 1 00 0 0 1 0
Sample Output
(0, 0)(1, 0)(2, 0)(2, 1)(2, 2)(2, 3)(2, 4)(3, 4)(4, 4)
思路:遍历从(0,0)到(4,4)的走法,每走一步,就要更新到当前格子所需的最少的步数【为之后的输出做铺垫,笔者使用与map一样的的数组来存放 到对应的格子所需的最小步数】;完成遍历后,
①从(4,4)开始往回走,设到(4,4)最小步数为n,则(4,4)之前的格子所需最少步数为n-1,然后查找与(4,4)相邻的格子,查看他们所需的步数,找出n-1步的,
②然后从n-1不的那个格子开始,找出与它相邻的n-2的格子,
③重复上述操作即可。。。
已Accept【c++提交】
#include <cstdio>#include <cstring>//#include <cstdlib>using namespace std;int map[6][6];int dis[7][7];int n[18];int m[18];int dx[4] = {0,0,1,-1};int dy[4] = {1,-1,0,0};void dfs(int sc, int sr, int total) {if(sc < 1 || sc > 5 || sr < 1 || sr > 5)//判断是否越界 return ;if(map[sc][sr] == 1)//判断是否是墙 return ;if(total + 1 >= dis[sc][sr])//若当前走到得格子原先到过且所需最小步数比现在 少,则不更新 ,反之则更新 return ;dis[sc][sr] = total + 1 ;map[sc][sr] = 1;for(int i = 0; i < 4; i++) {//当前格子的相邻的四个格子遍历过去 int nx = sc + dx[i];int ny = sr + dy[i];dfs(nx, ny, dis[sc][sr]);}map[sc][sr] = 0;return ;}void Scanf_a() {//输入 memset(dis, 0x3f, sizeof(dis));for(int i = 1; i < 6; i++)for(int j = 1; j < 6; j++)scanf("%d", &map[i][j]);}void Printf_a() {//输出 int x = 5, y = 5, q = 0;int current = dis[x][y];n[q] = x;m[q++] = y;while(x != 1 || y !=1) {for(int i = 0; i < 4; i++) {if(dis[x + dx[i]][y + dy[i]] == current - 1) {x = x + dx[i];y = y + dy[i];n[q] = x;m[q++] = y;current--;}}}for(int j = dis[5][5] - 1; j >= 0; j--)printf("(%d, %d)\n", n[j] - 1, m[j] - 1);}int main(){Scanf_a();dfs(1, 1, 0);Printf_a();//system("pause");return 0;}
0 0
- POJ 3984 迷宫问题 DFS
- poj 3984 迷宫问题【dfs】
- POJ 3984 迷宫问题 【DFS】
- POJ-3984迷宫问题dfs
- 迷宫问题 POJ dfs
- poj--3984 迷宫问题(dfs)
- POJ 3984 迷宫问题 (DFS)
- POJ - 3984 - 迷宫问题 (DFS)
- POJ 3984 迷宫问题 (DFS+BFS)
- poj 3984 迷宫问题【BFS && DFS】【简单】
- poj 3984 迷宫问题【dfs+路径输出】
- 【POJ】3984 迷宫问题 BFS 与 DFS
- POJ 3984 迷宫问题(dfs)
- poj dfs相关之3984 迷宫问题
- poj 3984 迷宫问题 dfs 求迷宫路径
- 迷宫问题(dfs)POJ
- 迷宫问题 poj 3984 搜索算法 宽搜+dfs回溯
- POJ 3984 迷宫问题 (DFS(深度优先搜索))
- VNC Viewer中启动qt5.5.0的qtcreator,报告QXcbConnection: Failed to initialize XRandr等,无法启动
- 待研究
- MySQL中有两种方法可以删除数据
- Eclipse中每行80字符限制的提示线
- 算法题:两个栈实现一个队列
- poj 3984 迷宫问题【dfs】
- HDU5072(容斥)
- C++中cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
- 如何在代码中取得一个特点type的关节
- 多次惨痛教训告诉我, 以时间戳获取数据必须设一个提前量
- 阿里云centOS配置lamp环境
- 【SSH快速进阶】——探索Hibernate对象的三种状态:Transient、Persistent、Detached
- 算法:链表
- 你可以选择放弃,永远别放弃选择