迷宫BFS+递归回溯找爹打印路径
来源:互联网 发布:管家婆普及版数据恢复 编辑:程序博客网 时间:2024/06/05 15:09
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include<queue>
using namespace std;
int ok=0;
int k;
int f[10][10];
struct node
{
int x,y;
};
node trackk[50][50];
queue <node> qq;
node point,father;
bool cmp(int a,int b)
{
return (a>b);
}
int legal(int r, int c)
{
if ( r>=1 && r<=5 && c>=1 && c<=5 && f[r][c]==0)
return 1;
return 0;
}
void bfs()
{
while ( !qq.empty() )
{
father=point=qq.front();
//printf("%d, %d\n",point.x,point.y);
if (point.x==5 && point.y==5) { return; }
f[point.x][point.y]=1;
if ( legal(point.x-1,point.y) )
{point.x--; qq.push(point);trackk[point.x][point.y]=father; //要输出位置X,Y 但是在位置x,y保存了他的父亲,用来回溯
point.x++;}
if ( legal(point.x+1,point.y) )
{point.x++; qq.push(point); trackk[point.x][point.y]=father; point.x--;}
if ( legal(point.x,point.y-1) )
{point.y--; qq.push(point); trackk[point.x][point.y]=father; point.y++;}
if ( legal(point.x,point.y+1) )
{point.y++; qq.push(point); trackk[point.x][point.y]=father; point.y--;}
qq.pop();
}
return ;
}
void output(node a)
{
if (a.x==1&&a.y==1) printf("(0, 0)\n");
else
{
output(trackk[a.x][a.y]);
printf("(%d, %d)\n",a.x-1,a.y-1);
}
}
int main()
{
int i,j;
for (i=1;i<=5;i++)
{
for (j=1;j<=5;j++)
{
scanf("%d",&f[i][j]);
}
}
f[1][1]=1;
point.x=point.y=1;
qq.push(point);
bfs();
node a;
a.x=a.y=5;
output(a);
return 0;
}
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include<queue>
using namespace std;
int ok=0;
int k;
int f[10][10];
struct node
{
int x,y;
};
node trackk[50][50];
queue <node> qq;
node point,father;
bool cmp(int a,int b)
{
return (a>b);
}
int legal(int r, int c)
{
if ( r>=1 && r<=5 && c>=1 && c<=5 && f[r][c]==0)
return 1;
return 0;
}
void bfs()
{
while ( !qq.empty() )
{
father=point=qq.front();
//printf("%d, %d\n",point.x,point.y);
if (point.x==5 && point.y==5) { return; }
f[point.x][point.y]=1;
if ( legal(point.x-1,point.y) )
{point.x--; qq.push(point);trackk[point.x][point.y]=father; //要输出位置X,Y 但是在位置x,y保存了他的父亲,用来回溯
point.x++;}
if ( legal(point.x+1,point.y) )
{point.x++; qq.push(point); trackk[point.x][point.y]=father; point.x--;}
if ( legal(point.x,point.y-1) )
{point.y--; qq.push(point); trackk[point.x][point.y]=father; point.y++;}
if ( legal(point.x,point.y+1) )
{point.y++; qq.push(point); trackk[point.x][point.y]=father; point.y--;}
qq.pop();
}
return ;
}
void output(node a)
{
if (a.x==1&&a.y==1) printf("(0, 0)\n");
else
{
output(trackk[a.x][a.y]);
printf("(%d, %d)\n",a.x-1,a.y-1);
}
}
int main()
{
int i,j;
for (i=1;i<=5;i++)
{
for (j=1;j<=5;j++)
{
scanf("%d",&f[i][j]);
}
}
f[1][1]=1;
point.x=point.y=1;
qq.push(point);
bfs();
node a;
a.x=a.y=5;
output(a);
return 0;
}
0 0
- 迷宫BFS+递归回溯找爹打印路径
- 迷宫问题bfs+打印路径
- bfs迷宫问题--打印路径
- 回溯法找迷宫最短路径
- poj3984 迷宫问题 简单bfs打印路径
- POJ 3984-迷宫问题(BFS+回溯路径)
- FOJ 1205 小鼠迷宫问题【BFS+dfs回溯查找路径】
- 回溯法-bfs--迷宫问题的最短路径
- 迷宫(回溯,递归)
- POJ 3984 迷宫问题(bfs搜索,递归打印)
- POJ 3984 迷宫问题(bfs+打印路径)
- BFS解迷宫问题,并打印最短路径
- poj 3984迷宫问题(bfs加打印路径)
- POJ-3984-迷宫问题(BFS打印路径)
- POJ 3984:迷宫问题 bfs+递归输出路径
- 搜索练习3 /poj.org/problem 3984 迷宫问题/bfs 回溯找路经
- 用BFS找最短路,并打印路径
- BFS + 回溯路径 POJ
- F - Catch That Cow-BFS-只计次不求路径
- G - Dungeon MasterBFS+填路
- java IO详解看完你就懂了耐心点。
- Node入门之一
- UITableableview上拉下拉刷新
- 迷宫BFS+递归回溯找爹打印路径
- hihoCoder 二分·二分查找之k小数
- HDU 4114 Disney's FastPass(状压dp)
- 迷宫DFS
- 路由事件初级案例
- poj 3080 Blue Jeans (多串最长公共子串 暴力)
- IIS7.5 错误代码0x8007007e HTTP 错误 500.19
- Androidapi.JNI.Net.Wifi
- 容器第八课,数据存储综合练习,javabean的介绍