假设在一个四行四列的二维数组中,1为墙,0为通道,我们要怎样找到出口
0行0列为入口,右下角为出口,最后还要打印他的坐标,那我们应该怎么做?
分析:可以采用深度优先算法
1、判断当前位置的上下左右是否有为0的数,为0就走,为1就不走
2、有个前提就是要判断这个坐标是否有效
3、当走错了,是不是要回到上下左右任意位置有为0的地方,但是假设上下左右都有为0的通路,那我们怎么知道那个路是我们走过的哪个是没走过的,使用就要进行标记,怎么标记,可以随便给他重新赋值
4、打印坐标,假设已经找到出口了,那我们打印坐标是不是要反着打印,那是不是就可以采用栈,栈的特性就是后进先出,先把坐标入栈,当遇到死胡同返回到某一位置时是不是就依次把错误的坐标出栈,最后再创建一个栈,把老栈里的坐标倒到新栈,是不是就可以把坐标从后往前打印
好了具体的分析完毕,再补充一个知识点
代码有点小长,上代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include <stdbool.h>
typedef struct path
{
int row;
int col;
}PAT;
/*///////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
typedef PAT STDataType;
typedef struct Stack
{
STDataType* a;
int top;//指向栈顶就是size
int capacity;//容量
}ST;
void StackInit(ST* ps);//初始化
void StackPrint(const ST* ps);//打印
void StackPush(ST* ps, STDataType x);//入栈
void StackDestroy(ST* ps);//销毁
void StackPop(ST* ps);//出栈
STDataType StackTop(const ST* ps);//取栈顶
int StackSize(const ST* ps);//计算栈的元素
//bool StackEmpty(const ST* ps,int* size);
bool StackEmpty(const ST* ps);//判断栈是否为空
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->capacity = 0;
ps->top = 0;//也可以给 -1
}
//void StackPrint(const ST* ps)
//{
// assert(ps);
// int i;
// for (i = ps->top - 1; i >= 0; i--)
// {
// printf("%d ", ps->a[i]);
// }
// printf("\n");
//}
void NewStack(ST* ps)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
ST* newStack = realloc(ps->a, sizeof(ST) * newcapacity);
if (newStack == NULL)
{
printf("开辟内存失败");
exit(-1);
}
ps->a = newStack;
ps->capacity = newcapacity;
}
void StackPush(ST* ps, STDataType x)
{
assert(ps);
if (ps->capacity == ps->top)
{
NewStack(ps);
}
ps->a[ps->top] = x;
ps->top++;
}
void StackPop(ST* ps)
{
assert(ps);
assert(ps->top > 0);
if (ps->top > 0)
ps->top--;
}
STDataType StackTop(const ST* ps)
{
assert(ps);
assert(ps->top > 0);
return ps->a[ps->top - 1];
}
int StackSize(const ST* ps)
{
assert(ps);
return ps->top;
}
bool StackEmpty(ST* ps)
{
return ps->top == 0;
}
void StackDestroy(ST* ps)
{
assert(ps);
free(ps->a); ps->a = NULL;
ps->capacity = ps->top = 0;
}
ST path;//设置全局栈
/*///////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
//输出路径坐标
void PrintPath(ST* path)
{
ST rPath;
StackInit(&rPath);
while (!StackEmpty(path))
{
StackPush(&rPath, StackTop(path));
StackPop(path);
}
while (!StackEmpty(&rPath))
{
PAT top = StackTop(&rPath);
printf("( %d , %d )", top.row,top.col);
StackPop(&rPath);
}
StackDestroy(&rPath);
}
void Print(int** maze,int n,int m)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
printf(" %d ", maze[i][j]);//输入二维数组的值
}
printf("\n");
}
printf("\n");
}
bool IsPass(int** maze, int n, int m, PAT cur)
{
if (cur.row >= 0 && cur.row < n
&& cur.col >= 0 && cur.col < m
&& maze[cur.row][cur.col] == 0)
{
return true;
}
else
return false;
}
bool GetMazePath(int** maze, int n, int m,PAT cur)
{
StackPush(&path, cur);//先入栈
if (cur.row == n - 1 && cur.col == m - 1)
{
maze[cur.row][cur.col] = 2;//把出口也赋值为 2
return true;
}
maze[cur.row][cur.col] = 2;
//上
PAT next;
next = cur;
next.row -= 1;
if (IsPass(maze, n, m, next))
{
if (GetMazePath(maze, n, m, next))//找到出路了
return true;
}
//下
next = cur;
next.row += 1;
if (IsPass(maze, n, m, next))
{
if (GetMazePath(maze, n, m, next))//找到出路了
return true;
}
//左
next = cur;
next.col -= 1;
if (IsPass(maze, n, m, next))
{
if (GetMazePath(maze, n, m, next))//找到出路了
return true;
}
//右
next = cur;
next.col += 1;
if (IsPass(maze, n, m, next))
{
if (GetMazePath(maze, n, m, next))//找到出路了
return true;
}
StackPop(&path);//如果以上条件都不满足就出栈
return false;
}
int main()
{
int n = 0, m = 0;
scanf("%d,%d", &n, &m);
//创建二维数组的行
int** maze = (int**)malloc(sizeof(int*) * n);
for (int i = 0; i < n; i++)
{
//创建二维数组的列
maze[i] = (int*)malloc(sizeof(int) * m);
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
scanf("%d", &maze[i][j]);//输入二维数组的值
}
}
PAT InPath = { 0.0 };
if (GetMazePath(maze, n, m, InPath))
PrintPath(&path);
else
printf("false");
printf("\n");
Print(maze, n, m);
for (int i = 0; i < n; i++)
{
free(maze[i]);
}
free(maze);
maze = NULL;
}