leetcode490题,迷宫。
leetcode499题,迷宫III,求到某个点的最小路径“lul”
带返回值与普通的C算法-DFS(深度优先搜索法)在写法上要注意的是:标记访问这个一定要有,如果本身条件不答应,就建立个数组vistied[MAXLEN][MAXLEN]
1、for循环节点,和判断什么时候递归,放在主函数,与回溯风格明显不同。
2、dfs结构:a、if 出界 xxx;b、if被访问过 xxx;c、如果成功 xxx;d、标记访问; c、dfs(next)
3、注意满足条件这个地方一定要好好填
4、新增注意,返回值都要填
比如499题目要传入最小路径做对比,那么需要在如果成功xxxx中进行算法分析。
像这种要增加字符串的,可以把字符串,当前字符串,当前字符串index作为全局变量操作
char g_ret[1000];
char g_final[1000];
int g_c;
bool dfs(int** maze, int row, int col, int sx, int sy, int dx, int dy, int count)
{
if (maze[sx][sy] != 0 && maze[sx][sy] < count) {
return false;
}
if (sx == dx && sy == dy) {
g_ret[g_c++] = '\0';
if (maze[sx][sy] == count && strcmp(g_final, g_ret) < 0) {
g_c--;
return false;
}
maze[sx][sy] = count;
memcpy(g_final, g_ret, g_c);
g_c--;
return true;
}
maze[sx][sy] = count;
int d, l, r, u, dcount, lcount, rcount, ucount;
bool dflag, lflag, rflag, uflag;
d = u = sx;
l = r = sy;
lcount = dcount = rcount = ucount = count;
dflag = lflag = rflag = uflag = false;
while (d + 1 < row && maze[d + 1][sy] != 1) {
d++;
dcount++;
if (sy == dy && d