迷宫C语言源程序代码

迷宫C语言源程序代码,第1张

#include<stdio.h>

#include<malloc.h>

int N

int **maze

int zx,zy

void PrintMaze(int N){

int *l[2],i,j

l[0]=(int*)malloc(sizeof(l)*N*N)

l[1]=(int*)malloc(sizeof(l)*N*N)

for(i=0i<N*Ni++){

l[0][i]=-1

}

for(i=0i<Ni++){

for(j=0j<Nj++){

if(maze[i][j]>1){

l[0][maze[i][j]-2]=i

l[1][maze[i][j]-2]=j

}

}

}for(i=0i<N*Ni++){

if(l[0][i]!=-1)printf("(%d,%d)\n",l[0][i]+1,l[1][i]+1)

else break

}

}int SelMaze(int x,int y){

if(x>9||y>9||x<0||y<0)return(0)

return(!maze[x][y])

}int FindMaze(int x,int y,int m){

if(x==zx&&y==zy){

maze[x][y]=m

return(1)

}

if(SelMaze(x+1,y)){

maze[x][y]=m

if(FindMaze(x+1,y,m+1))PrintMaze(N)

maze[x][y]=0

}if(SelMaze(x,y+1)){

maze[x][y]=m

if(FindMaze(x,y+1,m+1))PrintMaze(N)

maze[x][y]=0

}if(SelMaze(x,y-1)){

maze[x][y]=m

if(FindMaze(x,y-1,m+1))PrintMaze(N)

maze[x][y]=0

}if(SelMaze(x-1,y)){

maze[x][y]=m

if(FindMaze(x-1,y,m+1))PrintMaze(N)

maze[x][y]=0

}

return(0)

}

int main(){

int x,y,i,j

scanf("%d\n%d%d%d%d",&N,&x,&y,&zx,&zy)

zx--zy--x--y--

maze=(int**)malloc(sizeof(int*)*N)

for(i=0i<Ni++){

maze[i]=(int*)malloc(sizeof(maze)*N)

for(j=0j<Nj++){

scanf("%d",&maze[i][j])

}

}

if(!FindMaze(x,y,2))printf("No")

return(0)

}

源程序如下:

#include<iostream>

#include<ctime>

#include<stack>

#include<windows.h>

using namespace std

const int East = 0

const int South = 1

const int West = 2

const int North = 3

const int pause = 50//老鼠走一步需要的毫秒数

class MouseMaze

{

public:

MouseMaze(int h, int w)

~MouseMaze()

void display()const

bool hasCrossOver()

private:

const int row

const int col

char **p

void Init()

}

MouseMaze::MouseMaze(int h, int w):row(h), col(w){

p = new char *[row]

for (int i=0i<row++i)

p[i] = new char[col]

Init()

}

MouseMaze::~MouseMaze(){

for (int i=0i<rowi++)

delete[] p[i]

delete[] p

}

void MouseMaze::display()const {

for (int i=0i<row++i){

for (int j=0j<col++j)

cout <<p[i][j]

cout <<endl

}

}

void MouseMaze::Init() //随机生成迷宫

{

srand(time(0))

for (int i=0i<row++i){

for (int j=0j<col++j)

{

if ((i==0) || (j==0) || (i==row-1) || (j==col-1)) {

p[i][j] = '*'

continue

}

if (rand() % 100 <80)

p[i][j] = ' '

else

p[i][j] = 'O'

}

}

p[1][1] = 'Z'

}

bool MouseMaze::hasCrossOver(){

int flag, i = 1, j = 1

stack<int>s

while (true){

if (i==row-2 &&j==col-2)

{

int m, n

stack<int>newStack

while (!s.empty()){

newStack.push(s.top())

s.pop()

}

for (m=0m<row++m){

for (n=0n<col++n)

if (p[m][n] == 'Z')

p[m][n] = ' '

}

m = 1

n = 1

p[m][n] = 'Z'

while (!newStack.empty()){

flag = newStack.top()

switch (flag){

case East:

++n break

case South:

++mbreak

case West:

--nbreak

case North:

--mbreak

}

p[m][n] = 'Z'

Sleep(pause)

system("cls")

display()

newStack.pop()

}

return true

}//end if

//case East:

if ((j+1 <col-1) &&(p[i][j+1] == ' ')){

p[i][++j] = 'Z'

s.push(East)

}

//case South:

elseif ((i+1 <row-1) &&(p[i+1][j] == ' ')){

p[++i][j] = 'Z'

s.push(South)

}

//case West:

elseif ((j-1 >0) &&(p[i][j-1] == ' ')){

p[i][--j] = 'Z'

s.push(West)

}

//case North:

elseif ((i-1 >0) &&(p[i-1][j] == ' ')){

p[--i][j] = 'Z'

s.push(North)

}

else {

if (s.empty()) return false

flag = s.top()

s.pop()

switch (flag){

case East:

--j break

case South:

--ibreak

case West:

++jbreak

case North:

++ibreak

}

}

}

}

void main()

{

MouseMaze gMaze(20, 79)//迷宫高20, 宽79

gMaze.display()

system("pause")

if (gMaze.hasCrossOver())

cout <<"\n老鼠成功逃出迷宫" <<endl

else

cout <<"\n老鼠不能逃走<<endl

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11531487.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存