#include<iostream>
#include<conio.h>
using namespace std;
#define MAXLEN 30
#define add 10
#define INISIZE 100
typedef struct{
int row;//迷宮行數
int column;//迷宮列數
int grid[MAXLEN][MAXLEN];//保存迷宮狀態
}MazeType;//迷宮型別
typedef struct{
int row;
int column;
}Coorddinate;//坐標
typedef struct{
int ord;
Coorddinate seat;
int di;
}MazeNode;//堆疊元素型別
typedef MazeNode SElemType;
typedef struct{
SElemType *base;
SElemtype *top;
int stacksize;
}Stack;
int IniStack(Stack *s){
s->base=(SElemType*)malloc(INISIZE*sizeof(SElemType));
if(!s->base)
return -1;
s->top=s->base;
s->stacksize=INISIZE;
}
int StackEmpty(Stack *s){
if(s->top==s->base)
return 1;
else
return 0;
}
int StackIsFull(Stack *s){
if(s->top-s->base>=s->stacksize)
return 1;
else
return 0;
}
int Push(Stack *s,MazeNode mn){
if(s->top-s->base>=s->stacksize){
s->base=(SElemType*)malloc((s->stacksize+add)*sizeof(SElemType));
if(!s->base)
return -1;
s->top=s->base+s->stacksize;
s->stacksize+=add;
}
*s->top++=mn;
}
int Pop(Stack *s,MazeNode *mn){
if(s->top!=s->base){
*mn=*--s->top;
return 1;
}
else
return 0;
}
int DestroyStack(Stack *s){
s->top=s->base;
return 1;
}
int MazeInit(MazeType *maze){
cout<<"請輸入迷宮的行和列:";
cin>>maze->row>>maze->column;
cout<<endl;
int i,j;
for(i=0;i<=maze->column+1;i++){
maze->grid[0][i]='1';
maze->grid[maze->row+1][i]='1';
}
for(i=0;i<maze->row;i++){
maze->grid[i][0]='1';
maze->grid[i][maze->column+1]='1';
}
cout<<"請輸入障礙的坐標(輸入坐標為(0,0)時結束輸入):"<<endl;
int m,n;
cin>>m>>n;
do{
if(m>maze->row||m<0||n>maze->column||n<0){
cout<<"輸入的坐標越界,請重新輸入:"<<endl;
continue;
}
else
maze->grid[m][n]='1';
cin>>m>>n;
}while(m==0&&n==0);
return 1;
}
int Pass(MazeType maze,Coorddinate pos){
if(maze.grid[pos.row][pos.column]=='0')
return 1;
else
return 0;
}
int MarkerPass(MazeType maze,Coorddinate pos){
maze.grid[pos.row][pos.column]='2';
return 1;
}
Coorddinate NextCoord(Coorddinate pos,int i){
switch(i){
case 1:
pos.column+=1;break;
case 2:
pos.row+=1;break;
case 3:
pos.column-=1;break;
case 4:
pos.row-=1;break;
default:
exit(0);
}
return pos;
}
int MarkerNoPass(MazeType maze,Coorddinate pos){
maze.grid[pos.row][pos.column]='3';
return 1;
}
int MazePath(MazeType *maze,Coorddinate start,Coorddinate end){
Coorddinate pos;
Stack s;
int curstep;
MazeNode e;
IniStack(&s);
pos=start;
do{
if(Pass(*maze,pos)){
MarkerPass(*maze,pos);
e.ord=curstep;
e.seat=pos;
e.di=1;
Push(&s,e);
if(pos.row==end.row&&pos.column==end.column){
DestroyStack(&s);
return 1;
}
else{
pos=NextCoord(pos,1);
curstep++;
}
}
else{
if(!StackEmpty(&s)){
Pop(&s,&e);
if(e.di==4&&!StackEmpty(&s)){
MarkerNoPass(*maze,e.seat);
Pop(&s,&e);
}
if(e.di<4){
e.di++;
Push(&s,e);
pos=NextCoord(e.seat,e.di);
}
}
}
}while(!StackEmpty(&s));
DestroyStack(&s);
return 0;
}
void PrintMaze(MazeType *maze){
int i,j;
cout<<"⊕表示通路"<<endl;
for(i=0;i<=maze->row+1;i++){
for(j=0;j<=maze->column+1;j++){
if(maze->grid[i][j]=='1')
cout<<"此次是正方形";
else if(maze->grid[i][j]=='2')
cout<<"此處是圓形";
else
cout<<" ";
}
cout<<endl;
}
}
int main(){
MazeType maze;
Coorddinate start,end;
char cmd;
cout<<"創建迷宮:"<<endl;
if(!MazeInit(&maze)){
cout<<"創建失敗!"<<endl;
return -1;
}
do{
cout<<"請輸入入口的坐標:";
cin>>start.row>>start.column;
cout<<endl;
if(start.row>maze.row||start.column>maze.column){
cout<<"輸入的坐標越界,請重新輸入:"<<endl;
continue;
}
}while(start.row>maze.row||start.column>maze.column);
do{
cout<<"請輸入出口的坐標:";
cin>>end.row>>end.column;
cout<<endl;
if(end.row>maze.row||end.column>maze.column){
cout<<"輸入的坐標越界,請重新輸入:"<<endl;
continue;
}
}while(end.row>maze.row||end.column>maze.column);
if(!MazePath(&maze,start,end))
cout<<endl<<"沒有路徑可以到達出口:"<<endl;
else
PrintMaze(&maze);
getch();
return 0;
}
一直報錯如下:
syntax error : missing ';' before '*'
I:\資料結構\工程實踐\迷宮求解.cpp(25) : error C2501: 'SElemtype' : missing storage-class or type specifiers
I:\資料結構\工程實踐\迷宮求解.cpp(25) : error C2501: 'top' : missing storage-class or type specifiers
I:\資料結構\工程實踐\迷宮求解.cpp(33) : error C2039: 'top' : is not a member of 'Stack'
I:\資料結構\工程實踐\迷宮求解.cpp(23) : see declaration of 'Stack'
I:\資料結構\工程實踐\迷宮求解.cpp(37) : error C2039: 'top' : is not a member of 'Stack'
I:\資料結構\工程實踐\迷宮求解.cpp(23) : see declaration of 'Stack'
I:\資料結構\工程實踐\迷宮求解.cpp(43) : error C2039: 'top' : is not a member of 'Stack'
I:\資料結構\工程實踐\迷宮求解.cpp(23) : see declaration of 'Stack'
I:\資料結構\工程實踐\迷宮求解.cpp(49) : error C2039: 'top' : is not a member of 'Stack'
I:\資料結構\工程實踐\迷宮求解.cpp(23) : see declaration of 'Stack'
I:\資料結構\工程實踐\迷宮求解.cpp(53) : error C2039: 'top' : is not a member of 'Stack'
I:\資料結構\工程實踐\迷宮求解.cpp(23) : see declaration of 'Stack'
I:\資料結構\工程實踐\迷宮求解.cpp(56) : error C2039: 'top' : is not a member of 'Stack'
I:\資料結構\工程實踐\迷宮求解.cpp(23) : see declaration of 'Stack'
I:\資料結構\工程實踐\迷宮求解.cpp(59) : error C2039: 'top' : is not a member of 'Stack'
I:\資料結構\工程實踐\迷宮求解.cpp(23) : see declaration of 'Stack'
I:\資料結構\工程實踐\迷宮求解.cpp(60) : error C2039: 'top' : is not a member of 'Stack'
I:\資料結構\工程實踐\迷宮求解.cpp(23) : see declaration of 'Stack'
I:\資料結構\工程實踐\迷宮求解.cpp(67) : error C2039: 'top' : is not a member of 'Stack'
I:\資料結構\工程實踐\迷宮求解.cpp(23) : see declaration of 'Stack'
執行 cl.exe 時出錯.
迷宮求解.obj - 1 error(s), 0 warning(s)
uj5u.com熱心網友回復:
由于xe10找不到d版,試用已經過期,無法給你除錯轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/85961.html
標籤:基礎類
