给一个n*n的方格,让你求从左上角到所给一点的任意一条路径并输出
该题用到dfs,以下是对dfs的简要解析 :详解请参见 传送门
dfs是一种用于遍历或搜索树或图的算法。 沿着树的深度遍历树的节点,尽可能深的搜索树的分支。当节点v的所在边都己被探寻过或者在搜寻时结点不满足条件,搜索将回溯到发现节点v的那条边的起始节点。整个进程反复进行直到所有节点都被访问为止。属于盲目搜索,最糟糕的情况算法时间复杂度为O(!n)。
下面上代码:
#include<iostream>
#include<cstdio>
#define M 8
#define N 8
#define Maxn 100
using namespace std;
int mg[M+2][N+2];
typedef struct {
int i,j,di;
}Box;
typedef struct {
Box data[Maxn];
int top;
}StType;
bool Push(StType* &s,Box e){
// if(s->top==Maxn-1){
// return false;
// }
s->top++;
s->data[s->top]=e;
return true;
}
void Init(StType *&s){
s=(StType*)malloc(sizeof(StType));
s->top=-1;
}
bool Pop(StType *&s,Box &e){
if(s->top==-1){
return false;
}
e=s