主题:[讨论]迷宫小问题
#include<iostream>
#define EAST MAZE[x][y+1]//定义东方相对位置
#define WEST MAZE[x][y-1]
#define SOUTH MAZE[x+1][y]//定义南方相对位置
#define NORTH MAZE[x-1][y]
using namespace std;
const int ExitX=8;
const int ExitY=10;
struct list
{
int x,y;
struct list*next;
};
typedef struct list node;
typedef node* link;
int MAZE[10][12]={1,1,1,1,1,1,1,1,1,1,1,1,
1,0,0,0,1,1,1,1,1,1,1,1,
1,1,1,0,1,1,0,0,0,0,1,1,
1,1,1,0,1,1,0,1,1,0,1,1,
1,1,1,0,0,0,0,1,1,0,1,1,
1,1,1,0,1,1,0,1,1,0,1,1,
1,1,1,0,1,1,0,1,1,0,1,1,
1,1,1,1,1,1,0,1,1,0,1,1,
1,1,0,0,0,0,0,0,1,0,0,1,
1,1,1,1,1,1,1,1,1,1,1,1,};
link push(link stack,int x,int y);
link pop(link stack,int* x,int* y);
int chkExit(int ,int ,int ,int );
int main()
{
int i,j;
link path=NULL;
int x=1;
int y=1;//设置入口位置
cout<<"[迷宫的路径(0的部分)]"<<endl;//输出迷宫的路径
for(i=0;i<10;i++)
{
for(j=0;j<12;j++)
cout<<MAZE[i][j]<<" ";
cout<<endl;
}
while(x<=ExitX && y<=ExitY)
{
MAZE[x][y]=2;
if(NORTH==0)
{
x-=1;
path=push(path,x,y);
}
else if(SOUTH==0)
{
x+=1;
path=push(path,x,y);
}
else if(WEST==0)
{
y-=1;
path=push(path,x,y);
}
else if(EAST==0)
{
y+=1;
path=push(path,x,y);
}
else if(chkExit(x,y,ExitX,ExitY)==1)
break;//检查是否走到出口了
else
{
MAZE[x][y]=2;
path=pop(path,&x,&y);
}
}
cout<<"[老鼠走完的路径(2的部分)]"<<endl;//输出老鼠走完迷宫后的路径图
for(i=0;i<10;i++)
{
for(j=0;j<12;j++)
cout<<MAZE[i][j]<<" ";
cout<<endl;
}
system("pause");
return 0;
}
link push(link stack,int x,int y)
{
link newnode;
newnode=new node;
if(!newnode)
{
cout<<"!!!!!!!!!Error!!!!!!内存申请失败"<<endl;
return NULL;
}
newnode->x=x;
newnode->y=y;
newnode->next=stack;
stack=newnode;
return stack;
}
link pop(link stack,int* x,int* y)
{
link top;
if(stack!=NULL)
{
top=stack;
stack=stack->next;
*x=top->x;
*y=top->y;
delete top;
return stack;
}
else
*x=-1;
return stack;
}
int chkExit(int x,int y,int ex,int ey)//这是判断是否走到出口的代码
{
if(x==ex && y==ey)
{
if(NORTH==1 || SOUTH==1 ||WEST==1 || EAST==2)//这里的四行代码我看不懂,想请问下论坛上的你们
return 1;
if(NORTH==1 || SOUTH==1 ||WEST==2 || EAST==1)//给点留言
return 1;
if(NORTH==1 || SOUTH==2 ||WEST==1 || EAST==1)
return 1;
if(NORTH==2 || SOUTH==1 ||WEST==1 || EAST==1)
return 1;
}
return 0;
}
这是一道简单的迷宫问题,由于我是自学还不是很熟悉,想请教点问题[em10][em10][em10][em10][em10]
#define EAST MAZE[x][y+1]//定义东方相对位置
#define WEST MAZE[x][y-1]
#define SOUTH MAZE[x+1][y]//定义南方相对位置
#define NORTH MAZE[x-1][y]
using namespace std;
const int ExitX=8;
const int ExitY=10;
struct list
{
int x,y;
struct list*next;
};
typedef struct list node;
typedef node* link;
int MAZE[10][12]={1,1,1,1,1,1,1,1,1,1,1,1,
1,0,0,0,1,1,1,1,1,1,1,1,
1,1,1,0,1,1,0,0,0,0,1,1,
1,1,1,0,1,1,0,1,1,0,1,1,
1,1,1,0,0,0,0,1,1,0,1,1,
1,1,1,0,1,1,0,1,1,0,1,1,
1,1,1,0,1,1,0,1,1,0,1,1,
1,1,1,1,1,1,0,1,1,0,1,1,
1,1,0,0,0,0,0,0,1,0,0,1,
1,1,1,1,1,1,1,1,1,1,1,1,};
link push(link stack,int x,int y);
link pop(link stack,int* x,int* y);
int chkExit(int ,int ,int ,int );
int main()
{
int i,j;
link path=NULL;
int x=1;
int y=1;//设置入口位置
cout<<"[迷宫的路径(0的部分)]"<<endl;//输出迷宫的路径
for(i=0;i<10;i++)
{
for(j=0;j<12;j++)
cout<<MAZE[i][j]<<" ";
cout<<endl;
}
while(x<=ExitX && y<=ExitY)
{
MAZE[x][y]=2;
if(NORTH==0)
{
x-=1;
path=push(path,x,y);
}
else if(SOUTH==0)
{
x+=1;
path=push(path,x,y);
}
else if(WEST==0)
{
y-=1;
path=push(path,x,y);
}
else if(EAST==0)
{
y+=1;
path=push(path,x,y);
}
else if(chkExit(x,y,ExitX,ExitY)==1)
break;//检查是否走到出口了
else
{
MAZE[x][y]=2;
path=pop(path,&x,&y);
}
}
cout<<"[老鼠走完的路径(2的部分)]"<<endl;//输出老鼠走完迷宫后的路径图
for(i=0;i<10;i++)
{
for(j=0;j<12;j++)
cout<<MAZE[i][j]<<" ";
cout<<endl;
}
system("pause");
return 0;
}
link push(link stack,int x,int y)
{
link newnode;
newnode=new node;
if(!newnode)
{
cout<<"!!!!!!!!!Error!!!!!!内存申请失败"<<endl;
return NULL;
}
newnode->x=x;
newnode->y=y;
newnode->next=stack;
stack=newnode;
return stack;
}
link pop(link stack,int* x,int* y)
{
link top;
if(stack!=NULL)
{
top=stack;
stack=stack->next;
*x=top->x;
*y=top->y;
delete top;
return stack;
}
else
*x=-1;
return stack;
}
int chkExit(int x,int y,int ex,int ey)//这是判断是否走到出口的代码
{
if(x==ex && y==ey)
{
if(NORTH==1 || SOUTH==1 ||WEST==1 || EAST==2)//这里的四行代码我看不懂,想请问下论坛上的你们
return 1;
if(NORTH==1 || SOUTH==1 ||WEST==2 || EAST==1)//给点留言
return 1;
if(NORTH==1 || SOUTH==2 ||WEST==1 || EAST==1)
return 1;
if(NORTH==2 || SOUTH==1 ||WEST==1 || EAST==1)
return 1;
}
return 0;
}
这是一道简单的迷宫问题,由于我是自学还不是很熟悉,想请教点问题[em10][em10][em10][em10][em10]