#include <iostream>
#include<ctime>
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define SIZE 50
#define SIZEINCRE 10
static int mn=0;
using namespace std;
typedef struct{
int x;
int y;
}pos;
typedef struct{
int xuhao;
int fang;
pos xy;
}elemtype;
typedef struct{
elemtype*base;
elemtype*top;
int size;
}Sqstack;
int Initsqstack(Sqstack &L){
L.base=(elemtype*)malloc(SIZE*sizeof(elemtype));
if(!L.base)return ERROR;
L.top=L.base;
L.size=SIZE;
return OK;
}
int push(Sqstack&L,elemtype e){
if(L.top-L.base>=SIZE-1){
L.base=(elemtype*)realloc(L.base,SIZEINCRE*sizeof(elemtype));
L.size=SIZEINCRE+SIZE;}
*L.top++=e;
return OK;
}
int pop(Sqstack&L,elemtype &e){
if(L.base==L.top)return ERROR;
e=*(L.top-1);
return OK;
}
int dele(Sqstack&L){
if(L.base==L.top)return ERROR;
L.top=L.top-1;
return OK;
}
char goutu(int i){
if(i)return'0';
else return'#';
}
int jilu(pos a[50],elemtype e)
{a[mn].x=e.xy.x;
a[mn].y=e.xy.y;
mn++;
return OK;
}
int compare(pos a[50],elemtype e)
{
int j=0;int s=0;
while(j<mn){
if(a[j].x==e.xy.x && a[j].y==e.xy.y)
{s++;}
j++;
}
if(!s) return 1;
else return 0;
}
int move(Sqstack&L,elemtype e,int k,char **a,pos b[50])
{
cout<<"请输入终点坐标";
int m,n;
cin>>m>>n;
m--;n--;
e.xuhao=1;
push(L,e);jilu(b,e);
elemtype s;s=e;int x,y;
while(L.top!=L.base){
if(e.xy.x==m&&e.xy.y==n ){ cout<<"成功走出!"<<endl; break;}
for(int i=1;i<6;i++){
switch( i){
case 1:{x=e.xy.x;y=e.xy.y-1;break;}
case 2:{x=e.xy.x+1;y=e.xy.y;break;}
case 3:{x=e.xy.x;y=e.xy.y+1;break;}
case 4:{x=e.xy.x-1;y=e.xy.y;break;}
default:{dele(L);break;}
}
s.xy.x=x;
s.xy.y=y;
if((x>=0)&&(x<k)&&(y<k)&&(y>=0)&&(a[x][y]=='0')&&compare( b,s ))break;}
if((x>=0)&&(x<k)&&(y<k)&&(y>=0)&&a[x][y]=='0'&&compare( b,s )) {jilu(b,s);
push(L,s);
if(L.top-L.base<=1) (*(L.top-1)).xuhao++;
else (*(L.top-1)).xuhao=(*(L.top-2)).xuhao+1;
}
if(L.top==L.base)
{cout<<"无法走出"<<endl;break;}
e=*(L.top-1);
}
return OK;
}
int main(){srand(time(NULL));
elemtype e;
pos b[50];
Sqstack L;
Initsqstack(L);
cout<<"请输入迷宫大小(输入整数n,将随机构成n*n的迷宫)";
int k;
cin>>k;
char**a=new char*[k];
for(int i=0;i<k;i++)
a[i]=new char[k];
for(i=0;i<k;i++)
for(int j=0;j<k;j++)
a[i][j]=goutu(rand()%2);
for(i=0;i<k;i++){
for(int j=0;j<k;j++)
cout<<a[i][j];
cout<<endl;
}
cout<<endl<<"'0' 表示通路,'#' 表示障碍物。";
cout<<endl<<"请输入起点坐标:";
cin>>e.xy.x>>e.xy.y;e.xy.x--;e.xy.y--;
move(L,e,k,a,b);pop(L,e);
if(L.base!=L.top){
cout<<"总共经过"<<e.xuhao<<"点"<<endl;
cout<<"起点("<<L.base->xy.x+1<<","<<L.base->xy.y+1<<")-->";L.base++;
while(L.top-L.base>1)
{ cout<<"("<<L.base->xy.x+1<<","<<L.base->xy.y+1<<")-->";L.base++;}
cout<<"终点("<<L.base->xy.x+1<<","<<L.base->xy.y+1<<")"<<endl;}
for(i=0;i<k;i++)
delete[] a[i];
delete[] a;
return 0;
}
#include<ctime>
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define SIZE 50
#define SIZEINCRE 10
static int mn=0;
using namespace std;
typedef struct{
int x;
int y;
}pos;
typedef struct{
int xuhao;
int fang;
pos xy;
}elemtype;
typedef struct{
elemtype*base;
elemtype*top;
int size;
}Sqstack;
int Initsqstack(Sqstack &L){
L.base=(elemtype*)malloc(SIZE*sizeof(elemtype));
if(!L.base)return ERROR;
L.top=L.base;
L.size=SIZE;
return OK;
}
int push(Sqstack&L,elemtype e){
if(L.top-L.base>=SIZE-1){
L.base=(elemtype*)realloc(L.base,SIZEINCRE*sizeof(elemtype));
L.size=SIZEINCRE+SIZE;}
*L.top++=e;
return OK;
}
int pop(Sqstack&L,elemtype &e){
if(L.base==L.top)return ERROR;
e=*(L.top-1);
return OK;
}
int dele(Sqstack&L){
if(L.base==L.top)return ERROR;
L.top=L.top-1;
return OK;
}
char goutu(int i){
if(i)return'0';
else return'#';
}
int jilu(pos a[50],elemtype e)
{a[mn].x=e.xy.x;
a[mn].y=e.xy.y;
mn++;
return OK;
}
int compare(pos a[50],elemtype e)
{
int j=0;int s=0;
while(j<mn){
if(a[j].x==e.xy.x && a[j].y==e.xy.y)
{s++;}
j++;
}
if(!s) return 1;
else return 0;
}
int move(Sqstack&L,elemtype e,int k,char **a,pos b[50])
{
cout<<"请输入终点坐标";
int m,n;
cin>>m>>n;
m--;n--;
e.xuhao=1;
push(L,e);jilu(b,e);
elemtype s;s=e;int x,y;
while(L.top!=L.base){
if(e.xy.x==m&&e.xy.y==n ){ cout<<"成功走出!"<<endl; break;}
for(int i=1;i<6;i++){
switch( i){
case 1:{x=e.xy.x;y=e.xy.y-1;break;}
case 2:{x=e.xy.x+1;y=e.xy.y;break;}
case 3:{x=e.xy.x;y=e.xy.y+1;break;}
case 4:{x=e.xy.x-1;y=e.xy.y;break;}
default:{dele(L);break;}
}
s.xy.x=x;
s.xy.y=y;
if((x>=0)&&(x<k)&&(y<k)&&(y>=0)&&(a[x][y]=='0')&&compare( b,s ))break;}
if((x>=0)&&(x<k)&&(y<k)&&(y>=0)&&a[x][y]=='0'&&compare( b,s )) {jilu(b,s);
push(L,s);
if(L.top-L.base<=1) (*(L.top-1)).xuhao++;
else (*(L.top-1)).xuhao=(*(L.top-2)).xuhao+1;
}
if(L.top==L.base)
{cout<<"无法走出"<<endl;break;}
e=*(L.top-1);
}
return OK;
}
int main(){srand(time(NULL));
elemtype e;
pos b[50];
Sqstack L;
Initsqstack(L);
cout<<"请输入迷宫大小(输入整数n,将随机构成n*n的迷宫)";
int k;
cin>>k;
char**a=new char*[k];
for(int i=0;i<k;i++)
a[i]=new char[k];
for(i=0;i<k;i++)
for(int j=0;j<k;j++)
a[i][j]=goutu(rand()%2);
for(i=0;i<k;i++){
for(int j=0;j<k;j++)
cout<<a[i][j];
cout<<endl;
}
cout<<endl<<"'0' 表示通路,'#' 表示障碍物。";
cout<<endl<<"请输入起点坐标:";
cin>>e.xy.x>>e.xy.y;e.xy.x--;e.xy.y--;
move(L,e,k,a,b);pop(L,e);
if(L.base!=L.top){
cout<<"总共经过"<<e.xuhao<<"点"<<endl;
cout<<"起点("<<L.base->xy.x+1<<","<<L.base->xy.y+1<<")-->";L.base++;
while(L.top-L.base>1)
{ cout<<"("<<L.base->xy.x+1<<","<<L.base->xy.y+1<<")-->";L.base++;}
cout<<"终点("<<L.base->xy.x+1<<","<<L.base->xy.y+1<<")"<<endl;}
for(i=0;i<k;i++)
delete[] a[i];
delete[] a;
return 0;
}