程序设计基础7 深度优先搜索

题目描述


  有一个n*m格的迷宫(表示有n行、m列),其中有可走的也有不可走的,如果用1表示可以走,0表示不可以走,文件读入这n*m个数据和起始点、结束点(起始点和结束点都是用两个数据来描述的,分别表示这个点的行号和列号)。现在要你编程找出所有可行的道路,要求所走的路中没有重复的点,走时只能是上下左右四个方向。如果一条路都不可行,则输出相应信息(用-l表示无路)。
  请统一用 左上右下的顺序拓展,也就是 (0,-1),(-1,0),(0,1),(1,0)
 

输入

第一行是两个数n,m( 1 < n , m < 15 ),接下来是m行n列由1和0组成的数据,最后两行是起始点和结束点。 

输出

  所有可行的路径,描述一个点时用(x,y)的形式,除开始点外,其他的都要用“->”表示方向。 
  如果没有一条可行的路则输出-1。

样例输入

5 6
1 0 0 1 0 1
1 1 1 1 1 1
0 0 1 1 1 0
1 1 1 1 1 0
1 1 1 0 1 1
1 1
5 6

样例输出

(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(3,4)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(3,4)->(4,4)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(3,4)->(2,4)->(2,5)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(3,4)->(3,5)->(4,5)->(5,5)->(5,6)
(1,1)->(2,1)->(2,2)->(2,3)->(3,3)->(4,3)->(4,4)->(4,5)->(5,5)->(5,6)

一,我之前错误的方法

总结:定位到结点A,然后在A结点上下左右搜索,搜索到下一个能走的结点B就把A结点加入序列中,是一种退缩式方法,但缺陷是如果回溯时会出现混乱。

错误代码如下:

void DFS(int x,int y) {
	if (hashMap[x][y - 1] == 0 && road[x][y - 1] == 1) {
		hashMap[x][y-1] = 1;
		point[count].x = x;
		point[count].y = y-1;
		count++;
		DFS(x, y - 1);
	}
	if (hashMap[x-1][y] == 0 && road[x-1][y] == 1) {
		hashMap[x-1][y] = 1;
		point[count].x = x-1;
		point[count].y = y;
		count++;
		DFS(x-1, y);
	}
	if (hashMap[x][y + 1] == 0 && road[x][y + 1] == 1) {
		hashMap[x][y+1] = 1;
		point[count].x = x;
		point[count].y = y+1;
		count++;
		DFS(x, y + 1);
	}
	if (hashMap[x+1][y] == 0 && road[x+1][y] == 1) {
		hashMap[x+1][y] = 1;
		point[count].x = x+1;
		point[count].y = y;
		count++;
		DFS(x+1, y);
	}
        hashMap[x][y] = 0;
	count--;
	point[count].x = 0;
	point[count].y = 0;
}

二,正确的方法:

    先定位到结点A,在A结点上下左右搜索,搜索到可走结点B,就把B加入到序列中。

    即如果未来结点可走,立马把未来结点加入到序列中,在每一个DFS未来结点之后再把这个序列中的未来结点清空,这一点同全排列

      请仔细体会以上思想。

#include<cstdio>
using namespace std;
//到15:20
const int max_n = 289;
int count = 0;
int real_count = 0;
int start_x = 0, start_y = 0;
int end_x = 0, end_y = 0;
int road[17][17] = { 0 };
int hashMap[17][17] = { 0 };
struct Point {
	int x, y;
}point[max_n];
void check_print(int x,int y) {
	if (end_x == x && y == end_y) {
		point[count].x = x;
		point[count].y = y;
		count++;
		for (int i = 0; i < count; i++) {
			printf("(%d,%d)", point[i].x, point[i].y);
			if (i != count - 1) {
				printf("->");
			}
			else {
				printf("\n");
			}
		}
		count--;
		point[count].x = 0;
		point[count].y = 0;
		real_count++;
	}
}
void DFS(int x,int y) {                //二维数组作为形参不能忽略第二位的大小
	if (hashMap[x][y - 1] == 0 && road[x][y - 1] == 1) {
		if (x == end_x&&y - 1 == end_y) {
			check_print(x, y - 1);
			return;
		}
		hashMap[x][y-1] = 1;
		point[count].x = x;
		point[count].y = y-1;
		count++;
		DFS(x, y - 1);
		hashMap[x][y-1] = 0;
		count--;
		point[count].x = 0;
		point[count].y = 0;
	}
	if (hashMap[x-1][y] == 0 && road[x-1][y] == 1) {
		if (x-1 == end_x&&y == end_y) {
			check_print(x-1, y);
			return;
		}
		hashMap[x-1][y] = 1;
		point[count].x = x-1;
		point[count].y = y;
		count++;
		DFS(x-1, y);
		hashMap[x-1][y] = 0;
		count--;
		point[count].x = 0;
		point[count].y = 0;
	}
	if (hashMap[x][y + 1] == 0 && road[x][y + 1] == 1) {
		if (x == end_x&&y + 1 == end_y) {
			check_print(x, y + 1);
			return;
		}
		hashMap[x][y+1] = 1;
		point[count].x = x;
		point[count].y = y+1;
		count++;
		DFS(x, y + 1);
		hashMap[x][y+1] = 0;
		count--;
		point[count].x = 0;
		point[count].y = 0;
	}
	if (hashMap[x+1][y] == 0 && road[x+1][y] == 1) {
		if (x+1 == end_x&&y == end_y) {
			check_print(x+1, y);
			return;
		}
		hashMap[x+1][y] = 1;
		point[count].x = x+1;
		point[count].y = y;
		count++;
		DFS(x+1, y);
		hashMap[x+1][y] = 0;
		count--;
		point[count].x = 0;
		point[count].y = 0;
	}
/*	hashMap[x][y] = 0;
	count--;
	point[count].x = 0;
	point[count].y = 0;*/
}
int main() {
	int rows = 0, cols = 0;
	int check = 0;
	scanf("%d %d", &rows, &cols);
	for (int i = 1; i <= rows; i++) {
		for (int j = 1; j <= cols; j++) {
			scanf("%d", &road[i][j]);
		}
	}
	scanf("%d %d", &start_x, &start_y);
	scanf("%d %d", &end_x, &end_y);
	hashMap[start_x][start_y] = 1;
	point[count].x = start_x;
	point[count].y = start_y;
	count++;
	DFS(start_x, start_y);
	if (real_count == 0) {
		printf("-1");
	}
	return 0;
}

     

猜你喜欢

转载自blog.csdn.net/qq2285580599/article/details/82289219