c语言迷宫问题输出坐标,迷宫问题求解,我写的程序有问题?

本文探讨了一段用于解决迷宫问题的C++程序,该程序使用非递归方法来寻找并输出从起点到终点的路径。文章包含了完整的源代码,并通过讨论解决了程序中存在的错误。

迷宫问题求解,我写的程序有问题?

本人C才入门 我写了这段程序 但是运行不了 高手请帮忙查查错误

功能是:输入任意大小迷宫,用非递归方法求出一条走出迷宫的路径 并将路径输出

#include"linkstack.cpp"

#include"iostream.h"

struct node

{

int row;

int col;

int dir;

};

void main()

{

const int move[8][2]={{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,1}};

int row=8,col=11,start=1,end=9,i,j,k;

{

cout<

cin>>row>>col;

row+=2;col+=2;

cout<

for(i=1;i

{

cout<

for(j=1;j

cin>>maze[i][j];

}

cout<

cin>>start>>end;

for(i=0;i

maze[i][0]=maze[i][col-1]=1;

for(i=0;i

maze[0][i]=maze[row-1][i]=1;

}

cout<

for(i=0;i

{

for(j=0;j

cout<

}

cout<

linkstackstack;

node mynode;

mynode.row=1;

mynode.col=start;

mynode.dir=2;

stack.push(mynode);

int g,h,find=0;

while(!stack.isempty()&&!find)

{

stack.pop(mynode);

i=mynode.row;j=mynode.col;k=mynode.dir;

while(k<8)

{

g=i+move[k][0];

h=j+move[k][1];

if(g==row-2&&h==end&&maze[g][h]==0)

{

find=1;

maze[g][h]=2;break;

}

if(maze[g][h]==0)

{

maze[g][h]=2;

mynode.row=g;mynode.col=h;mynode.dir=k;

stack.push(mynode);

i=g;

j=h;

k=0;continue;

}

k=k+1;

}

}

if(find)

{

for(i=0;i

{

for(j=0;j

cout<

cout<

}

}

else cout<

}

搜索更多相关的解决方案:

迷宫  问题求解

----------------解决方案--------------------------------------------------------

大哥你这是C++吧?

----------------解决方案--------------------------------------------------------

对啊,是C++啊 ,帮我看看吧 有一个ERROR,我不知道为什么

----------------解决方案--------------------------------------------------------

/*用栈解迷宫*/

#include "Stdio.h"

#include "Conio.h"

#include "Stack.h"

#define N 10

typedef struct INFOR

{

int num;

int flag;

}info;

typedef struct MAZE

{

cod point;

int tag;

}maze;

maze search(info a[][N+2],int x,int y);

int main(void)

{

list_stack *s=NULL;

info arr[N+2][N+2];

FILE *fp;

int i,j;

cod cur;

InitialStack(s);

fp=fopen("MAZE.TXT","r");

if(fp==NULL)

exit(1);

for(i=0;i

for(j=0;j

for(i=1;i<=N;i++)

for(j=1;j<=N;fscanf(fp,"%d",&arr[i][j].num),j++);

fclose(fp);

cur.x=cur.y=10;

arr[10][10].flag=1;

push(s,cur);

do

{

maze var;

var=search(arr,cur.x,cur.y);

if(var.tag)

{

cur=GetTop(s);

pop(s);

}

else

{

push(s,var.point);

cur=var.point;

}

}while(cur.x!=1||cur.y!=1);

s=s->link;

while(s->link->link)

{

printf("(%d,%d)->",s->xy.x,s->xy.y);

s=s->link;

}

printf("(%d,%d)",10,10);

getch();

return 0;

}

maze search(info a[][N+2],int x,int y)

{

maze var;

/*左*/

if(a[x][y-1].num==1&&a[x][y-1].flag==0)

{

a[x][y-1].flag=1;

var.point.x=x;

var.point.y=y-1;

var.tag=0;/*表示还可继续查询*/

return var;

}

/*左上*/

if(a[x-1][y-1].num==1&&a[x-1][y-1].flag==0)

{

a[x-1][y-1].flag=1;

var.point.x=x-1;

var.point.y=y-1;

var.tag=0;/*表示还可继续查询*/

return var;

}

/*上*/

if(a[x-1][y].num==1&&a[x-1][y].flag==0)

{

a[x-1][y].flag=1;

var.point.x=x-1;

var.point.y=y;

var.tag=0;/*表示还可继续查询*/

return var;

}

/*右上*/

if(a[x-1][y+1].num==1&&a[x-1][y+1].flag==0)

{

a[x-1][y+1].flag=1;

var.point.x=x-1;

var.point.y=y+1;

var.tag=0;/*表示还可继续查询*/

return var;

}

/*右*/

if(a[x][y+1].num==1&&a[x][y+1].flag==0)

{

a[x][y+1].flag=1;

var.point.x=x;

var.point.y=y+1;

var.tag=0;/*表示还可继续查询*/

return var;

}

/*右下*/

if(a[x+1][y+1].num==1&&a[x+1][y+1].flag==0)

{

a[x+1][y+1].flag=1;

var.point.x=x+1;

var.point.y=y+1;

var.tag=0;/*表示还可继续查询*/

return var;

}

/*下*/

if(a[x+1][y].num==1&&a[x+1][y].flag==0)

{

a[x+1][y].flag=1;

var.point.x=x+1;

var.point.y=y;

var.tag=0;/*表示还可继续查询*/

return var;

}

/*左下*/

if(a[x+1][y-1].num==1&&a[x+1][y-1].flag==0)

{

a[x+1][y-1].flag=1;

var.point.x=x+1;

var.point.y=y-1;

var.tag=0;/*表示还可继续查询*/

return var;

}

var.tag=1;

return var;

}

栈定义如下:"Stack.h"

#include "Stdio.h"

#include "Conio.h"

#include "stdlib.h"

typedef struct coordinate

{

int x;

int y;

}cod;

typedef struct STACK

{

cod xy;

struct STACK *link;

}list_stack;

void DoError(int error)

{

switch(error)

{

case 1:puts("Memory out!");break;

case 2:puts("Can't find a path!");break;

default:exit(1);

}

}

list_stack *InitialStack(list_stack *stack)

{

stack=(list_stack*)malloc(sizeof(list_stack));

if(stack==NULL)

DoError(1);

stack->link=NULL;

}

void push(list_stack *stack,cod data)

{

list_stack* node=(list_stack *)malloc(sizeof(list_stack));

if(node==NULL)

DoError(1);

node->xy=data;

node->link=stack->link;

stack->link=node;

}

cod GetTop(list_stack* stack)

{

if(stack->link)

return stack->link->xy;

else DoError(2);

}

void pop(list_stack* stack)

{

list_stack* node=stack->link;

if(node==NULL)

DoError(2);

stack->link=node->link;

free(node);

}

忘了把迷宫图打了,

1 0 0 0 1 1 0 1 1 0

0 1 1 0 0 0 0 1 1 1

1 1 0 1 1 1 1 1 1 0

1 1 0 1 0 0 1 1 1 1

0 0 1 1 0 1 1 0 1 0

0 1 1 1 1 0 0 1 1 1

0 0 1 1 0 1 1 0 1 1

1 1 0 0 0 1 1 0 1 0

0 0 1 1 1 1 1 0 0 1

0 1 0 0 1 1 1 1 0 1

[此贴子已经被作者于2006-9-9 10:51:05编辑过]

----------------解决方案--------------------------------------------------------

楼上的朋友

你程序里STACK.H这个函数不能用 为什么

----------------解决方案--------------------------------------------------------

不能用吗?

我能算出答案啊

答案如下:

(1,1)->(2,2)->(2,3)->(3,4)->(3,5)->(3,6)->(4,7)->(5,6)->(5,7)->(6,8)->(7,9)->(8,

9)->(9,10)->(10,10)

----------------解决方案--------------------------------------------------------

pp13.cpp(3) : fatal error C1083: Cannot open include file: 'Stack.h': No such file or directory

执行 cl.exe 时出错.

----------------解决方案--------------------------------------------------------

你把主程序下面的stack.h存到头文件中去了吗?

----------------解决方案--------------------------------------------------------

要怎么存啊

----------------解决方案--------------------------------------------------------

#include #include #define N1 9 #define N2 8 #define T N1*N2 #define M 4 char B[N1+1][N2+1]; int count=0; //记录路径条数 typedef struct node1 { int a1; int a2; }find,direct[M+1]; typedef struct { int b1; int b2; int id; }site; typedef struct //顺序栈 { site ht[T]; int top; }Stack; void Push(Stack *s,int a,int b) { s->top++; s->ht[s->top].b1=a; s->ht[s->top].b2=b; } void Gettop(Stack * s,int *a,int *b) { *a=s->ht[s->top].b1; *b=s->ht[s->top].b2; } void create(char *a) //从文件读出迷宫(正确) { int i=0,j=0,p=1; char x; FILE *fp; fp=fopen("in.txt","r"); if(fp==NULL) { printf("文件不能打开!\n"); exit(0); } x=fgetc(fp); while(x!=EOF) { if(x=='0') { i++; a[i]=x; } if(x=='1') { i++; a[i]=x; } x=fgetc(fp); } printf(" ~~~~~~~生成迷宫~~~~~~~\n"); x=fgetc(fp); while(p<=T) //用二维数组b记录迷宫每个位置是否可行 { for(i=1;i<=N1;i++) for(j=1;j<=N2;j++) { B[i][j]=a[p]; p++; } } printf(" "); printf("■■■■■■■■■■■■\n"); printf(" ■"); printf(" ■\n"); for(i=1;i<=N1;i++) { printf(" "); printf("■ "); for(j=1;jht[s1->top].id=id; B[x][y]='*'; while(s1->top>0) { Gettop(s1,&x,&y); id=s1->ht[s1->top].id; if(x==B1&&y==B2) { count++; fprintf(fp,"%d%c%c",count,':',' '); printf("第 %d 条路径(长度为%d):\n",count,s1->top); s1->ht[s1->top].id=0; for(i=1;itop;i++) { printf("(%d,%d,%d)->",s1->ht[i].b1,s1->ht[i].b2,s1->ht[i].id); fprintf(fp,"%c%d%c%d%c%d%c%c",'(',s1->ht[i].b1,',',s1->ht[i].b2,',',s1->ht[i].id,')',' '); if(i==0) fprintf(fp,"%c%c%c%c",'\n',' ',' ',' '); if(i%8==0) printf("\n"); } fprintf(fp,"%c",'\n'); printf("结束!\n\n"); if(s1->toptop=s1->top; min=s1->top; for(i=1;itop;i++) s2->ht[i]=s1->ht[i]; } B[x][y]='0'; s1->top--; //退栈(s1->top--) Gettop(s1,&x,&y); id=s1->ht[s1->top].id; } fun=0; while(idht[s1->top].b1; y=s1->ht[s1->top].b2; x=x+p[id].a1; y=y+p[id].a2; if(x==0||y==0||x>N1||y>N2) continue; if(B[x][y]=='0') { fun=1; break; } } if(fun==1) //找到通路 { s1->ht[s1->top].id=id; Push(s1,x,y); B[x][y]='*'; s1->ht[s1->top].id=0; } else { x=s1->ht[s1->top].b1; y=s1->ht[s1->top].b2; B[x][y]='0'; s1->top--; } } if(count==0) printf(" 无路径!\n"); else { printf("\n\n\n "); printf("所有路径已存储在文件%s 中,请去查找!\n\n",filename); } return 1; } void Print(Stack *s2,char filename[]) { int i; FILE *fp; fp=fopen(filename,"a+"); if(fp==NULL) { printf("文件不能打开!\n"); exit(0); } if(count!=0) { fprintf(fp,"%s","最短路径为:"); fprintf(fp,"%c",'\n'); printf(" "); printf("%s\n","**********最短路径**********\n"); for(i=1;itop;i++) { printf("(%d,%d,%d) ->",s2->ht[i].b1,s2->ht[i].b2,s2->ht[i].id); fprintf(fp,"%c%d%c%d%c%d%c%c",'(',s2->ht[i].b1,',',s2->ht[i].b2,',',s2->ht[i].id,')',' '); if(i==0) fprintf(fp,"%c",'\n'); if(i%7==0) printf("\n"); } fprintf(fp,"%c",'\n'); printf("结束!\n"); printf("(最短路径长度: %d)\n",s2->top); } } void main() //主函数 { char a[T+1]; //二维数组b记录迷宫的每个位置 char filename1[20],filename2[20]; int x1,x2,y1,y2,k; Stack *s1,*s2; direct f1; f1[1].a1=0; f1[1].a2=1; //判断方向(右) f1[2].a1=1; f1[2].a2=0; //(下) f1[3].a1=0; f1[3].a2=-1; //(左) f1[4].a1=-1; f1[4].a2=0; //(上) s1=(Stack *)malloc(sizeof(Stack)); s2=(Stack *)malloc(sizeof(Stack)); s1->top=0; //指向栈顶(初始化栈) s2->top=0; create(a); printf("\n\n "); printf("请输入入口坐标: "); scanf("%d%d",&x1,&x2); printf(" "); printf("请输入出口坐标: "); scanf("%d%d",&y1,&y2); printf(" "); printf("请输入存储所有路径的文件名:"); scanf("%s",filename1); printf(" "); printf("请输入存储最短路径的文件名:"); scanf("%s",filename2); system("cls"); k=search(x1,x2,y1,y2,s1,s2,f1,filename1); if(k==1) Print(s2,filename2); printf("\n"); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值