程序如下
#include<iostream.h>
#include <stdlib.h>
#include<conio.h>
const int StackMaxSize= 30;
struct items
{
int x,y,d;
};
typedef items ElemType;
typedef struct stack
{
ElemType data[StackMaxSize];
int top;
};
#include"Stack.h"
const M =10,N =10;
int maze[M+2][N+2];
int mark[M+2][N+2];
int move [4][2]={{0,1},{1,0},{0,-1},{-1,0}};
void Seekpath(int m,int n)
{
mark[1][1]=1;
Stack * S;
InitStack(S);
items temp;
temp.x=1;temp.y=1;temp.d=-1;
Push(S,temp);
while(!StackEmpty(s))
{
temp=pop(s);
int x=temp.x;int y=temp.y;int d=temp.d+1;
while (d<4)
{
if(x==m&&y==n)
{
cout<<m<<" "<<n<<endl;
while(!StackEmpty(s))
{temp=pop(s);
cout<<temp.x<<" "<<temp.y<<endl;
}
return;
}
int g=x+move[d][0]; int h=y+move[d][1];
if(maze[g][h]==0&&mark[g][h]==0)
{
mark[g][h]=1;
temp.x=x;temp.y=y;temp.d=d;
Push(S,temp);
x=g;y=h;d=0;
}
else
d++;
}
}
cout<<"不存在从出口到入口的通路"<<endl;
}
void main(void)
{
int i,j;
int m,n;
do{
cout<<"输入迷宫的行数m和列数n:";
cin>>m>>n;
if(m>M||n>N)
cout<<"重新输入m和n的值"<<endl;
}while(m>M||n>N);
for(i=0;i<m+2;i++)
for(j=0;j<n+2;j++)
cin>>maze[i][j];
for(i=0;i<m+2;i++)
for(j=0;j<n+2;j++)
mark[i][j]=0;
Seekpath(m,n);
}
总是说不能打开〈stack.h〉该头文件,请问是怎么回事啊?
谢谢!
12 个解决方案
#1
有这个文件吗?
stack.h 自己写的吧。
stack.h 自己写的吧。
#2
stack.h??感觉上是一个写了一些栈运算的头文件,包括StackEmpty(*Stack)这个判断栈是否为空的函数
#3
#include <stack>
using namespace std;
stack<int> S;
S.push(temp);
temp=S.top();
S.pop();
using namespace std;
stack<int> S;
S.push(temp);
temp=S.top();
S.pop();
#4
place the file stack.h in the same directory with your .c file.
#5
文件路径问题吧
#6
超超,什么意思啊?我是新手,不太明白你的意思,说的具体一点好吧!谢谢!
#7
各位大峡,可以帮忙具体改一下吗?
小生实在是研究不出来了!
谢谢!非常!
小生实在是研究不出来了!
谢谢!非常!
#8
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#define RANGE 12
#define true 1
#define false 0
#define OK 1
#define ERROR 0
int leftup=218;/*左上角┌*/
int rightup=191;/*右上角┐*/
int leftdn=192;/*左下角└*/
int rightdn=217;/*右下角┘*/
int linehc=196;/*横线─*/
int linevc=179;/*竖线│*/
int midmid=197;/* ┼*/
int leftmid=195;/* ├*/
int upmid=194; /*┬*/
int rightmid=180; /*┤*/
int dnmid=193; /*┴*/
typedef int DirectiveType;
typedef int Status;
typedef int bool;
typedef struct
{
int x,y;
}PosType;/*坐标位置类型*/
typedef struct
{
int m,n;
char arr[RANGE][RANGE];
}MazeType;/*迷宫类型*/
typedef struct
{
int step;
PosType seat;
DirectiveType di;
}ElemType;/*栈元素类型*/
typedef struct LNode
{
ElemType data;
struct LNode * next;
}NodeType,* LinkType;
typedef struct
{
LinkType top;
int size;
}Stack;
/*函数声明,应该将所有定义的函数都声明*/
void InitMaze(MazeType * M,int a[RANGE][RANGE],int r,int c);
void PrintMaze(MazeType * M);
/*全局变量定义*/
Stack s;
void InitStack(Stack *S)
/*初始化栈,设栈为空栈*/
{
S->top=NULL;
S->size=0;
}
int StackLength(Stack S)
/*取栈的长度*/
{
return S.size;
}
bool StackEmpty(Stack S)
{
if(S.top==NULL)
return true;
else
return false;
}
Status GetTop(Stack S,ElemType * e)
{
if(StackEmpty(S)) return ERROR;
*e=(S.top)->data;
return OK;
}
Status Pop(Stack *S,ElemType *e)
{
LinkType p;
if(StackEmpty(*S)) return ERROR;
*e=(S->top)->data;
p=S->top;
S->top=(S->top)->next;
free(p);
(S->size)--;
return OK;
}
Status Push(Stack *S,ElemType e)
/*出栈*/
{
LinkType p;
p=(LinkType)malloc(sizeof(NodeType));
if(!p)
{
printf("error!\n");
return ERROR;
}
p->data=e;
if(S->top==NULL)
p->next=NULL;
else
p->next=S->top;
S->top=p;
S->size++;
return OK;
}
bool Pass(MazeType *M,PosType pos)
/*检查是否为空格*/
{
if(M->arr[pos.x][pos.y]==' ')
return true;
else
return false;
}
void FootPrint(MazeType *M,PosType pos)
/*将该位置标记已走*/
{
M->arr[pos.x][pos.y]='*';
}
void MarkPrint(MazeType *M,PosType pos)
/*将该位置标记为死胡同*/
{
M->arr[pos.x][pos.y]='@';
}
bool Same(PosType p1,PosType p2)
/*测试两个坐标是否相同*/
{
if(p1.x==p2.x&&p1.y==p2.y)
return true;
else
return false;
}
PosType NextPos(PosType pos,DirectiveType i)
/*求当前位置的下一个探索位置*/
{
switch(i)
{
case 1:
pos.y++;
break;
case 2:
pos.x++;
break;
case 3:
pos.y--;
break;
case 4:
pos.x--;
break;
}
return pos;
}
bool MazePath(MazeType *M,PosType start,PosType end)
{
ElemType e;/**/
PosType curpos=start;
int curstep=1;
Status found=false;
InitStack(&s);
do
{
if(Pass(M,curpos))
{
FootPrint(M,curpos);
e.step=curstep;
e.seat=curpos;
e.di=1;
Push(&s,e);
if(Same(curpos,end)) return true;
else
{
curpos=NextPos(curpos,e.di);
curstep++;
}
}
else
{
if(!StackEmpty(s))
{
Pop(&s,&e);
while(e.di==4&&!StackEmpty(s))
{
MarkPrint(M,e.seat);
Pop(&s,&e);
curstep--;
}
if(e.di<4)
{
e.di++;
Push(&s,e);
curpos=NextPos(e.seat,e.di);
}
}
}
}while(!StackEmpty(s)&&!found);
return found;
}
void main()
{
FILE *fp;
MazeType ma;
ElemType e;
PosType from,to;
int i,j,m,n,a[RANGE][RANGE];
int status;
char filename[80]="d:\\tc\\ds\\m2.dat";
printf("Please Input the data filename:\n");
gets(filename);
printf("Please input the start and the end:\n");
scanf("%d %d",&from.x,&from.y);
scanf("%d %d",&to.x,&to.y);
fp=fopen(filename,"r");
if(!fp)
{
printf("can not open the data file!\n");
exit(-1);
}
fscanf(fp,"%d %d",&m,&n);
printf("m=%d,n=%d\n",m,n);
for(i=0;i<m+2;i++)
{
for(j=0;j<n+2;j++)
{
if(i==0||j==0||i==m+1||j==n+1)
{
a[i][j]=1;
printf("%d ",a[i][j]);
continue;
}
else
{
fscanf(fp,"%d",&a[i][j]);
printf("%d ",a[i][j]);
}
}
printf("\n");
}
fclose(fp);
printf("\n");
InitMaze(&ma,a,m,n);
printf("\n");
TablePrint(&ma);
printf("\n");
TablePrint(&ma);
printf("\n");
status=MazePath(&ma,from,to);
if(status==false)
printf("can not find a path!\n");
else
TablePrint(&ma);
}
void InitMaze(MazeType * M,int a[RANGE][RANGE],int r,int c)
{
int i,j;
M->m=r;
M->n=c;
for(i=0;i<M->m+2;i++)
for(j=0;j<M->n+2;j++)
if(a[i][j]) M->arr[i][j]='#';
else M->arr[i][j]=' ';
}
/*void PrintMaze(MazeType * M)
{
int i,j;
for(i=0;i<M->m+2;i++)
{
putchar(leftup);
for(j=1;j<M->n+1;j++)
{
printf("\xa9\xc4\xc2");
putchar
printf("\n");
for(j=0;j<M->n+2;j++)
printf("\xb3%c\xb3",M->arr[i][j]);
printf("\n");
}
}
*/
void TablePrint(MazeType * M)
{
int i,j,m=M->m,n=M->n;
for(i=1;i<=m;i++)
{
if(i==1)
{
printf("%c%c",leftup,linehc);
for(j=2;j<=n;j++)
printf("%c%c",upmid,linehc);
printf("%c\n",rightup);
for(j=1;j<=n;j++)
printf("%c%c",linevc,M->arr[i][j]);
printf("%c\n",linevc);
}
else
{
printf("%c%c",leftmid,linehc);
for(j=2;j<=n;j++)
printf("%c%c",midmid,linehc);
printf("%c\n",rightmid);
for(j=1;j<=n;j++)
printf("%c%c",linevc,M->arr[i][j]);
printf("%c\n",linevc);
}
}
printf("%c%c",leftdn,linehc);
for(j=2;j<=n;j++)
printf("%c%c",dnmid,linehc);
printf("%c\n",rightdn);
}
注意,你要按严老师的要求
生成迷宫数据文件
在运行时输入文件的全名(含目录)
并指定起点和终点
如
d:\tc\ds\m2.dat
起点是
1 1
终点是
3 4
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#define RANGE 12
#define true 1
#define false 0
#define OK 1
#define ERROR 0
int leftup=218;/*左上角┌*/
int rightup=191;/*右上角┐*/
int leftdn=192;/*左下角└*/
int rightdn=217;/*右下角┘*/
int linehc=196;/*横线─*/
int linevc=179;/*竖线│*/
int midmid=197;/* ┼*/
int leftmid=195;/* ├*/
int upmid=194; /*┬*/
int rightmid=180; /*┤*/
int dnmid=193; /*┴*/
typedef int DirectiveType;
typedef int Status;
typedef int bool;
typedef struct
{
int x,y;
}PosType;/*坐标位置类型*/
typedef struct
{
int m,n;
char arr[RANGE][RANGE];
}MazeType;/*迷宫类型*/
typedef struct
{
int step;
PosType seat;
DirectiveType di;
}ElemType;/*栈元素类型*/
typedef struct LNode
{
ElemType data;
struct LNode * next;
}NodeType,* LinkType;
typedef struct
{
LinkType top;
int size;
}Stack;
/*函数声明,应该将所有定义的函数都声明*/
void InitMaze(MazeType * M,int a[RANGE][RANGE],int r,int c);
void PrintMaze(MazeType * M);
/*全局变量定义*/
Stack s;
void InitStack(Stack *S)
/*初始化栈,设栈为空栈*/
{
S->top=NULL;
S->size=0;
}
int StackLength(Stack S)
/*取栈的长度*/
{
return S.size;
}
bool StackEmpty(Stack S)
{
if(S.top==NULL)
return true;
else
return false;
}
Status GetTop(Stack S,ElemType * e)
{
if(StackEmpty(S)) return ERROR;
*e=(S.top)->data;
return OK;
}
Status Pop(Stack *S,ElemType *e)
{
LinkType p;
if(StackEmpty(*S)) return ERROR;
*e=(S->top)->data;
p=S->top;
S->top=(S->top)->next;
free(p);
(S->size)--;
return OK;
}
Status Push(Stack *S,ElemType e)
/*出栈*/
{
LinkType p;
p=(LinkType)malloc(sizeof(NodeType));
if(!p)
{
printf("error!\n");
return ERROR;
}
p->data=e;
if(S->top==NULL)
p->next=NULL;
else
p->next=S->top;
S->top=p;
S->size++;
return OK;
}
bool Pass(MazeType *M,PosType pos)
/*检查是否为空格*/
{
if(M->arr[pos.x][pos.y]==' ')
return true;
else
return false;
}
void FootPrint(MazeType *M,PosType pos)
/*将该位置标记已走*/
{
M->arr[pos.x][pos.y]='*';
}
void MarkPrint(MazeType *M,PosType pos)
/*将该位置标记为死胡同*/
{
M->arr[pos.x][pos.y]='@';
}
bool Same(PosType p1,PosType p2)
/*测试两个坐标是否相同*/
{
if(p1.x==p2.x&&p1.y==p2.y)
return true;
else
return false;
}
PosType NextPos(PosType pos,DirectiveType i)
/*求当前位置的下一个探索位置*/
{
switch(i)
{
case 1:
pos.y++;
break;
case 2:
pos.x++;
break;
case 3:
pos.y--;
break;
case 4:
pos.x--;
break;
}
return pos;
}
bool MazePath(MazeType *M,PosType start,PosType end)
{
ElemType e;/**/
PosType curpos=start;
int curstep=1;
Status found=false;
InitStack(&s);
do
{
if(Pass(M,curpos))
{
FootPrint(M,curpos);
e.step=curstep;
e.seat=curpos;
e.di=1;
Push(&s,e);
if(Same(curpos,end)) return true;
else
{
curpos=NextPos(curpos,e.di);
curstep++;
}
}
else
{
if(!StackEmpty(s))
{
Pop(&s,&e);
while(e.di==4&&!StackEmpty(s))
{
MarkPrint(M,e.seat);
Pop(&s,&e);
curstep--;
}
if(e.di<4)
{
e.di++;
Push(&s,e);
curpos=NextPos(e.seat,e.di);
}
}
}
}while(!StackEmpty(s)&&!found);
return found;
}
void main()
{
FILE *fp;
MazeType ma;
ElemType e;
PosType from,to;
int i,j,m,n,a[RANGE][RANGE];
int status;
char filename[80]="d:\\tc\\ds\\m2.dat";
printf("Please Input the data filename:\n");
gets(filename);
printf("Please input the start and the end:\n");
scanf("%d %d",&from.x,&from.y);
scanf("%d %d",&to.x,&to.y);
fp=fopen(filename,"r");
if(!fp)
{
printf("can not open the data file!\n");
exit(-1);
}
fscanf(fp,"%d %d",&m,&n);
printf("m=%d,n=%d\n",m,n);
for(i=0;i<m+2;i++)
{
for(j=0;j<n+2;j++)
{
if(i==0||j==0||i==m+1||j==n+1)
{
a[i][j]=1;
printf("%d ",a[i][j]);
continue;
}
else
{
fscanf(fp,"%d",&a[i][j]);
printf("%d ",a[i][j]);
}
}
printf("\n");
}
fclose(fp);
printf("\n");
InitMaze(&ma,a,m,n);
printf("\n");
TablePrint(&ma);
printf("\n");
TablePrint(&ma);
printf("\n");
status=MazePath(&ma,from,to);
if(status==false)
printf("can not find a path!\n");
else
TablePrint(&ma);
}
void InitMaze(MazeType * M,int a[RANGE][RANGE],int r,int c)
{
int i,j;
M->m=r;
M->n=c;
for(i=0;i<M->m+2;i++)
for(j=0;j<M->n+2;j++)
if(a[i][j]) M->arr[i][j]='#';
else M->arr[i][j]=' ';
}
/*void PrintMaze(MazeType * M)
{
int i,j;
for(i=0;i<M->m+2;i++)
{
putchar(leftup);
for(j=1;j<M->n+1;j++)
{
printf("\xa9\xc4\xc2");
putchar
printf("\n");
for(j=0;j<M->n+2;j++)
printf("\xb3%c\xb3",M->arr[i][j]);
printf("\n");
}
}
*/
void TablePrint(MazeType * M)
{
int i,j,m=M->m,n=M->n;
for(i=1;i<=m;i++)
{
if(i==1)
{
printf("%c%c",leftup,linehc);
for(j=2;j<=n;j++)
printf("%c%c",upmid,linehc);
printf("%c\n",rightup);
for(j=1;j<=n;j++)
printf("%c%c",linevc,M->arr[i][j]);
printf("%c\n",linevc);
}
else
{
printf("%c%c",leftmid,linehc);
for(j=2;j<=n;j++)
printf("%c%c",midmid,linehc);
printf("%c\n",rightmid);
for(j=1;j<=n;j++)
printf("%c%c",linevc,M->arr[i][j]);
printf("%c\n",linevc);
}
}
printf("%c%c",leftdn,linehc);
for(j=2;j<=n;j++)
printf("%c%c",dnmid,linehc);
printf("%c\n",rightdn);
}
注意,你要按严老师的要求
生成迷宫数据文件
在运行时输入文件的全名(含目录)
并指定起点和终点
如
d:\tc\ds\m2.dat
起点是
1 1
终点是
3 4
#9
前阵子写的,参考一下
#10
谢谢,程序好长哦,你好强呀,不知道到我要多久才能写出这么复杂的程序,小张,可以看一下我的程序吗,我想知道错在那里,再谢。
#11
就是超超写的那样啊,用标准c++,那样可以包含stack的类
#12
#include<iostream>
#include<stdlib.h>
#include<conio.h>
#include<stack>
using namespace std;
struct items
{
int x,y,d;
};
typedef items ElemType;
const M =10,N =10;
int maze[M+2][N+2];
int mark[M+2][N+2];
int move [4][2]={{0,1},{1,0},{0,-1},{-1,0}};
void Seekpath(int m,int n)
{
mark[1][1]=1;
stack<ElemType> S;
struct items temp;
temp.x=1;temp.y=1;temp.d=-1;
S.push(temp);
while(!S.empty())
{
temp=S.top(); S.pop();
int x=temp.x;int y=temp.y;int d=temp.d+1;
while (d<4)
{
if(x==m&&y==n)
{
cout<<m<<" "<<n<<endl;
while(!S.empty())
{temp=S.top(); S.pop();
cout<<temp.x<<" "<<temp.y<<endl;
}
return;
}
int g=x+move[d][0]; int h=y+move[d][1];
if(maze[g][h]==0&&mark[g][h]==0)
{
mark[g][h]=1;
temp.x=x;temp.y=y;temp.d=d;
S.push(temp);
x=g;y=h;d=0;
}
else
d++;
}
}
cout<<"不存在从出口到入口的通路"<<endl;
}
... ...
#include<stdlib.h>
#include<conio.h>
#include<stack>
using namespace std;
struct items
{
int x,y,d;
};
typedef items ElemType;
const M =10,N =10;
int maze[M+2][N+2];
int mark[M+2][N+2];
int move [4][2]={{0,1},{1,0},{0,-1},{-1,0}};
void Seekpath(int m,int n)
{
mark[1][1]=1;
stack<ElemType> S;
struct items temp;
temp.x=1;temp.y=1;temp.d=-1;
S.push(temp);
while(!S.empty())
{
temp=S.top(); S.pop();
int x=temp.x;int y=temp.y;int d=temp.d+1;
while (d<4)
{
if(x==m&&y==n)
{
cout<<m<<" "<<n<<endl;
while(!S.empty())
{temp=S.top(); S.pop();
cout<<temp.x<<" "<<temp.y<<endl;
}
return;
}
int g=x+move[d][0]; int h=y+move[d][1];
if(maze[g][h]==0&&mark[g][h]==0)
{
mark[g][h]=1;
temp.x=x;temp.y=y;temp.d=d;
S.push(temp);
x=g;y=h;d=0;
}
else
d++;
}
}
cout<<"不存在从出口到入口的通路"<<endl;
}
... ...
#1
有这个文件吗?
stack.h 自己写的吧。
stack.h 自己写的吧。
#2
stack.h??感觉上是一个写了一些栈运算的头文件,包括StackEmpty(*Stack)这个判断栈是否为空的函数
#3
#include <stack>
using namespace std;
stack<int> S;
S.push(temp);
temp=S.top();
S.pop();
using namespace std;
stack<int> S;
S.push(temp);
temp=S.top();
S.pop();
#4
place the file stack.h in the same directory with your .c file.
#5
文件路径问题吧
#6
超超,什么意思啊?我是新手,不太明白你的意思,说的具体一点好吧!谢谢!
#7
各位大峡,可以帮忙具体改一下吗?
小生实在是研究不出来了!
谢谢!非常!
小生实在是研究不出来了!
谢谢!非常!
#8
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#define RANGE 12
#define true 1
#define false 0
#define OK 1
#define ERROR 0
int leftup=218;/*左上角┌*/
int rightup=191;/*右上角┐*/
int leftdn=192;/*左下角└*/
int rightdn=217;/*右下角┘*/
int linehc=196;/*横线─*/
int linevc=179;/*竖线│*/
int midmid=197;/* ┼*/
int leftmid=195;/* ├*/
int upmid=194; /*┬*/
int rightmid=180; /*┤*/
int dnmid=193; /*┴*/
typedef int DirectiveType;
typedef int Status;
typedef int bool;
typedef struct
{
int x,y;
}PosType;/*坐标位置类型*/
typedef struct
{
int m,n;
char arr[RANGE][RANGE];
}MazeType;/*迷宫类型*/
typedef struct
{
int step;
PosType seat;
DirectiveType di;
}ElemType;/*栈元素类型*/
typedef struct LNode
{
ElemType data;
struct LNode * next;
}NodeType,* LinkType;
typedef struct
{
LinkType top;
int size;
}Stack;
/*函数声明,应该将所有定义的函数都声明*/
void InitMaze(MazeType * M,int a[RANGE][RANGE],int r,int c);
void PrintMaze(MazeType * M);
/*全局变量定义*/
Stack s;
void InitStack(Stack *S)
/*初始化栈,设栈为空栈*/
{
S->top=NULL;
S->size=0;
}
int StackLength(Stack S)
/*取栈的长度*/
{
return S.size;
}
bool StackEmpty(Stack S)
{
if(S.top==NULL)
return true;
else
return false;
}
Status GetTop(Stack S,ElemType * e)
{
if(StackEmpty(S)) return ERROR;
*e=(S.top)->data;
return OK;
}
Status Pop(Stack *S,ElemType *e)
{
LinkType p;
if(StackEmpty(*S)) return ERROR;
*e=(S->top)->data;
p=S->top;
S->top=(S->top)->next;
free(p);
(S->size)--;
return OK;
}
Status Push(Stack *S,ElemType e)
/*出栈*/
{
LinkType p;
p=(LinkType)malloc(sizeof(NodeType));
if(!p)
{
printf("error!\n");
return ERROR;
}
p->data=e;
if(S->top==NULL)
p->next=NULL;
else
p->next=S->top;
S->top=p;
S->size++;
return OK;
}
bool Pass(MazeType *M,PosType pos)
/*检查是否为空格*/
{
if(M->arr[pos.x][pos.y]==' ')
return true;
else
return false;
}
void FootPrint(MazeType *M,PosType pos)
/*将该位置标记已走*/
{
M->arr[pos.x][pos.y]='*';
}
void MarkPrint(MazeType *M,PosType pos)
/*将该位置标记为死胡同*/
{
M->arr[pos.x][pos.y]='@';
}
bool Same(PosType p1,PosType p2)
/*测试两个坐标是否相同*/
{
if(p1.x==p2.x&&p1.y==p2.y)
return true;
else
return false;
}
PosType NextPos(PosType pos,DirectiveType i)
/*求当前位置的下一个探索位置*/
{
switch(i)
{
case 1:
pos.y++;
break;
case 2:
pos.x++;
break;
case 3:
pos.y--;
break;
case 4:
pos.x--;
break;
}
return pos;
}
bool MazePath(MazeType *M,PosType start,PosType end)
{
ElemType e;/**/
PosType curpos=start;
int curstep=1;
Status found=false;
InitStack(&s);
do
{
if(Pass(M,curpos))
{
FootPrint(M,curpos);
e.step=curstep;
e.seat=curpos;
e.di=1;
Push(&s,e);
if(Same(curpos,end)) return true;
else
{
curpos=NextPos(curpos,e.di);
curstep++;
}
}
else
{
if(!StackEmpty(s))
{
Pop(&s,&e);
while(e.di==4&&!StackEmpty(s))
{
MarkPrint(M,e.seat);
Pop(&s,&e);
curstep--;
}
if(e.di<4)
{
e.di++;
Push(&s,e);
curpos=NextPos(e.seat,e.di);
}
}
}
}while(!StackEmpty(s)&&!found);
return found;
}
void main()
{
FILE *fp;
MazeType ma;
ElemType e;
PosType from,to;
int i,j,m,n,a[RANGE][RANGE];
int status;
char filename[80]="d:\\tc\\ds\\m2.dat";
printf("Please Input the data filename:\n");
gets(filename);
printf("Please input the start and the end:\n");
scanf("%d %d",&from.x,&from.y);
scanf("%d %d",&to.x,&to.y);
fp=fopen(filename,"r");
if(!fp)
{
printf("can not open the data file!\n");
exit(-1);
}
fscanf(fp,"%d %d",&m,&n);
printf("m=%d,n=%d\n",m,n);
for(i=0;i<m+2;i++)
{
for(j=0;j<n+2;j++)
{
if(i==0||j==0||i==m+1||j==n+1)
{
a[i][j]=1;
printf("%d ",a[i][j]);
continue;
}
else
{
fscanf(fp,"%d",&a[i][j]);
printf("%d ",a[i][j]);
}
}
printf("\n");
}
fclose(fp);
printf("\n");
InitMaze(&ma,a,m,n);
printf("\n");
TablePrint(&ma);
printf("\n");
TablePrint(&ma);
printf("\n");
status=MazePath(&ma,from,to);
if(status==false)
printf("can not find a path!\n");
else
TablePrint(&ma);
}
void InitMaze(MazeType * M,int a[RANGE][RANGE],int r,int c)
{
int i,j;
M->m=r;
M->n=c;
for(i=0;i<M->m+2;i++)
for(j=0;j<M->n+2;j++)
if(a[i][j]) M->arr[i][j]='#';
else M->arr[i][j]=' ';
}
/*void PrintMaze(MazeType * M)
{
int i,j;
for(i=0;i<M->m+2;i++)
{
putchar(leftup);
for(j=1;j<M->n+1;j++)
{
printf("\xa9\xc4\xc2");
putchar
printf("\n");
for(j=0;j<M->n+2;j++)
printf("\xb3%c\xb3",M->arr[i][j]);
printf("\n");
}
}
*/
void TablePrint(MazeType * M)
{
int i,j,m=M->m,n=M->n;
for(i=1;i<=m;i++)
{
if(i==1)
{
printf("%c%c",leftup,linehc);
for(j=2;j<=n;j++)
printf("%c%c",upmid,linehc);
printf("%c\n",rightup);
for(j=1;j<=n;j++)
printf("%c%c",linevc,M->arr[i][j]);
printf("%c\n",linevc);
}
else
{
printf("%c%c",leftmid,linehc);
for(j=2;j<=n;j++)
printf("%c%c",midmid,linehc);
printf("%c\n",rightmid);
for(j=1;j<=n;j++)
printf("%c%c",linevc,M->arr[i][j]);
printf("%c\n",linevc);
}
}
printf("%c%c",leftdn,linehc);
for(j=2;j<=n;j++)
printf("%c%c",dnmid,linehc);
printf("%c\n",rightdn);
}
注意,你要按严老师的要求
生成迷宫数据文件
在运行时输入文件的全名(含目录)
并指定起点和终点
如
d:\tc\ds\m2.dat
起点是
1 1
终点是
3 4
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#define RANGE 12
#define true 1
#define false 0
#define OK 1
#define ERROR 0
int leftup=218;/*左上角┌*/
int rightup=191;/*右上角┐*/
int leftdn=192;/*左下角└*/
int rightdn=217;/*右下角┘*/
int linehc=196;/*横线─*/
int linevc=179;/*竖线│*/
int midmid=197;/* ┼*/
int leftmid=195;/* ├*/
int upmid=194; /*┬*/
int rightmid=180; /*┤*/
int dnmid=193; /*┴*/
typedef int DirectiveType;
typedef int Status;
typedef int bool;
typedef struct
{
int x,y;
}PosType;/*坐标位置类型*/
typedef struct
{
int m,n;
char arr[RANGE][RANGE];
}MazeType;/*迷宫类型*/
typedef struct
{
int step;
PosType seat;
DirectiveType di;
}ElemType;/*栈元素类型*/
typedef struct LNode
{
ElemType data;
struct LNode * next;
}NodeType,* LinkType;
typedef struct
{
LinkType top;
int size;
}Stack;
/*函数声明,应该将所有定义的函数都声明*/
void InitMaze(MazeType * M,int a[RANGE][RANGE],int r,int c);
void PrintMaze(MazeType * M);
/*全局变量定义*/
Stack s;
void InitStack(Stack *S)
/*初始化栈,设栈为空栈*/
{
S->top=NULL;
S->size=0;
}
int StackLength(Stack S)
/*取栈的长度*/
{
return S.size;
}
bool StackEmpty(Stack S)
{
if(S.top==NULL)
return true;
else
return false;
}
Status GetTop(Stack S,ElemType * e)
{
if(StackEmpty(S)) return ERROR;
*e=(S.top)->data;
return OK;
}
Status Pop(Stack *S,ElemType *e)
{
LinkType p;
if(StackEmpty(*S)) return ERROR;
*e=(S->top)->data;
p=S->top;
S->top=(S->top)->next;
free(p);
(S->size)--;
return OK;
}
Status Push(Stack *S,ElemType e)
/*出栈*/
{
LinkType p;
p=(LinkType)malloc(sizeof(NodeType));
if(!p)
{
printf("error!\n");
return ERROR;
}
p->data=e;
if(S->top==NULL)
p->next=NULL;
else
p->next=S->top;
S->top=p;
S->size++;
return OK;
}
bool Pass(MazeType *M,PosType pos)
/*检查是否为空格*/
{
if(M->arr[pos.x][pos.y]==' ')
return true;
else
return false;
}
void FootPrint(MazeType *M,PosType pos)
/*将该位置标记已走*/
{
M->arr[pos.x][pos.y]='*';
}
void MarkPrint(MazeType *M,PosType pos)
/*将该位置标记为死胡同*/
{
M->arr[pos.x][pos.y]='@';
}
bool Same(PosType p1,PosType p2)
/*测试两个坐标是否相同*/
{
if(p1.x==p2.x&&p1.y==p2.y)
return true;
else
return false;
}
PosType NextPos(PosType pos,DirectiveType i)
/*求当前位置的下一个探索位置*/
{
switch(i)
{
case 1:
pos.y++;
break;
case 2:
pos.x++;
break;
case 3:
pos.y--;
break;
case 4:
pos.x--;
break;
}
return pos;
}
bool MazePath(MazeType *M,PosType start,PosType end)
{
ElemType e;/**/
PosType curpos=start;
int curstep=1;
Status found=false;
InitStack(&s);
do
{
if(Pass(M,curpos))
{
FootPrint(M,curpos);
e.step=curstep;
e.seat=curpos;
e.di=1;
Push(&s,e);
if(Same(curpos,end)) return true;
else
{
curpos=NextPos(curpos,e.di);
curstep++;
}
}
else
{
if(!StackEmpty(s))
{
Pop(&s,&e);
while(e.di==4&&!StackEmpty(s))
{
MarkPrint(M,e.seat);
Pop(&s,&e);
curstep--;
}
if(e.di<4)
{
e.di++;
Push(&s,e);
curpos=NextPos(e.seat,e.di);
}
}
}
}while(!StackEmpty(s)&&!found);
return found;
}
void main()
{
FILE *fp;
MazeType ma;
ElemType e;
PosType from,to;
int i,j,m,n,a[RANGE][RANGE];
int status;
char filename[80]="d:\\tc\\ds\\m2.dat";
printf("Please Input the data filename:\n");
gets(filename);
printf("Please input the start and the end:\n");
scanf("%d %d",&from.x,&from.y);
scanf("%d %d",&to.x,&to.y);
fp=fopen(filename,"r");
if(!fp)
{
printf("can not open the data file!\n");
exit(-1);
}
fscanf(fp,"%d %d",&m,&n);
printf("m=%d,n=%d\n",m,n);
for(i=0;i<m+2;i++)
{
for(j=0;j<n+2;j++)
{
if(i==0||j==0||i==m+1||j==n+1)
{
a[i][j]=1;
printf("%d ",a[i][j]);
continue;
}
else
{
fscanf(fp,"%d",&a[i][j]);
printf("%d ",a[i][j]);
}
}
printf("\n");
}
fclose(fp);
printf("\n");
InitMaze(&ma,a,m,n);
printf("\n");
TablePrint(&ma);
printf("\n");
TablePrint(&ma);
printf("\n");
status=MazePath(&ma,from,to);
if(status==false)
printf("can not find a path!\n");
else
TablePrint(&ma);
}
void InitMaze(MazeType * M,int a[RANGE][RANGE],int r,int c)
{
int i,j;
M->m=r;
M->n=c;
for(i=0;i<M->m+2;i++)
for(j=0;j<M->n+2;j++)
if(a[i][j]) M->arr[i][j]='#';
else M->arr[i][j]=' ';
}
/*void PrintMaze(MazeType * M)
{
int i,j;
for(i=0;i<M->m+2;i++)
{
putchar(leftup);
for(j=1;j<M->n+1;j++)
{
printf("\xa9\xc4\xc2");
putchar
printf("\n");
for(j=0;j<M->n+2;j++)
printf("\xb3%c\xb3",M->arr[i][j]);
printf("\n");
}
}
*/
void TablePrint(MazeType * M)
{
int i,j,m=M->m,n=M->n;
for(i=1;i<=m;i++)
{
if(i==1)
{
printf("%c%c",leftup,linehc);
for(j=2;j<=n;j++)
printf("%c%c",upmid,linehc);
printf("%c\n",rightup);
for(j=1;j<=n;j++)
printf("%c%c",linevc,M->arr[i][j]);
printf("%c\n",linevc);
}
else
{
printf("%c%c",leftmid,linehc);
for(j=2;j<=n;j++)
printf("%c%c",midmid,linehc);
printf("%c\n",rightmid);
for(j=1;j<=n;j++)
printf("%c%c",linevc,M->arr[i][j]);
printf("%c\n",linevc);
}
}
printf("%c%c",leftdn,linehc);
for(j=2;j<=n;j++)
printf("%c%c",dnmid,linehc);
printf("%c\n",rightdn);
}
注意,你要按严老师的要求
生成迷宫数据文件
在运行时输入文件的全名(含目录)
并指定起点和终点
如
d:\tc\ds\m2.dat
起点是
1 1
终点是
3 4
#9
前阵子写的,参考一下
#10
谢谢,程序好长哦,你好强呀,不知道到我要多久才能写出这么复杂的程序,小张,可以看一下我的程序吗,我想知道错在那里,再谢。
#11
就是超超写的那样啊,用标准c++,那样可以包含stack的类
#12
#include<iostream>
#include<stdlib.h>
#include<conio.h>
#include<stack>
using namespace std;
struct items
{
int x,y,d;
};
typedef items ElemType;
const M =10,N =10;
int maze[M+2][N+2];
int mark[M+2][N+2];
int move [4][2]={{0,1},{1,0},{0,-1},{-1,0}};
void Seekpath(int m,int n)
{
mark[1][1]=1;
stack<ElemType> S;
struct items temp;
temp.x=1;temp.y=1;temp.d=-1;
S.push(temp);
while(!S.empty())
{
temp=S.top(); S.pop();
int x=temp.x;int y=temp.y;int d=temp.d+1;
while (d<4)
{
if(x==m&&y==n)
{
cout<<m<<" "<<n<<endl;
while(!S.empty())
{temp=S.top(); S.pop();
cout<<temp.x<<" "<<temp.y<<endl;
}
return;
}
int g=x+move[d][0]; int h=y+move[d][1];
if(maze[g][h]==0&&mark[g][h]==0)
{
mark[g][h]=1;
temp.x=x;temp.y=y;temp.d=d;
S.push(temp);
x=g;y=h;d=0;
}
else
d++;
}
}
cout<<"不存在从出口到入口的通路"<<endl;
}
... ...
#include<stdlib.h>
#include<conio.h>
#include<stack>
using namespace std;
struct items
{
int x,y,d;
};
typedef items ElemType;
const M =10,N =10;
int maze[M+2][N+2];
int mark[M+2][N+2];
int move [4][2]={{0,1},{1,0},{0,-1},{-1,0}};
void Seekpath(int m,int n)
{
mark[1][1]=1;
stack<ElemType> S;
struct items temp;
temp.x=1;temp.y=1;temp.d=-1;
S.push(temp);
while(!S.empty())
{
temp=S.top(); S.pop();
int x=temp.x;int y=temp.y;int d=temp.d+1;
while (d<4)
{
if(x==m&&y==n)
{
cout<<m<<" "<<n<<endl;
while(!S.empty())
{temp=S.top(); S.pop();
cout<<temp.x<<" "<<temp.y<<endl;
}
return;
}
int g=x+move[d][0]; int h=y+move[d][1];
if(maze[g][h]==0&&mark[g][h]==0)
{
mark[g][h]=1;
temp.x=x;temp.y=y;temp.d=d;
S.push(temp);
x=g;y=h;d=0;
}
else
d++;
}
}
cout<<"不存在从出口到入口的通路"<<endl;
}
... ...