迷宫求解c语言
㈠ 求解c语言一递归迷宫问题
给你给伪算法:(设坐标为x,y,坐标向右和下延生。)
函数:
{
判断当前是不是(7,7),如果是,表示走出迷宫。打印轨迹
1 尝试往左先走一步(x-1,如果x小于0,或者对应位置标识为阻塞)
2 1如果成功,用本函数递归调用左走一步的坐标,并记下当前位置到轨迹列表。
3 尝试往前先走一步(y+1,如果y小于0,或者对应位置标识为阻塞)
4 3如果成功,用本函数递归调用前走一步的坐标,并记下当前位置到轨迹列表。
5 尝试往右先走一步(x+1,如果x小于0,或者对应位置标识为阻塞)
6 5如果成功,用本函数递归调用前走一步的坐标,并记下当前位置到轨迹列表。
如果是(0,0),表示没有合适的路径可走出迷宫。
如果不是(0,0),将轨迹列表最后一位弹出。
}
㈡ 求C语言迷宫程序的解释说明!!!
我看了一下,算法应该是一样的,下面是我以前用C++写的
相信你能看得懂
/////////////////////////
/////////迷宫求解////////
//////作者:hacker/////
/////时间:11.10.2006/////
/////////////////////////
/*class:
Matrix:矩阵类
offsets:搜索偏移
enum directions:四个方向
struct item:搜索节点
Migong:迷宫类
1.创建一个Migong对象
2.使用用Create方法输入数据
3.使用Solve方法进行求解
4.ShowSolve方法显示解
5.可以重复使用Create方法
6.入口只能在左上角
7.默认出口在右下角
ShowAllPath:穷举所有的路径
备注:
由于算法原因,这里的所有路径应该是指
介于:
a.如果两条路存在某个点不同那么就是不同的路
b.如果在一条路中去掉一个或者一个以上的圈,那么他们是同一条路
之间意义上的路
*/
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
#ifndef MIGONG_H
#define MIGONG_H
///////////////////
///////矩阵类//////
///////////////////
class Matrix{
int* m;
int row, col;
bool iscreate;
public:
Matrix(){m=0;iscreate=false;};
~Matrix() {Release();};
bool Create(int, int);
int& operator () (int, int);
int GetRow(){return row;};
int GetCol(){return col;};
void Release();
void Show(char, char );
};
bool Matrix::Create(int r, int c)
{
if( r<=0 || c<=0) return false;
Release();
row = r;
col = c;
m = new int[row*col];
for (int i=0;i<row*col;i++)
{
*(m+i) = 0;
}
iscreate = true;
return true;
}
int& Matrix::operator ()(int r, int c)
{
return *(m+r*col+c);
}
void Matrix::Release()
{
if (iscreate)
{
row = col = 0;
if (m) delete[] m;
m = 0;
}
iscreate = false;
}
void Matrix::Show(char blk='#', char nblk=' ')
{
int i, j;
for (i=0;i<row;i++)
{
for (j=0;j<col;j++)
{
if (*(m+i*col+j) == 0)
cout<<nblk;
else
cout<<blk;
}
cout<<endl;
}
}
/////////////////////////////
////迷宫相关数据结构的定义///
/////////////////////////////
struct offsets{
int a, b;
};
enum directions{
_S = 0,
_E,
_N,
_W
};
struct item{
int row, col, dir;
};
class Migong{
static offsets move[4];
Matrix maze;
Matrix mark;
int row;
int col;
int desr;
int desc;
stack<item> stk;
bool iscreate;
int pathlength;
bool GetPath();
bool IsInPath(int, int);
public:
Migong(){issolved=false;result=0;pathlength=row=col=0;iscreate=false;};
~Migong(){Release();};
bool Create(int* , int , int , int , int );
void Solve();
void Release();
void OutputMaze();
void ShowSolve(char, char );
public:
bool issolved;
item* result;
};
offsets Migong::move[4]={ {1, 0}, {0, 1},
{-1, 0}, {0, -1}};
////////////////////////////
//迷宫数据应该是不含边框的//
////////////////////////////
bool Migong::Create(int* m, int r, int c, int desrow=-1, int descol=-1)
{
if (r<=0 || c<=0) return false;
Release();
if (desrow==-1 || descol==-1)
{
desr = r;
desc = c;
}
else
{
desr = desrow;
desc = descol;
}
row = r;
col = c;
maze.Create(r+2, c+2);
mark.Create(r+2, c+2);
int i, j;
for (i=0;i<r+2;i++)
{
for (j=0;j<c+2;j++)
{
if (j==0 || j==c+1 || i==0 || i==r+1)
{
mark(i, j) = maze(i, j) = 1;
}else
{
mark(i, j) = 0;
maze(i, j) = m[((i-1)*col+j-1)];
}
}
}
return iscreate = true;
}
bool Migong::GetPath()
{
mark(1,1) = 1;
item temp;
temp.col = 1;
temp.row = 1;
temp.dir = _S;
stk.push(temp);
while (!stk.empty())
{
temp = stk.top();
stk.pop();
int i = temp.row;
int j = temp.col;
int d = temp.dir;
while (d<4)
{//根据当前点的状态确定下一个搜索点
int g = i + move[d].a;
int h = j + move[d].b;
if (g==desr && h==desc)
{
return true;
}
//如果这个点不是障碍点且没有被搜索过那么可以对这个点进行搜索
if (maze(g, h)==0 && mark(g, h)==0)
{
mark(g, h) = 1;
temp.row = g;
temp.col = h;
temp.dir = d+1;
stk.push(temp);
i = g;
j = h;
d = _S;//对一下个点进行搜索
}
else d++;
}
}
return false;
}
void Migong::Solve()
{
issolved = GetPath();
if (issolved)
{
pathlength = stk.size();
result = new item[pathlength];
for (int i=0;i<pathlength;i++)
{
*(result+i) = stk.top();
stk.pop();
// cout<<"("<<(*(result+i)).row<<","<<(*(result+i)).col<<")"<<endl;
}
}
while (!stk.empty())
stk.pop();
}
void Migong::Release()
{
if (iscreate)
{
maze.Release();
mark.Release();
row=col=0;
if (result)
delete [] result;
result = 0;
while (!stk.empty())
stk.pop();
}
iscreate = false;
issolved = false;
pathlength = 0;
}
void Migong::OutputMaze()
{
if (!iscreate) return;
maze.Show();
}
bool Migong::IsInPath(int r, int c)
{
if (!iscreate || !issolved)
return false;
item temp;
for (int i=0;i<pathlength;i++)
{
temp = *(result+i);
if ((temp.row==r) && (temp.col==c))
return true;
}
return false;
}
void Migong::ShowSolve(char blk='#',char s='o')
{
if (!iscreate) return;
if (!issolved)
{
cout<<"无解"<<endl;
}
else
{
int i, j;
for (i=0;i<row+2;i++)
{
for (j=0;j<col+2;j++)
{
if ((i==1 && j==1) || (i==desr && j==desc))
{
cout<<s;
}
else if (maze(i, j) == 1)
{
cout<<blk;
}else
{
if (IsInPath(i, j))
cout<<s;
else
cout<<' ';
}
}
cout<<endl;
}
}
}
//////////////////////
//////穷举所有路径////
//////////////////////
offsets move[4]={ {1, 0}, {0, 1},
{-1, 0}, {0, -1}};
struct node
{
int row,col;
};
vector<node> path;
int count;
bool IsReachable( Matrix& maze, Matrix& mark, node beg, node des)
{
if (beg.row==des.row&&beg.col==des.col)
{//如果达到的话那么显示路径
count++;
cout<<"第"<<count<<"条路径:"<<endl;
for (int i=0;i<path.size();i++)
cout<<"("<<path[i].row<<","<<path[i].col<<")";
cout<<"("<<des.row<<","<<des.col<<")";
cout<<endl;
return false;
}
if (maze(beg.row, beg.col)==1 || mark(beg.row, beg.col)==1)
{
return false;
}
path.push_back(beg);
mark(beg.row, beg.col) = 1;
node nextnode;
for (int i=_S;i<_W+1;i++)
{
nextnode.row = beg.row + move[i].a;
nextnode.col = beg.col + move[i].b;
IsReachable(maze, mark, nextnode, des);
}
path.resize(path.size()-1);
mark(beg.row, beg.col) = 0;
return false;//如果不是穷举的话应该根据for循环的结果重新设置返回值
}
/*
参数maze,mark为迷宫长宽均加二的矩阵
desr,desc为出口点
*/
void FindAllPath( Matrix& maze, Matrix& mark, int desr, int desc)
{
node first, last;
first.row = 1;
first.col = 1;
last.row = desr;
last.col = desc;
IsReachable(maze, mark, first, last);
path.clear();
}
/*
m迷宫矩阵数据
r,c行和列的大小
desr,desc目标位置
*/
void ShowAllPath(int* m, int r, int c, int desr=-1, int desc=-1)
{
Matrix maze, mark;
maze.Create(r+2, c+2);
mark.Create(r+2, c+2);
if (desr==-1 || desc==-1)
{
desr = r;
desc = c;
}
int i, j;
for (i=0;i<r+2;i++)
{
for (j=0;j<c+2;j++)
{
if (j==0 || j==c+1 || i==0 || i==r+1)
{
mark(i, j) = maze(i, j) = 1;
}else{
mark(i, j) = 0;
maze(i, j) = m[((i-1)*c+j-1)];
}
}
}
count = 0;
FindAllPath(maze, mark, desr, desc);
maze.Release();
mark.Release();
}
#endif
㈢ 迷宫问题 C语言,数据结构课设
你好
楼主。
很幸运的看到你的问题。
但是又很遗憾到现在还没有人回答你的问题。也可能你现在已经在别的地方找到了答案,那就得恭喜你啦。
对于你的问题我爱莫能助!
可能是你问的问题有些专业了。或者别人没有遇到或者接触过你的问题,所以帮不了你。建议你去问题的相关论坛去求助,那里的人通常比较多,也比较热心,可能能快点帮你解决问题。
希望我的回答也能够帮到你!
祝你好运。
快过年了,
最后祝您全家幸福健康快乐每一天!
㈣ 如何用C语言实现求迷宫的最短路径
#include<stdio.h>
#include<stdlib.h>
#define M 8
#define N 8
#define Max 100
int mg[M+2][N+2]= //定义迷宫,0表示能走的块,1表示不能走,在外围加上一圈不能走的块
{
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,1,0,0,0,1,0,1},
{1,0,0,0,0,1,1,0,0,1},
{1,0,1,1,1,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,1},
{1,0,1,0,0,0,1,0,0,1},
{1,0,1,1,1,0,1,1,0,1},
{1,1,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
struct
{
int i,j; //块的位置
int pre; //本路径中上一块在队列中的下标
}Qu[Max];
int front=-1,rear=-1;
void print(int n);
int mgpath(int xi,int yi,int xe,int ye) //搜索算法
{
int i,j,find=0,di;
rear++;
Qu[rear].i=xi;
Qu[rear].j=yi;
Qu[rear].pre=-1;
mg[1][1]=-1;
while(front<=rear&&!find)
{
front++;
i=Qu[front].i;
j=Qu[front].j;
if(i==xe&&j==ye)
{
find=1;
print(front);
return(1);
}
for(di=0;di<4;di++)
{
switch(di) //四个方向
{
case 0:i=Qu[front].i-1;j=Qu[front].j;break;
case 1:i=Qu[front].i;j=Qu[front].j+1;break;
case 2:i=Qu[front].i+1;j=Qu[front].j;break;
case 3:i=Qu[front].i;j=Qu[front].j-1;break;
}
if(mg[i][j]==0)
{
rear++;
Qu[rear].i=i;
Qu[rear].j=j;
Qu[rear].pre=front;
mg[i][j]=-1; //避免死循环
}
}
}
return 0;
}
void print(int n) //输出 路径算法
{
int k=n,j,m=1;
printf("\n");
do //将输出的路径上的所有pre改为-1
{
j=k;
k=Qu[k].pre;
Qu[j].pre=-1;
}while(k!=0);
printf("迷宫最短路径如下:\n");
k=0;
while(k<Max)
{
if(Qu[k].pre==-1)
{
printf("\t(%d,%d)",Qu[k].i,Qu[k].j);
if(m%5==0)
printf("\n");
m++;
}
k++;
}
printf("\n");
}
int main()
{
mgpath(1,1,M,N);
system("pause");
return 0;
}
㈤ c语言用栈解决迷宫问题
明摆着图的深度遍历 用堆栈模拟递归
㈥ c语言的迷宫问题
//寻路_带权重_带障碍_最短_文件地图_不闪------wlfryq------//
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
typedefstruct
{
intx;
inty;
}_PT;
_PTpt;
introw=0,col=0;
//设置CMD窗口光标位置
voidsetxy(intx,inty)
{
COORDcoord={x,y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
//获取当前CMD当前光标所在位置
voidgetxy()
{
HANDLEhConsole=GetStdHandle(STD_OUTPUT_HANDLE);
COORDcoordScreen={0,0};//光标位置
CONSOLE_SCREEN_BUFFER_INFOcsbi;
if(GetConsoleScreenBufferInfo(hConsole,&csbi))
{
pt.x=csbi.dwCursorPosition.X;
pt.y=csbi.dwCursorPosition.Y;
}
}
typedefstruct
{
intx;
inty;
inttype;
intv;
}PT;
PT**s=NULL,stack[50],start,end,c;
intpos=0;
voidprt()
{
inti,j;
system("cls");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(s[i][j].type==1)
{
printf("■");
}
elseif(i==end.x&&j==end.y)
{
printf("★");
}
elseif(i==c.x&&j==c.y)
{
printf("◎");
}
else
{
printf("");
}
}
printf(" ");
}
Sleep(500);
}
voidstack_in(PTa)
{
stack[pos++]=a;
}
PTstack_out()
{
inti;
PTt;
t=stack[0];
for(i=0;i<pos-1;i++)
{
stack[i]=stack[i+1];
}
pos--;
returnt;
}
voidfun()
{
PTa;
intx,y,v;
while(1)
{
if(pos==0)
{
break;
}
a=stack_out();
x=a.x;
y=a.y;
if(x==start.x&&y==start.y)
{
break;
}
v=s[x][y].v;
if(x-1>=0&&s[x-1][y].type==0&&(s[x-1][y].v==-1||s[x-1][y].v>v+1))
{
s[x-1][y].v=v+1;
stack_in(s[x-1][y]);
}
if(x+1<=row-1&&s[x+1][y].type==0&&(s[x+1][y].v==-1||s[x-1][y].v>v+1))
{
s[x+1][y].v=v+1;
stack_in(s[x+1][y]);
}
if(y-1>=0&&s[x][y-1].type==0&&(s[x][y-1].v==-1||s[x-1][y].v>v+1))
{
s[x][y-1].v=v+1;
stack_in(s[x][y-1]);
}
if(y+1<=col-1&&s[x][y+1].type==0&&(s[x][y+1].v==-1||s[x-1][y].v>v+1))
{
s[x][y+1].v=v+1;
stack_in(s[x][y+1]);
}
}
}
voidgo(intx,inty)
{
printf(" 按任意键开始 ");
getchar();
intv;
while(1)
{
if(x==end.x&&y==end.y)
{
setxy(0,y+2);
printf("end....");
return;
}
v=s[x][y].v;
if(v==0)
{
return;
}
if(x-1>=0&&s[x-1][y].v==v-1)
{
c=s[x-1][y];
setxy(y*2,x);
x=x-1;
printf("");
setxy(y*2,x);
printf("◎");
Sleep(500);
continue;
}
elseif(x+1<=row-1&&s[x+1][y].v==v-1)
{
c=s[x+1][y];
setxy(y*2,x);
x++;
printf("");
setxy(y*2,x);
printf("◎");
Sleep(500);
continue;
}
elseif(y-1>=0&&s[x][y-1].v==v-1)
{
c=s[x][y-1];
setxy(y*2,x);
y--;
printf("");
setxy(y*2,x);
printf("◎");
Sleep(500);
continue;
}
elseif(y+1<=col-1&&s[x][y+1].v==v-1)
{
c=s[x][y+1];
setxy(y*2,x);
y++;
printf("");
setxy(y*2,x);
printf("◎");
Sleep(500);
continue;
}
}
printf(" returngo ");
system("pause");
}
voidGetMapFromFile()
{
inti,j,x,y,len;
charch[50]={'