用c语言运用栈以及深度优先算法写一个迷宫代码
时间: 2023-02-23 14:55:09 浏览: 82
用栈写了一个迷宫算法
好的,这是一份示例代码,使用栈和深度优先搜索算法来解决迷宫问题:
```
#include <stdio.h>
#include <stdlib.h>
#define MAX_ROW 100
#define MAX_COL 100
#define STACK_SIZE 10000
struct Point {
int row, col;
};
struct Stack {
int top;
struct Point data[STACK_SIZE];
};
int maze[MAX_ROW][MAX_COL] = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 1, 0, 0, 0, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 1, 1, 0, 1, 0, 1},
{1, 0, 1, 0, 0, 0, 0, 1, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
int endRow = 7, endCol = 7;
void push(struct Stack *s, struct Point p) {
if (s->top == STACK_SIZE - 1) {
printf("栈已满\n");
exit(0);
}
s->data[++s->top] = p;
}
struct Point pop(struct Stack *s) {
if (s->top == -1) {
printf("栈已空\n");
exit(0);
}
return s->data[s->top--];
}
int isEmpty(struct Stack *s) {
return s->top == -1;
}
void printMaze() {
int i, j;
for (i = 0; i < MAX_ROW; i++) {
for (j = 0; j < MAX_COL; j++) {
printf("%d ", maze[i][j]);
}
printf("\n");
}
printf("\n");
}
int main() {
int i, j, row, col;
struct Point cur, next;
struct Stack s;
s.top = -1;
cur.row = 1;
cur.col = 1;
push(&s, cur);
while (!isEmpty(&s)) {
cur = pop(&s);
if (cur.row == endRow && cur.col == endCol) {
printf("找到了
阅读全文