将代码改正后,发给我
时间: 2023-12-08 08:04:09 浏览: 165
以下是修改后的代码:
```c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define max_stack 1000
typedef struct xy {
char d;
int x, y;
} xy;
typedef struct maze_Stack {
int top;
xy stack[max_stack];
} Mstack;
//栈空
int Empty_stack(Mstack *s) {
if (s->top == -1)
return 1;
else
return 0;
}
//栈满
int Full_stack(Mstack *s) {
if (s->top + 1 == max_stack)
return 1;
else
return 0;
}
//入栈
int Push_stack(Mstack *s, xy temp) {
if (Full_stack(s))
return 0;
else {
s->top++;
s->stack[s->top] = temp;
}
return 1;
}
//出栈
int Pop_stack(Mstack *s, xy *temp) {
if (Empty_stack(s)) {
return 0;
} else {
s->top--;
*temp = s->stack[s->top + 1];
}
return 1;
}
void find_way(Mstack *s);
int main() {
Mstack *s;
s = (Mstack *) malloc(sizeof(Mstack));
int i, j, r, c, row, col;
char str[1000];
int maze[1000][1000];
for (r = 0;; r++) {
gets(str);
if (strlen(str) == 0)
break;
for (c = 0; c < strlen(str); c++)
maze[r][c] = str[c] - '0';
}//输入迷宫
row = r;
col = c;
find_way(s);
/* for(r=0;r<row;r++)
{
for(c=0;c<col;c++)
printf("%d",maze[r][c]);
printf("\n");
}//输出迷宫*/
for (i = 0; i <= s->top; i++) {
printf("%c", s->stack[i].d);
}//输出路径
}
void find_way(Mstack *s) {
int i = 0, j = 0;
xy temp;
while (i != row - 1 || j != col - 1) {
if (maze[i][j + 1] == 0 && j < col - 1) {
temp.d = 'R';
temp.x = i;
temp.y = j;
Push_stack(s, temp);
maze[i][j] = 1;
j++;
continue;
} else if (maze[i + 1][j] == 0 && i < row - 1) {
temp.d = 'D';
temp.x = i;
temp.y = j;
Push_stack(s, temp);
maze[i][j] = 1;
i++;
continue;
} else if (maze[i - 1][j] == 0 && i > 0) {
temp.d = 'U';
temp.x = i;
temp.y = j;
Push_stack(s, temp);
maze[i][j] = 1;
i--;
continue;
} else if (maze[i][j - 1] == 0 && j > 0) {
temp.d = 'L';
temp.x = i;
temp.y = j;
Push_stack(s, temp);
maze[i][j] = 1;
j--;
continue;
} else {
xy temp;
if (Pop_stack(s, &temp)) {
maze[i][j] = 1;
i = temp.x;
j = temp.y;
if (Empty_stack(s)) {
printf("NO PASS!\n");//找不到路
break;
}
continue;
} else {
break;
}
}
}//找路
}
```
阅读全文