/*
Name: 迷宫问题
Copyright:
Author:
Date: 12/11/14 15:32
Description:
分别实现了广度优先搜索和深度优先搜索最短路径算法,还给出了深度优先搜索路径(非最短路径)的递归和非递归两种算法。
地图的设计可选择机器随机设计和人工设计,还可以进行人工修改地图。
*/
#include<stdio.h>
#include<stdlib.h>
#define false 0
#define true 1
#define M 15 //最大行数
#define N 15 //最大列数
#define MAXLEN 99999 //最长路径
enum position {OPEN, CLOSE, PASSED, ROAD=-1}; //分别表示该点通,不通,已走和属于所选路径
struct stype{//存储每个路径点的有关数据;横坐标,纵坐标和其前驱在数组中的位置
int x;
int y;
int pre;//在广度搜索中用来指向前驱,在深度搜索中用来表明目前所指的方向
} way[M*N];
int zx[4] = {0, 1, 0, -1};//存储东南西北四个方向所对应的x的值
int zy[4] = {1, 0, -1, 0};//存储东南西北四个方向所对应的y的值
int begin[2];//存储入口坐标
int end[2]; //存储出口坐标
int map[M+2][N+2];//存储迷宫地图
int c_map[M+2][N+2];//存储迷宫地图拷贝
int minLen = MAXLEN; //最短路径
void GetBegin();//创建入口坐标
void GetEnd();//创建出口坐标
int IsBegin(int x, int y);//判断该点是否为入口
int IsEnd(int x, int y);//判断该点是否为出口
void BuildMiGong(); //建立迷宫地图
void PerfectMiGong();//修正迷宫地图
void CopyMiGong();//拷贝迷宫地图
void PrintMiGong();//输出迷宫地图
void PrintWay(); //输出运动路径
int ExtentSearchWay();//寻找路径:广度搜索
int DeepSearchWay();//寻找路径:深度搜索
int Search(int x, int y);//深度搜索递归子函数
int DeepSearchWay_2();//寻找路径:深度搜索
void PutStack(int top); //把栈路径显示到迷宫中
void PutQueue(int rear); //把队列路径显示到迷宫中
int DeepSearchWay_3();//寻找路径:深度搜索(最短路径)
int main()
{
char choice;
do
{
BuildMiGong();
PrintMiGong();
puts("对迷宫地图还满意吗?选择此迷宫输入y,重新选择迷宫输入n, 修改迷宫输入x");
do
{
scanf("%c", &choice);
} while (choice != 'y' && choice != 'n' && choice != 'x');
} while (choice == 'n');
if (choice == 'x') //修正迷宫地图
PerfectMiGong();
GetBegin(); //创建入口坐标
GetEnd(); //创建出口坐标
puts("广度搜索:");
if (ExtentSearchWay()) //如果找到迷宫路径
PrintWay(); //输出路径
else
puts("此路不通!");
puts("深度搜索1:");
if (DeepSearchWay()) //如果找到迷宫路径
PrintWay(); //输出路径
else
puts("此路不通!");
puts("深度搜索2:");
if (DeepSearchWay_2()) //如果找到迷宫路径
PrintWay(); //输出路径
else
puts("此路不通!");
puts("深度搜索3:");
if (DeepSearchWay_3()) //如果找到迷宫路径
PrintWay(); //输出路径
else
puts("此路不通!");
return 0;
}
void GetBegin()//创建入口坐标
{
do
{
puts("请输入入口坐标:");
scanf("%d%d", &begin[0], &begin[1]);
} while (begin[0]<1 || begin[0]>M || begin[1]<1 || begin[1]>N);
}
void GetEnd()//创建出口坐标
{
do
{
puts("请输入出口坐标:");
scanf("%d%d", &end[0], &end[1]);
} while (end[0]<1 || end[0]>M || end[1]<1 || end[1]>N);
}
int IsBegin(int x, int y)//判断该点是否为入口
{
return (x==begin[0] && y==begin[1]) ? 1 : 0;
}
int IsEnd(int x, int y)//判断该点是否为出口
{
return (x==end[0] && y==end[1]) ? 1 : 0;
}
void BuildMiGong() //建立迷宫地图
{
int i, j;
puts("请选择建立迷宫的方法: 输入m表示人工建立,输入c表示计算机建立");
char choice;
do
{
scanf("%c", &choice);
} while (choice != 'm' && choice != 'c');
//设置边界
for (i=0; i<M+2; i++)
{
map[i][0] = map[i][N+1] = CLOSE;
}
for (j=1; j<=N; j++)
{
map[0][j] = map[M+1][j] = CLOSE;
}
if (choice == 'm')
{
puts("请输入迷宫地图,0表示通,1表示不通");
for (i=1; i<=M; i++)
for (j=1; j<=N; j++)
scanf("%d", &map[i][j]);
}
else
{
srand( (unsigned) time(NULL));
for (i=1; i<=M; i++)
for (j=1; j<=N; j++)
map[i][j] = rand() % 2;
}
}
void PerfectMiGong()//修正迷宫地图
{
int i, j;
char choice;
do
{
do
{
puts("请输入需要修改的坐标:(输入0,0结束)");
scanf("%d%d", &i, &j);
} while (i<0 || i>M || j<0 || j>N);
fflush(stdin);
if (i!=0 && j!=0)
{
do
{
puts("打开该点输入o,关闭该点输入c");
scanf("%c", &choice);
fflush(stdin);
} while (choice != 'c' && choice != 'o');
if (choice == 'o')
map[i][j] = OPEN;
else
map[i][j] = CLOSE;
PrintMiGong();
}
} while (i!=0 && j!=0);
}
void CopyMiGong()//拷贝迷宫地图
{
int i, j;
for (i=0; i<M+2; i++)
for (j=0; j<N+2; j++)
c_map[i][j] = map[i][j];
}
void PrintMiGong()//输出迷宫地图
{
int i, j;
for (i=0; i<M+2; i++)
{
for (j=0; j<N+2; j++)
{
if (i==0 || i==M+1)
printf("%02d", j);
else if (j==0 || j==N+1)
printf("%02d", i);
else if (OPEN == map[i][j])
printf(" ");
else
printf("##");
}
printf("\n");
}
}
void PrintWay() //输出运动路径
{
int i, j;
int count = 0;
for (i=0; i<M+2; i++)
{
for (j=0; j<N+2; j++)
{
if (i==0 || i==M+1)
printf("%02d", j);
else if (j==0 || j==N+1)
printf("%02d", i);
else if (ROAD == c_map[i][j])
{
count++;
printf("%c%c", 2, 2);
}
else if (PASSED == c_map[i][j])
printf("%c%c", 3, 3);
else if (OPEN == c_map[i][j])
printf(" ");
else
printf("##");
}
printf("\n");
}
printf("共%d步\n", count);
}
int ExtentSearchWay()//寻找路径:广度搜索
{
int i, j, x, y;
int front = 0; //队首
int rear = 0; //队尾
CopyMiGong();
way[0].x = begin[0];
way[0].y = begin[1];
c_map[way[0].x][way[0].y] = PASSED; //该点已走过
while (front<=rear)
{
for (i=0; i<4; i++)//判断当前路径点四周是否可通过
{
x = way[front].x + zx[i];
y = way[front].y + zy[i];
if (c_map[x][y] == OPEN)//如果某个方向可通过,将该点纳入队列
{
rear++;
way[rear].x = x;
way[rear].y = y;
way[rear].pre = front;
c_map[x][y] = PASSED;
}
if (IsEnd(x, y)) //找到出口
{
PutQueue(rear); //把队列路径显示到迷宫中
printf("\n广度优先搜索,总共搜索过%d个点\n", rear);
return true;
}
}
front++;
}
return false;
}
void PutQueue(int rear) //把队列路径显示到迷宫中
{
int i = rear;
CopyMiGong();
do
{
c_map[way[i].x][way[i].y] = ROAD;
i = way[i].pre;
} while (!IsBegin(way[i].x, way[i].y));
c_map[begin[0]][begin[1]] = ROAD;
}
int DeepSearchWay()//寻找路径:深度搜索
{
CopyMiGong();
if (c_map[begin[0]][begin[1]] == OPEN && Search(begin[0], begin[1]))
{
c_map[begin[0]][begin[1]] = ROAD;
return true;
}
return false;
}
int Search(int x, int y)//深度搜索递归子函数
{
int i;
c_map[x][y] = PASSED;
if (IsEnd(x, y)) //找到出口
{
c_map[x][y] = ROAD;
return true;
}
for (i=0; i<4; i++)//判断当前路径点四周是否可通过
{
if (c_map[x+zx[i]][y+zy[i]] == OPEN && Search(x+zx[i], y+zy[i]))
{
c_map[x][y] = ROAD;
return true;
}
}
return false;
}
int DeepSearchWay_2()//寻找路径:深度搜索
{
int x, y;
int top = 0; //栈顶指针
int sum = 0;//累积搜索过的点数量
CopyMiGong();
way[0].x = begin[0];
way[0].y = begin[1];
way[0].pre = 0;
c_map[way[0].x][way[0].y] = PASSED; //该点已走过
while (top >= 0)
{
if (way[top].pre < 4)
{
x = way[top].x + zx[way[top].pre];
y = way[top].y + zy[way[top].pre];
if (c_map[x][y] == OPEN)//如果某个方向可通过,将该点纳入栈
{
sum++;
top++;
way[top].x = x;
way[top].y = y;
way[top].pre = 0;
c_map[x][y] = PASSED;
if (IsEnd(x, y)) //找到出口
{
PutStack(top); //把栈路径显示到迷宫中
printf("\n深度优先搜索可行路径,总共搜索过%d个点\n", sum);
return true;
}
}
else //否则换个方向
way[top].pre++;
}
else
{
top--;
}
}
return false;
}
void PutStack(int top) //把栈路径显示到迷宫中
{
CopyMiGong();
while (top >= 0)
{
c_map[way[top].x][way[top].y] = ROAD;
top--;
}
}
int DeepSearchWay_3()//寻找路径:深度搜索(最短路径)
{
int x, y, i, j;
int top = 0; //栈顶指针
int pathLen[M+2][N+2] = {0};
struct stype shortWay[M*N];
int flag = false; //标记是否能到达终点
int sum = 0;//累积搜索过的点数量
for (x=0; x<M+2; x++) //设置各点初始路径长度均为最大值
for (y=0; y<N+2; y++)
pathLen[x][y] = MAXLEN;
CopyMiGong();
minLen = MAXLEN; //最短路径
way[0].x = begin[0];
way[0].y = begin[1];
way[0].pre = 0;
pathLen[begin[0]][begin[1]] = 0;
while (top >= 0)
{
if (way[top].pre < 4)
{
x = way[top].x + zx[way[top].pre];
y = way[top].y + zy[way[top].pre];
if (c_map[x][y] == OPEN && pathLen[x][y] > top+1)//如果某个方向可通过,且为最短路径,将该点纳入栈
{
sum++;
top++;
way[top].x = x;
way[top].y = y;
way[top].pre = 0;
pathLen[x][y] = top;
if (IsEnd(x, y)) //找到出口
{
if (top < minLen)
{
minLen = top;
for (i=0; i<=top; i++)
{
shortWay[i] = way[i];
}
}
flag = true;
top--;
}
}
else //否则换个方向
way[top].pre++;
}
else
{
top--;
}
}
if (flag)
{
for (i=0; i<=minLen; i++)
{
way[i] = shortWay[i];
}
PutStack(minLen); //把栈路径显示到迷宫中
}
printf("\n深度优先搜索最短路径,总共搜索过%d个点\n", sum);
return flag;
}
Name: 迷宫问题
Copyright:
Author:
Date: 12/11/14 15:32
Description:
分别实现了广度优先搜索和深度优先搜索最短路径算法,还给出了深度优先搜索路径(非最短路径)的递归和非递归两种算法。
地图的设计可选择机器随机设计和人工设计,还可以进行人工修改地图。
*/
#include<stdio.h>
#include<stdlib.h>
#define false 0
#define true 1
#define M 15 //最大行数
#define N 15 //最大列数
#define MAXLEN 99999 //最长路径
enum position {OPEN, CLOSE, PASSED, ROAD=-1}; //分别表示该点通,不通,已走和属于所选路径
struct stype{//存储每个路径点的有关数据;横坐标,纵坐标和其前驱在数组中的位置
int x;
int y;
int pre;//在广度搜索中用来指向前驱,在深度搜索中用来表明目前所指的方向
} way[M*N];
int zx[4] = {0, 1, 0, -1};//存储东南西北四个方向所对应的x的值
int zy[4] = {1, 0, -1, 0};//存储东南西北四个方向所对应的y的值
int begin[2];//存储入口坐标
int end[2]; //存储出口坐标
int map[M+2][N+2];//存储迷宫地图
int c_map[M+2][N+2];//存储迷宫地图拷贝
int minLen = MAXLEN; //最短路径
void GetBegin();//创建入口坐标
void GetEnd();//创建出口坐标
int IsBegin(int x, int y);//判断该点是否为入口
int IsEnd(int x, int y);//判断该点是否为出口
void BuildMiGong(); //建立迷宫地图
void PerfectMiGong();//修正迷宫地图
void CopyMiGong();//拷贝迷宫地图
void PrintMiGong();//输出迷宫地图
void PrintWay(); //输出运动路径
int ExtentSearchWay();//寻找路径:广度搜索
int DeepSearchWay();//寻找路径:深度搜索
int Search(int x, int y);//深度搜索递归子函数
int DeepSearchWay_2();//寻找路径:深度搜索
void PutStack(int top); //把栈路径显示到迷宫中
void PutQueue(int rear); //把队列路径显示到迷宫中
int DeepSearchWay_3();//寻找路径:深度搜索(最短路径)
int main()
{
char choice;
do
{
BuildMiGong();
PrintMiGong();
puts("对迷宫地图还满意吗?选择此迷宫输入y,重新选择迷宫输入n, 修改迷宫输入x");
do
{
scanf("%c", &choice);
} while (choice != 'y' && choice != 'n' && choice != 'x');
} while (choice == 'n');
if (choice == 'x') //修正迷宫地图
PerfectMiGong();
GetBegin(); //创建入口坐标
GetEnd(); //创建出口坐标
puts("广度搜索:");
if (ExtentSearchWay()) //如果找到迷宫路径
PrintWay(); //输出路径
else
puts("此路不通!");
puts("深度搜索1:");
if (DeepSearchWay()) //如果找到迷宫路径
PrintWay(); //输出路径
else
puts("此路不通!");
puts("深度搜索2:");
if (DeepSearchWay_2()) //如果找到迷宫路径
PrintWay(); //输出路径
else
puts("此路不通!");
puts("深度搜索3:");
if (DeepSearchWay_3()) //如果找到迷宫路径
PrintWay(); //输出路径
else
puts("此路不通!");
return 0;
}
void GetBegin()//创建入口坐标
{
do
{
puts("请输入入口坐标:");
scanf("%d%d", &begin[0], &begin[1]);
} while (begin[0]<1 || begin[0]>M || begin[1]<1 || begin[1]>N);
}
void GetEnd()//创建出口坐标
{
do
{
puts("请输入出口坐标:");
scanf("%d%d", &end[0], &end[1]);
} while (end[0]<1 || end[0]>M || end[1]<1 || end[1]>N);
}
int IsBegin(int x, int y)//判断该点是否为入口
{
return (x==begin[0] && y==begin[1]) ? 1 : 0;
}
int IsEnd(int x, int y)//判断该点是否为出口
{
return (x==end[0] && y==end[1]) ? 1 : 0;
}
void BuildMiGong() //建立迷宫地图
{
int i, j;
puts("请选择建立迷宫的方法: 输入m表示人工建立,输入c表示计算机建立");
char choice;
do
{
scanf("%c", &choice);
} while (choice != 'm' && choice != 'c');
//设置边界
for (i=0; i<M+2; i++)
{
map[i][0] = map[i][N+1] = CLOSE;
}
for (j=1; j<=N; j++)
{
map[0][j] = map[M+1][j] = CLOSE;
}
if (choice == 'm')
{
puts("请输入迷宫地图,0表示通,1表示不通");
for (i=1; i<=M; i++)
for (j=1; j<=N; j++)
scanf("%d", &map[i][j]);
}
else
{
srand( (unsigned) time(NULL));
for (i=1; i<=M; i++)
for (j=1; j<=N; j++)
map[i][j] = rand() % 2;
}
}
void PerfectMiGong()//修正迷宫地图
{
int i, j;
char choice;
do
{
do
{
puts("请输入需要修改的坐标:(输入0,0结束)");
scanf("%d%d", &i, &j);
} while (i<0 || i>M || j<0 || j>N);
fflush(stdin);
if (i!=0 && j!=0)
{
do
{
puts("打开该点输入o,关闭该点输入c");
scanf("%c", &choice);
fflush(stdin);
} while (choice != 'c' && choice != 'o');
if (choice == 'o')
map[i][j] = OPEN;
else
map[i][j] = CLOSE;
PrintMiGong();
}
} while (i!=0 && j!=0);
}
void CopyMiGong()//拷贝迷宫地图
{
int i, j;
for (i=0; i<M+2; i++)
for (j=0; j<N+2; j++)
c_map[i][j] = map[i][j];
}
void PrintMiGong()//输出迷宫地图
{
int i, j;
for (i=0; i<M+2; i++)
{
for (j=0; j<N+2; j++)
{
if (i==0 || i==M+1)
printf("%02d", j);
else if (j==0 || j==N+1)
printf("%02d", i);
else if (OPEN == map[i][j])
printf(" ");
else
printf("##");
}
printf("\n");
}
}
void PrintWay() //输出运动路径
{
int i, j;
int count = 0;
for (i=0; i<M+2; i++)
{
for (j=0; j<N+2; j++)
{
if (i==0 || i==M+1)
printf("%02d", j);
else if (j==0 || j==N+1)
printf("%02d", i);
else if (ROAD == c_map[i][j])
{
count++;
printf("%c%c", 2, 2);
}
else if (PASSED == c_map[i][j])
printf("%c%c", 3, 3);
else if (OPEN == c_map[i][j])
printf(" ");
else
printf("##");
}
printf("\n");
}
printf("共%d步\n", count);
}
int ExtentSearchWay()//寻找路径:广度搜索
{
int i, j, x, y;
int front = 0; //队首
int rear = 0; //队尾
CopyMiGong();
way[0].x = begin[0];
way[0].y = begin[1];
c_map[way[0].x][way[0].y] = PASSED; //该点已走过
while (front<=rear)
{
for (i=0; i<4; i++)//判断当前路径点四周是否可通过
{
x = way[front].x + zx[i];
y = way[front].y + zy[i];
if (c_map[x][y] == OPEN)//如果某个方向可通过,将该点纳入队列
{
rear++;
way[rear].x = x;
way[rear].y = y;
way[rear].pre = front;
c_map[x][y] = PASSED;
}
if (IsEnd(x, y)) //找到出口
{
PutQueue(rear); //把队列路径显示到迷宫中
printf("\n广度优先搜索,总共搜索过%d个点\n", rear);
return true;
}
}
front++;
}
return false;
}
void PutQueue(int rear) //把队列路径显示到迷宫中
{
int i = rear;
CopyMiGong();
do
{
c_map[way[i].x][way[i].y] = ROAD;
i = way[i].pre;
} while (!IsBegin(way[i].x, way[i].y));
c_map[begin[0]][begin[1]] = ROAD;
}
int DeepSearchWay()//寻找路径:深度搜索
{
CopyMiGong();
if (c_map[begin[0]][begin[1]] == OPEN && Search(begin[0], begin[1]))
{
c_map[begin[0]][begin[1]] = ROAD;
return true;
}
return false;
}
int Search(int x, int y)//深度搜索递归子函数
{
int i;
c_map[x][y] = PASSED;
if (IsEnd(x, y)) //找到出口
{
c_map[x][y] = ROAD;
return true;
}
for (i=0; i<4; i++)//判断当前路径点四周是否可通过
{
if (c_map[x+zx[i]][y+zy[i]] == OPEN && Search(x+zx[i], y+zy[i]))
{
c_map[x][y] = ROAD;
return true;
}
}
return false;
}
int DeepSearchWay_2()//寻找路径:深度搜索
{
int x, y;
int top = 0; //栈顶指针
int sum = 0;//累积搜索过的点数量
CopyMiGong();
way[0].x = begin[0];
way[0].y = begin[1];
way[0].pre = 0;
c_map[way[0].x][way[0].y] = PASSED; //该点已走过
while (top >= 0)
{
if (way[top].pre < 4)
{
x = way[top].x + zx[way[top].pre];
y = way[top].y + zy[way[top].pre];
if (c_map[x][y] == OPEN)//如果某个方向可通过,将该点纳入栈
{
sum++;
top++;
way[top].x = x;
way[top].y = y;
way[top].pre = 0;
c_map[x][y] = PASSED;
if (IsEnd(x, y)) //找到出口
{
PutStack(top); //把栈路径显示到迷宫中
printf("\n深度优先搜索可行路径,总共搜索过%d个点\n", sum);
return true;
}
}
else //否则换个方向
way[top].pre++;
}
else
{
top--;
}
}
return false;
}
void PutStack(int top) //把栈路径显示到迷宫中
{
CopyMiGong();
while (top >= 0)
{
c_map[way[top].x][way[top].y] = ROAD;
top--;
}
}
int DeepSearchWay_3()//寻找路径:深度搜索(最短路径)
{
int x, y, i, j;
int top = 0; //栈顶指针
int pathLen[M+2][N+2] = {0};
struct stype shortWay[M*N];
int flag = false; //标记是否能到达终点
int sum = 0;//累积搜索过的点数量
for (x=0; x<M+2; x++) //设置各点初始路径长度均为最大值
for (y=0; y<N+2; y++)
pathLen[x][y] = MAXLEN;
CopyMiGong();
minLen = MAXLEN; //最短路径
way[0].x = begin[0];
way[0].y = begin[1];
way[0].pre = 0;
pathLen[begin[0]][begin[1]] = 0;
while (top >= 0)
{
if (way[top].pre < 4)
{
x = way[top].x + zx[way[top].pre];
y = way[top].y + zy[way[top].pre];
if (c_map[x][y] == OPEN && pathLen[x][y] > top+1)//如果某个方向可通过,且为最短路径,将该点纳入栈
{
sum++;
top++;
way[top].x = x;
way[top].y = y;
way[top].pre = 0;
pathLen[x][y] = top;
if (IsEnd(x, y)) //找到出口
{
if (top < minLen)
{
minLen = top;
for (i=0; i<=top; i++)
{
shortWay[i] = way[i];
}
}
flag = true;
top--;
}
}
else //否则换个方向
way[top].pre++;
}
else
{
top--;
}
}
if (flag)
{
for (i=0; i<=minLen; i++)
{
way[i] = shortWay[i];
}
PutStack(minLen); //把栈路径显示到迷宫中
}
printf("\n深度优先搜索最短路径,总共搜索过%d个点\n", sum);
return flag;
}