#include <stdio.h> #define M 8 #define N 8 #define MaxSize 100 int mg[M+2][N+2]={ {1,1,1,1,1,1,1,1,1}, {1,0,0,0,1,1,0,0,1}, {1,0,1,0,0,0,0,1,1}, {1,0,0,1,1,1,0,1,1}, {1,1,0,0,0,1,0,1,1}, {1,0,1,1,0,1,0,0,1}, {1,0,0,1,0,0,0,1,1}, {1,0,1,0,0,1,0,0,1}, {1,0,0,1,1,0,1,0,1} }; struct { int i,j; int di; } St[MaxSize],Path[MaxSize]; int top=-1; int count=1; int minlen=MaxSize; void dispapath() { int k; printf("%5d: ",count++); for (k=0;k<=top;k++) printf("(%d,%d) ",St[k].i,St[k].j); printf("\n"); if (top+1<minlen) { for (k=0;k<=top;k++) Path[k]=St[k]; minlen=top+1; } } void dispminpath() { printf("最短路径如下:\n"); printf("长度: %d\n",minlen); printf("路径: "); for (int k=0;k<minlen;k++) printf("(%d,%d) ",Path[k].i,Path[k].j); printf("\n"); } void mgpath(int xi,int yi,int xe,int ye) { int i,j,i1,j1,di; bool find; top++; St[top].i=xi; St[top].j=yi; St[top].di=-1;mg[xi][yi]=-1; while (top>-1) { i=St[top].i;j=St[top].j;di=St[top].di; if (i==xe && j==ye) { dispapath(); mg[i][j]=0; top--; i=St[top].i;j=St[top].j; di=St[top].di; } find=false; while (di<4 && !find) { di++; switch(di) { case 0:i1=i-1; j1=j; break; case 1:i1=i; j1=j+1; break; case 2:i1=i+1; j1=j; break; case 3:i1=i, j1=j-1; break; } if (mg[i1][j1]==0) find=true; } if (find) { St[top].di=di; top++;St[top].i=i1;St[top].j=j1; St[top].di=-1; mg[i1][j1]=-1; } else { mg[i][j]=0; top--; } } dispminpath(); } int main() { printf("迷宫所有路径如下:\n"); mgpath(1,1,M,N); return 1; }帮我修改一下错误
时间: 2024-03-11 13:47:17 浏览: 26
这段代码运行时会报错,因为结构体的定义中没有给出结构体名称。需要将其定义改为:
```
struct node {
int i, j;
int di;
} St[MaxSize], Path[MaxSize];
```
此外,在 `mgpath()` 函数中,参数的命名与 `main()` 中调用时传入的参数名称不一致,需要修改为:
```
void mgpath(int x1, int y1, int x2, int y2) {
int i, j, i1, j1, di;
bool find;
top++;
St[top].i = x1;
St[top].j = y1;
St[top].di = -1;
mg[x1][y1] = -1;
while (top > -1) {
i = St[top].i;
j = St[top].j;
di = St[top].di;
if (i == x2 && j == y2) {
dispapath();
mg[i][j] = 0;
top--;
i = St[top].i;
j = St[top].j;
di = St[top].di;
}
find = false;
while (di < 4 && !find) {
di++;
switch (di) {
case 0:
i1 = i - 1;
j1 = j;
break;
case 1:
i1 = i;
j1 = j + 1;
break;
case 2:
i1 = i + 1;
j1 = j;
break;
case 3:
i1 = i;
j1 = j - 1;
break;
}
if (mg[i1][j1] == 0) find = true;
}
if (find) {
St[top].di = di;
top++;
St[top].i = i1;
St[top].j = j1;
St[top].di = -1;
mg[i1][j1] = -1;
} else {
mg[i][j] = 0;
top--;
}
}
dispminpath();
}
```
相关推荐
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)