POJ-3984迷宫问题dfs
来源:互联网 发布:爱奇艺自制网络剧破案 编辑:程序博客网 时间:2024/06/05 00:11
5*5的大小,不用担心会超时。
用了两遍DFS,第一遍找出最短路径,第二遍记录节点
#include<iostream>#include<cstdio>#include<algorithm>#include<queue>#include<stack>#include<cstring>#include<string>using namespace std;int minn;int t = 0;int a[10][10],b[10][10];int dx[4] = {0,0,1,-1},dy[4] = {1,-1,0,0};struct node{int px,py;}p[100];int is_out(int x,int y){if(x >= 0 && x < 5 && y >= 0 && y < 5) return 1;return 0;}void dfs1(int x,int y,int sum){if(x == 4 && y == 4){minn = min (sum,minn);return;}for(int i = 0;i < 4;i++){int xx = x + dx[i],yy = y + dy[i];if(is_out(xx,yy) && a[xx][yy] == 0 && b[xx][yy] == 0){b[xx][yy] = 1;dfs1(xx,yy,sum + 1);b[xx][yy] = 0;}}}void dfs2(int x,int y,int sum){if(x == 4 && y == 4){if(sum == minn && t == 0){t = 1;for(int i = 0;i < sum;i++){printf("(%d, %d)\n",p[i].px,p[i].py);}}return;}for(int i = 0;i < 4;i++){int xx = x + dx[i],yy = y + dy[i];if(is_out(xx,yy) && a[xx][yy] == 0 && b[xx][yy] == 0){b[xx][yy] = 1;p[sum].px = xx,p[sum].py = yy;dfs2(xx,yy,sum + 1);b[xx][yy] = 0;}}}int main(){minn = 100;for(int i = 0;i < 5;i++){for(int j = 0;j < 5;j++) scanf("%d",&a[i][j]);}memset(b,0,sizeof(b));dfs1(0,0,1);p[0].px = 0,p[0].py = 0;dfs2(0,0,1);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(深度优先搜索))
- form 表单字段 autocomplete 设置为off 后仍然无效的解决方法
- 离群点检测方法
- LeetCode
- 接口和形式参数
- js基础
- POJ-3984迷宫问题dfs
- gulp教程 前端工作环境的搭建
- Java反射生成对象注入spring(结合工厂模式)
- 变态跳台阶
- python中set(集合)类型的操作
- lintcode刷题系列:链表排序----归并排序 递归
- Java第一个设计模式----单例设计模式
- ABAP实例:如何设计和使用自定义的权限对象
- 原来python开发后端服务器还可以这么简单(未完成)