用递归思想解迷宫问题
来源:互联网 发布:淘宝禁售规则 编辑:程序博客网 时间:2024/04/29 04:45
#include <stdio.h>
#define MAX_ROW 8
#define MAX_COL 8
int flag = 0;
typedef struct point
{
int row;
int col;
}Point;
int maze[MAX_ROW][MAX_COL] = {
0, 1, 0, 1, 0, 0, 1, 0,
0, 0, 1, 0, 1, 0, 0, 1,
0, 0, 0, 0, 1, 1, 1, 1,
1, 0, 1, 0, 1, 0, 0, 1,
1, 0, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 1, 1, 1,
1, 1, 1, 1, 0, 0, 0, 0,
};
Point FootPrints[MAX_ROW][MAX_COL] = {
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
};
void Print_Maze(void)
{
int i = 0, j = 0;
for(i = 0; i < MAX_ROW; i++)
{
for(j = 0; j < MAX_COL; j++)
{
printf("%2d", maze[i][j]);
}
printf("\n");
}
printf("\n***************************************** \n");
}
void Print_Foots(void)
{
Point p = {7, 7};
while(FootPrints[p.row][p.col].row != -1)/*FootPrints[p.row][p.col]记录p点的上一个点的坐标*/
{
printf("{%d, %d} \n", p.row, p.col);/*倒序打印走出 迷宫的每一个点坐标*/
p = FootPrints[p.row][p.col];
printf("{%d, %d} \n", p.row, p.col);/*倒序打印走出 迷宫的每一个点坐标*/
}
printf("seccess out ! \n");
}
void Print(void)
{
int i = 0, j = 0;
for(i = 0; i < MAX_ROW; i++)
{
for(j = 0; j < MAX_COL; j++)
{
printf("{%d, %d},", FootPrints[i][j].row, FootPrints[i][j].col);
}
printf("\n");
}
printf("\n");
}
void VisitPoint(Point p)
{
Print_Maze();/*打印迷宫*/
if((p.row+1 == MAX_ROW) && (p.col+1 == MAX_COL))
{
printf("it's go out ! \n");
flag = 1;
return;
}
if((p.row+1 < MAX_ROW) && (maze[p.row+1][p.col] == 0))
{
Point tempPoint = {p.row+1, p.col};
maze[p.row+1][p.col] = 2;
FootPrints[p.row+1][p.col] = p;
VisitPoint(tempPoint);
}
if((p.col+1 < MAX_COL) && (maze[p.row][p.col+1] == 0))
{
Point tempPoint = {p.row, p.col+1};
maze[p.row][p.col+1] = 2;
FootPrints[p.row][p.col+1] = p;
VisitPoint(tempPoint);
}
if((p.row-1 >= 0) && (maze[p.row-1][p.col] == 0))
{
Point tempPoint = {p.row-1, p.col};
maze[p.row-1][p.col] = 2;
FootPrints[p.row-1][p.col] = p;
VisitPoint(tempPoint);
}
if((p.col-1 >= 0) && (maze[p.row][p.col-1] == 0))
{
Point tempPoint = {p.row, p.col-1};
maze[p.row][p.col-1] = 2;
FootPrints[p.row][p.col-1] = p;
VisitPoint(tempPoint);
}
return;
}
int main(void)
{
Point p = {0, 0};/*出发点*/
maze[p.row][p.col] = 2;/*访问过的点标记为2*/
VisitPoint(p);
if(flag)
{
Print_Foots();/*打印成功走出迷宫的路径*/
}
else
{
printf("No Path ! \n");
}
return 0;
}
#define MAX_ROW 8
#define MAX_COL 8
int flag = 0;
typedef struct point
{
int row;
int col;
}Point;
int maze[MAX_ROW][MAX_COL] = {
0, 1, 0, 1, 0, 0, 1, 0,
0, 0, 1, 0, 1, 0, 0, 1,
0, 0, 0, 0, 1, 1, 1, 1,
1, 0, 1, 0, 1, 0, 0, 1,
1, 0, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 1, 1, 1,
1, 1, 1, 1, 0, 0, 0, 0,
};
Point FootPrints[MAX_ROW][MAX_COL] = {
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}},
};
void Print_Maze(void)
{
int i = 0, j = 0;
for(i = 0; i < MAX_ROW; i++)
{
for(j = 0; j < MAX_COL; j++)
{
printf("%2d", maze[i][j]);
}
printf("\n");
}
printf("\n***************************************** \n");
}
void Print_Foots(void)
{
Point p = {7, 7};
while(FootPrints[p.row][p.col].row != -1)/*FootPrints[p.row][p.col]记录p点的上一个点的坐标*/
{
printf("{%d, %d} \n", p.row, p.col);/*倒序打印走出 迷宫的每一个点坐标*/
p = FootPrints[p.row][p.col];
printf("{%d, %d} \n", p.row, p.col);/*倒序打印走出 迷宫的每一个点坐标*/
}
printf("seccess out ! \n");
}
void Print(void)
{
int i = 0, j = 0;
for(i = 0; i < MAX_ROW; i++)
{
for(j = 0; j < MAX_COL; j++)
{
printf("{%d, %d},", FootPrints[i][j].row, FootPrints[i][j].col);
}
printf("\n");
}
printf("\n");
}
void VisitPoint(Point p)
{
Print_Maze();/*打印迷宫*/
if((p.row+1 == MAX_ROW) && (p.col+1 == MAX_COL))
{
printf("it's go out ! \n");
flag = 1;
return;
}
if((p.row+1 < MAX_ROW) && (maze[p.row+1][p.col] == 0))
{
Point tempPoint = {p.row+1, p.col};
maze[p.row+1][p.col] = 2;
FootPrints[p.row+1][p.col] = p;
VisitPoint(tempPoint);
}
if((p.col+1 < MAX_COL) && (maze[p.row][p.col+1] == 0))
{
Point tempPoint = {p.row, p.col+1};
maze[p.row][p.col+1] = 2;
FootPrints[p.row][p.col+1] = p;
VisitPoint(tempPoint);
}
if((p.row-1 >= 0) && (maze[p.row-1][p.col] == 0))
{
Point tempPoint = {p.row-1, p.col};
maze[p.row-1][p.col] = 2;
FootPrints[p.row-1][p.col] = p;
VisitPoint(tempPoint);
}
if((p.col-1 >= 0) && (maze[p.row][p.col-1] == 0))
{
Point tempPoint = {p.row, p.col-1};
maze[p.row][p.col-1] = 2;
FootPrints[p.row][p.col-1] = p;
VisitPoint(tempPoint);
}
return;
}
int main(void)
{
Point p = {0, 0};/*出发点*/
maze[p.row][p.col] = 2;/*访问过的点标记为2*/
VisitPoint(p);
if(flag)
{
Print_Foots();/*打印成功走出迷宫的路径*/
}
else
{
printf("No Path ! \n");
}
return 0;
}
0 0
- 用递归思想解迷宫问题
- 迷宫问题递归解
- 迷宫问题的非递归解
- Java 递归算法解迷宫问题
- 用栈和递归求解迷宫问题
- 用循环和递归处理迷宫问题
- 用栈和递归解决迷宫问题
- 迷宫问题递归算法
- 递归:: 迷宫问题
- 递归求解迷宫问题
- 迷宫问题递归求解
- 迷宫问题(递归)
- 用递归思想解决河内塔问题
- 递归思想:汉诺塔问题
- 递归法求解迷宫问题
- 迷宫问题的递归求解
- 迷宫问题(栈,递归,队列)
- 老鼠走迷宫----------递归问题
- CMake编写大纲
- 用mysql存储二进制数据流
- 公司发展慢不等于慢公司
- Undefined attribute name (xmlns)
- 如何实现一个与数据库表字段松耦合的j2ee应用
- 用递归思想解迷宫问题
- 2013年总结(4)-人脉
- MySQL存储过程详解
- xhtml学习总结
- ubuntu13.10安装fcitx
- POKER HANDS题解
- 通过限制循环次数来避免死循环
- poj1028Web Navigation(stack)
- Linux 进程状态