C算法-DFS(深度优先搜索法)带返回值

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 == dx) {
    
    
			break;
		}
	}
	if (dcount > count) {
    
    
		g_ret[g_c++] = 'd';
		//printf("old sx %d sy %d  new sx %d sy %d count %d \n", sx, sy, d, sy, count);
		dflag = dfs(maze, row, col, d, sy, dx, dy, dcount);
		g_c--;
	}

	while (l - 1 >= 0 && maze[sx][l - 1] != 1) {
    
    
		l--;
		lcount++;
		if (sx == dx && l == dy) {
    
    
			break;
		}
	}
	if (lcount > count) {
    
    
		g_ret[g_c++] = 'l';
		//printf("old sx %d sy %d  new sx %d sy %d count %d \n", sx, sy, sx, l, count);
		lflag = dfs(maze, row, col, sx, l, dx, dy, lcount);
		g_c--;
	}

	while (r + 1 < col && maze[sx][r + 1] != 1) {
    
    
		r++;
		rcount++;
		if (sx == dx && r == dy) {
    
    
			break;
		}
	}
	if (rcount > count) {
    
    
		g_ret[g_c++] = 'r';
		//printf("old sx %d sy %d  new sx %d sy %d count %d \n", sx, sy, sx, r, count);
		rflag = dfs(maze, row, col, sx, r, dx, dy, rcount);
		g_c--;

	}

	while (u - 1 >= 0 && maze[u - 1][sy] != 1) {
    
    
		u--;
		ucount++;
		if (sy == dy && u == dx) {
    
    
			break;
		}
	}
	if (ucount > count) {
    
    
		g_ret[g_c++] = 'u';
		//printf("old sx %d sy %d  new sx %d sy %d count %d \n", sx, sy, u, sy, count);
		uflag = dfs(maze, row, col, u, sy, dx, dy, ucount);
		g_c--;

	}
	return dflag || rflag || lflag || uflag;
}

char * findShortestWay(int** maze, int mazeSize, int* mazeColSize, int* ball, int ballSize, int* hole, int holeSize) {
    
    
	int row, col, dx, dy, count;
	if (mazeSize == 0) {
    
    
		return "impossible";
	}
	if (ball[0] == hole[0] && ball[1] == hole[1]) {
    
    
		return "impossible";
	}
	row = mazeSize;
	col = *mazeColSize;
	dx = hole[0];
	dy = hole[1];
	count = 2;
	g_final[0] = '\0';
	g_ret[0] = '\0';
	g_c = 0;
	if (dfs(maze, row, col, ball[0], ball[1], dx, dy, count) == false) {
    
    
		return "impossible";
	}

	int retlen = strlen(g_final) + 1;
	char * ret = (char*)malloc(retlen);
	memcpy(ret, g_final, retlen);
	return ret;
}

490迷宫答案

#define MAXLEN 2000
int visited[MAXLEN][MAXLEN];

bool dfs(int sx, int sy, int dx, int dy, int row, int col, int** maze)
{
    
    
	if (sx == dx &&  sy == dy) {
    
    
		return true;
	}
	if (sx < 0 || sx >= row || sy < 0 || sy >= col) {
    
    
		return false;
	}
	if (visited[sx][sy] == 1) {
    
    
		return false;
	}
	//printf("sx %d sy %d\n", sx, sy);
	visited[sx][sy] = 1;
	int tmpx, tmpy;
	tmpx = sx;
	tmpy = sy;
	while (sx - 1 >= 0 && maze[sx - 1][sy] == 0) {
    
    
		sx--;
	}
	if (dfs(sx, sy, dx, dy, row, col, maze) == 1) {
    
    
		return true;
	}
	sx = tmpx;
	while (sx + 1 < row && maze[sx + 1][sy] == 0) {
    
    
		sx++;
	}
	if (dfs(sx, sy, dx, dy, row, col, maze) == 1) {
    
    
		return true;
	}
	sx = tmpx;
	while (sy + 1 < col && maze[sx][sy + 1] == 0) {
    
    
		sy++;
	}
	if (dfs(sx, sy, dx, dy, row, col, maze) == 1) {
    
    
		return true;
	}
	sy = tmpy;
	while (sy - 1 >= 0 && maze[sx][sy - 1] == 0) {
    
    
		sy--;
	}
	if (dfs(sx, sy, dx, dy, row, col, maze) == 1) {
    
    
		return true;
	}
	return false;

}

bool hasPath(int** maze, int mazeSize, int* mazeColSize, int* start, int startSize, int* destination, int destinationSize) {
    
    
	int row, col, dx, dy, sx, sy;
	if (mazeSize == 0) {
    
    
		return false;
	}
	if (start[0] == destination[0] && start[1] == destination[1]) {
    
    
		return true;
	}
	memset(visited, 0, sizeof(int)*MAXLEN*MAXLEN);
	row = mazeSize;
	col = *mazeColSize;
	dx = destination[0];
	dy = destination[1];
	sx = start[0];
	sy = start[1];

	return dfs(sx, sy, dx, dy, row, col, maze);

}

猜你喜欢

转载自blog.csdn.net/weixin_45554139/article/details/105068770