2019第十届蓝桥杯B组C++省赛E题迷宫--BFS(倒搜)

试题 E: 迷宫
本题总分:15 分
【问题描述】
下图给出了一个迷宫的平面图,其中标记为 1 的为障碍,标记为 0 的为可以通行的地方。

010000
000100
001001
110000

迷宫的入口为左上角,出口为右下角,在迷宫中,只能从一个位置走到这 个它的上、下、左、右四个方向之一。
对于上面的迷宫,从入口开始,可以按DRRURRDDDR 的顺序通过迷宫, 一共 10 步。其中 D、U、L、R 分别表示向下、向上、向左、向右走。
对于下面这个更复杂的迷宫(30 行 50 列),请找出一种通过迷宫的方式,其使用的步数最少,在步数最少的前提下,请找出字典序最小的一个作为答案。请注意在字典序中D<L<R<U。(如果你把以下文字复制到文本文件中,请务 必检查复制的内容是否与文档中的一致。在试题目录下有一个文件 maze.txt, 内容与下面的文本相同)

01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010
00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000

思路:
这题应该BFS去做,广度优先记录路径不如深度优先那么简单,它要先走一遍把经过点到开始点的距离dis[x][y]先标记下来,然后终点往回走,走的下一个点到开始点的距离dis[next.x][next.y],要比当前点到开始点的距离dis[cur.x][cur.y]小1,即,dis[next.x][next.y]=dis[cur.x][dis[cur.y]-1就可以得倒路径了。
但是这里有一点要注意。
考虑下列情况:(这是bfs倒搜dis数组的一部分
)
在这里插入图片描述

回溯的时候有这种请况在dis[x][y]=147的时候下一步有两个点可以走都是最短的。因为迷宫里最短路径并非唯一,BFS可以求的最短路径没错,但得到的与题目要求的字典序最小可能不一样。有人说,按DLRU顺序走就可以了,但是,是开始标记dis数组时按着这顺序,还是回溯时按这个顺序呢?
答案是如果开始标记dis数组时是倒着搜时,然后回溯时按这个顺序是对的,但如果开始时正着搜的,那么回溯时就不能按这个顺序了,因为这样得到的路径是从(29,49)(0,0)字典序最小的最短路径而不是从(0,0)(29,49)字典序最小的最短路径。
可以人工模拟一下回溯过程。
正搜时dis数组:

 0  *  4  *  *  * 14  * 24 23  * 21  *  * 26 25  * 29 30  * 32  *132  *  *131132  *120  *  *121  *119120  *122123124125  *135134133  *143  *  *  *  *
  1  2  3  4  * 12 13 14  * 22 21 20 21 22  * 24  * 28  * 30 31  *131130129130  *120119118119120119118  *120121  *  *126127  *  *132  *142141  *141  *
  2  *  *  *  * 11  *  * 14  * 20 19  * 21 22 23  * 27 28 29 30 31  *  *128  *122121  *117  *  *  *117118119  *  *128127128129130131132  *140139140141
  3  *  7  8  9 10 11 12 13 14  * 18  * 20  * 24 25 26  *  * 31  *129128127126  *120  *116115114115116  *120  *122  *128  *130  *  *133134  *138  *  *
  4  5  6  *  *  *  *  * 14 15 16 17 18 19  * 25  * 27 28 29 30  *130129  *125  *119118117  *113  *117118119120121  *129  *  *136135134135136137138139
  *  *  7  8  * 34 35 36  *  * 17  * 19  * 27 26 27 28  * 30  * 66  *  *125124123  *  *118  *112111  *  *120  *122  *130  *  *  *  *  *  *137  *  *  *
 10  9  8  *  * 33  *  *  *  * 18  * 20  * 28 27  * 29 30  * 66 65  * 67  *123122121120119120  *110111112  *136  *132131  *  *  *141140139138139140141
  * 10  * 34 33 32 31 30  * 28  * 22 21 22  * 28 29  *  *  *  * 64  * 66  *124  *  *  *  *  *110109  *  *136135134133  *145144143142  *  *  *140  *142
 12 11  *  *  * 31 30 29 28 27  * 23  * 23  * 29 30 31 32  *  * 63 64 65  *125126127128129130  *108107106  *136  *134135  *  *144143144145  *141142  *
  *  * 37 36 35  *  * 30  * 26 25 24 25  *  *  * 31 32  * 62 61 62  * 66 67  *127  *129  *131  *109  *105  *137  *135  *147146145  *  *146  *142143144
 40 39 38  * 34 33 32 31  * 27 26  * 26 27 28 29 30  * 60  * 60 61  * 67  * 67  *  *  *  *  *  *  *105104105  *101  *  *  *147  *149148147148  *144  *
  *  *  * 36 35  * 33 32  * 28  * 28 27  * 29 30  * 58 59 60 59  * 69 68 67 66 65  * 69  * 71  *  *  *103  *101100  *  *  *  *151150149  *149  *145146
 40 39 38 37 36 37  * 31 30 29 30 29 28 29  * 31  * 57  *  * 58 59  *  *  *  * 64  * 68 69 70  *  *103102101100 99  *  *  *  *  *151  *151150151  *  *
  * 40  * 38  * 38  * 32 31  *  *  * 29 30 31 32  * 56 57 56 57  *  * 62 61 62 63  * 67  *  *  *  *  *  *  *  * 98  *  *  *  *153152153152  *152153154
  * 41  * 39  * 37  * 33  * 33 32 31 30  *  * 33  * 55  * 55  * 59 60  * 60  * 64 65 66 67  *  *  * 93 94 95 96 97  *  *  *159  *  *  *153  *153154  *
  * 40 39 38 37 36 35 34  * 34  *  * 31 32 33  * 55 54 53 54  * 58  *  * 59 60  * 64  *  * 95  * 93 92  * 96  *  *  *160159158157156155154155  *155156
  * 41  * 39  * 37 36  * 36 35 34 33 32 33 34  * 56  * 52 53  * 57 56 57 58  * 62 63 64  * 94 93 92 91 92  * 88 89 90  *  *  *  *157  *155  *157156  *
 43 42  * 40  * 38 37  * 37  * 35  * 33  *  * 38  * 52 51  * 53  * 55  * 59 60 61  *  *  *  * 94  * 90  *  * 87  *  *  *161160159158  *  *185  *157  *
  *  * 42 41  * 39  * 39 38 37 36  * 34 35 36 37  *  * 50 51 52 53 54 55  * 59  * 61 62  * 92  * 90 89 88 87 86  *164163162  *  *  *182183184185  *  *
 45 44 43 42  * 40 41 40  *  * 37 36 35 36  *  * 47  * 49  *  * 52  * 56 57 58 59 60 61  * 91 90  * 90  * 86 85  *165164  *182181180181  *  *  *183  *
  * 45  * 43 44  * 42  * 40 39 38  * 36  * 48 47 46 47 48 49 50 51  *  *  * 59  *  * 62 63  * 89  *  *  *  * 84  *  *165  *183  *179  *181180181182  *
 47 46  * 44  * 44 43 42 41  * 39 38 37 38  *  * 45  * 47  * 51  * 55 56 57 58  * 64 63 64  * 88 87 88  * 84 83  *167166167  *179178177  *179  *183  *
  * 47  * 45 46 45 44  * 42 41 40  *  * 39 40  * 44 45 46  * 52 53 54 55  * 59  * 65  * 65 66  * 86  * 84  * 82  *168  *  *  *  *  *176  *178179  *183
 49 48 47 46 47  * 45 46  * 42  * 42 41 40 41 42 43  *  * 54 53  * 55  * 61 60  * 64  * 66 67  * 85 84 83 82 81  *169170171172173174175176177178  *182
  *  * 48  * 48 47 46 45 44 43  * 43 42  *  *  * 44  *  *  * 54 55  * 59 60  * 62 63 64 65  *  *  * 85  * 81 80  *170  *  *173  *  *  *177  *179180181
 51 50 49 50 49  *  * 46  * 44 45 44  * 48 49 50  * 54 55 56  * 56 57 58 59 60 61 62  * 66 67 68 69  *  *  * 79  *171172173174175176  *  *181180  *  *
  * 51  * 51  * 49 48 47  * 45  * 45 46 47  * 51 52 53  * 57 58 57  *  *  *  *  * 63 64 65  * 69  * 75  * 77 78  *172  *174175176177178179  *181182183
  * 52 53 52 51 50  * 48  * 46 47  * 47  * 51 52  * 54  * 58  *  * 61 62 63 64 65 64 65  * 71 70  * 74  * 76  *174173174  *176  *  *  *180  *182183184
 54 53  *  *  *  * 50 49 48 47  * 49 48 49 50  * 56 55 56 57 58 59 60  *  * 65  *  *  * 73 72 71 72 73 74 75 76  *174175176177178179180181  *183  *  *
  * 54 55 54 53 52 51  *  * 48 49  *  *  * 51  * 57  *  *  * 59  * 61 62 63  *  *  *  *  *  * 72  *  *  * 76  *  *  *176  *  *179  *  *  *  *184185186

倒着搜dis数组:

186  *  *  *  *  *180  *176175  *173  *  *176175  *177178  *180  * 90  *  * 89 90  * 76  *  * 73  * 71 72  * 64 63 62 61  * 59 58 57  * 57  *  *  *  *
185186  *  *  *178179180  *174173172173172  *174  *176  *178179  * 89 88 87 88  * 76 75 74 73 72 71 70  * 66 65  *  * 60 59  *  * 56  * 56 55  * 55  *
184  *  *  *  *177  *  *174  *172171  *171172173  *175176177178179  *  * 86  * 78 77  * 75  *  *  * 69 68 67  *  * 60 59 58 57 56 55 54  * 54 53 54 55
183  *179178177176175174173172  *170  *170  *172173174  *  *177  * 87 86 85 84  * 78  * 74 73 72 71 70  * 68  * 72  * 60  * 58  *  * 53 52  * 52  *  *
182181180  *  *  *  *  *172171170169168169  *171  *173174175176  * 88 87  * 83  * 77 76 75  * 73  * 71 70 69 70 71  * 61  *  * 54 53 52 51 50 51 52 53
  *  *181182  *172173174  *  *171  *167  *171170171172  *176  *152  *  * 83 82 81  *  * 76  * 74 75  *  * 70  * 72  * 62  *  *  *  *  *  * 49  *  *  *
184183182  *  *171  *  *  *  *172  *166  *170169  *173174  *152151  *149  * 81 80 79 78 77 78  * 76 77 78  * 68  * 64 63  *  *  * 45 46 47 48 49 50 51
  *184  *172171170169168  *166  *164165166  *168167  *  *  *  *150  *148  * 82  *  *  *  *  * 78 77  *  * 68 67 66 65  * 47 46 45 44  *  *  * 50  * 52
186185  *  *  *169168167166165  *163  *167  *167166167168  *  *149148147  * 83 84 85 86 87 88  * 78 79 80  * 68  * 66 67  *  * 44 43 42 41  * 51 52  *
  *  *171170169  *  *166  *164163162161  *  *  *165166  *150149150  *146147  * 85  * 87  * 89  * 79  * 81  * 69  * 67  * 47 46 45  *  * 40  * 52 53 54
174173172  *168167166165  *163164  *160161162163164  *148  *148149  *145  *143  *  *  *  *  *  *  * 83 82 83  * 89  *  *  * 47  * 39 40 39 38  * 54  *
  *  *  *168167  *165164  *162  *160159  *163162  *146147148147  *145144143142141  *141  *143  *  *  * 83  * 87 88  *  *  *  * 39 38 39  * 37  * 55 56
170169168167166165  *163162161160159158159  *161  *145  *  *146147  *  *  *  *140  *140141142  *  * 85 84 85 86 87  *  *  *  *  * 37  * 35 36 37  *  *
  *168  *166  *164  *164163  *  *  *157158159160  *144145144145  *  *138137138139  *139  *  *  *  *  *  *  *  * 88  *  *  *  * 37 36 35 34  * 38 39 40
  *167  *165  *163  *165  *159158157156  *  *161  *143  *143  *139140  *136  *138137138139  *  *  * 93 92 91 90 89  *  *  * 33  *  *  * 33  * 39 40  *
  *166165164163162163164  *158  *  *155156157  *143142141142  *138  *  *135136  *136  *  * 99  * 95 94  * 92  *  *  * 34 33 32 31 30 31 32 33  * 41 42
  *167  *163  *161162  *158157156155154155156  *144  *140141  *137136135134  *134135136  * 98 97 96 95 96  *102103104  *  *  *  * 29  * 33  * 43 42  *
169168  *162  *160161  *157  *155  *153  *  *156  *140139  *137  *135  *133132133  *  *  *  * 98  * 96  *  *101  *  *  * 25 26 27 28  *  * 21  * 43  *
  *  *162161  *159  *157156155154  *152153154155  *  *138137136135134133  *131  *129128  *116  * 98 97 98 99100  * 24 23 24  *  *  * 18 19 20 21  *  *
163162161160  *158157158  *  *153152151152  *  *141  *139  *  *136  *132131130129128127  *115114  * 98  *100101  * 23 22  * 18 17 16 17  *  *  * 15  *
  *161  *159160  *156  *154153152  *150  *142141140139138137136137  *  *  *131  *  *126125  *113  *  *  *  *102  *  * 21  * 19  * 15  * 13 12 13 14  *
161160  *158  *156155154153  *151150149148  *  *141  *139  *135  *135134133132  *126125124  *112111112  *104103  * 19 20 21  * 15 14 13  * 11  * 15  *
  *159  *157156155154  *152151152  *  *147146  *142141140  *134135136135  *131  *125  *123124  *110  *108  *104  * 18  *  *  *  *  * 12  * 10  9  * 11
159158157156155  *153152  *150  *148147146145144143  *  *134133  *137  *129130  *124  *122123  *109108107106105  * 17 16 15 14 13 12 11 10  9  8  * 10
  *  *158  *154153152151150149  *147148  *  *  *144  *  *  *132131  *129128  *124123122121  *  *  *109  *107106  * 18  *  * 15  *  *  * 11  *  7  8  9
159158157156155  *  *152  *148147146  *142141140  *136135134  *130129128127126125124  *120119118119  *  *  *107  * 19 18 17 16 17 18  *  *  7  6  *  *
  *159  *157  *155154153  *149  *145144143  *139138137  *133132131  *  *  *  *  *123122121  *117  *115  *109108  * 20  * 18 17 18 19 20 21  *  5  6  7
  *160159158157156  *154  *150151  *145  *141140  *136  *134  *  *129128127126125124123  *117116  *114  *110  * 22 21 22  * 18  *  *  * 22  *  4  5  6
162161  *  *  *  *154153152151  *145144143142  *136135134133132131130  *  *127  *  *  *117116115114113112111112  * 22 21 20 19 20 21 22 23  *  3  *  *
  *160159158157156155  *  *152153  *  *  *143  *137  *  *  *133  *131132133  *  *  *  *  *  *116  *  *  *112  *  *  * 22  *  * 21  *  *  *  *  2  1  0

以下倒搜代码:

#include<iostream>
#include<iomanip>
#include<queue>
#include<stack>
#include<string>
using namespace std;

const int n = 30, m = 50;
const int dx[4] = {1,0,0,-1};			// D,L,R,U   下左右上 
const int dy[4] = {0,-1,1,0};

class Point
{
public:
	int x;
	int y;
};

void bfs(int map[n][m],int dis[n][m],Point path[],int& pathLen)
{
	dis[29][49]=0;			//将最后一点的坐标dis记为0 
	Point start,end;
	start.x = 0; start.y = 0; 
	end.x = 29; end.y = 49;
	queue<Point>q;
	q.push(end);		//最后一个点进入队列 
	Point cur,next;   //当前坐标,下一个点的坐标 
	while(!q.empty())				//找最短路径 
	{
		cur = q.front();
		map[cur.x][cur.y] = 1;
		q.pop();
		for(int i = 0; i < 4; i++)
		{
			next.x = cur.x + dx[i];
			next.y = cur.y + dy[i];
			if(next.x >= 0 && next.x < 30 && next.y >= 0 && next.y <= 49 && map[next.x][next.y] != 1)
			{
				q.push(next);
				dis[next.x] [next.y] = dis[cur.x][cur.y] + 1; 
				if(next.x == start.x && next.y == start.y)  //到达出口 
				{
					break;
				}
			} 
		}
		if(next.x == start.x && next.y == start.y)  //到达出口 
		{
			break;
		}
	}
	
	//记录路径
	pathLen = dis[0][0];
	cur = start;
	for(int i = 0; i < pathLen ; i++)
	{
		path[i] = cur;
		for(int j = 0; j < 4; j++)
		{
			next.x = cur.x + dx[j];
			next.y = cur.y + dy[j];
			if(next.x >= 0 && next.x < 30 && next.y >= 0 && next.y < 50 
					&& dis[next.x][next.y] < dis[cur.x][cur.y])
			{
				break;
			} 
		}
		cur = next;
	}
	
	//输出路径
	for(int i = 0; i < pathLen; i++)
	{
		cout<<"path["<<i<<"]="<<path[i].x<<","<<path[i].y<<endl;
	} 
	string res;
	int dx = 0,dy = 0; 
	for(int i = 0; i < pathLen - 1; i++)
	{
		cur = path[i];
		next = path[i+1];
		dx = next.x - cur.x;
		dy = next.y - cur.y;
		if(dx == 1 && dy == 0)
		{
			res += 'D';
		}
		else if(dx == -1 && dy == 0)
		{
			res += 'U'; 
		}
		else if(dx == 0 && dy == 1)
		{
			res += 'R'; 
		}
		else
		{
			res += 'L'; 
		}
	}
	cout<<pathLen; 
	cout<<res;
}
int main()
{
	int map[n][m]=
{
0,1,0,1,0,1,0,1,0,0,1,0,1,1,0,0,1,0,0,1,0,1,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,0,0,0,1,0,0,0,1,0,1,0,1,0,
0,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,1,1,0,0,1,1,0,1,0,0,1,0,1,
0,1,1,1,1,0,1,1,0,1,0,0,1,0,0,0,1,0,0,0,0,0,1,1,0,1,0,0,1,0,1,1,1,0,0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0,
0,1,0,0,0,0,0,0,0,0,1,0,1,0,1,0,0,0,1,1,0,1,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,1,0,1,0,1,1,0,0,1,0,1,1,
0,0,0,1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,1,1,0,0,1,0,0,1,0,0,0,0,1,1,1,0,1,0,0,1,0,1,1,0,1,1,1,0,1,0,0,0,
0,0,0,0,0,1,1,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,1,1,1,0,1,0,0,0,0,0,0,1,1,0,0,1,1,
1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,1,0,0,0,1,1,1,1,1,0,0,0,1,0,1,0,1,0,0,1,0,1,0,0,0,0,0,0,1,0,0,0,
1,0,0,0,0,0,1,0,1,0,0,1,0,1,0,0,1,0,1,0,1,1,0,0,0,0,0,0,0,1,0,0,1,0,1,0,1,0,0,0,1,0,1,1,1,0,1,0,0,0,
0,0,1,1,1,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,1,0,1,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,1,1,
1,0,0,0,0,0,0,1,1,0,0,1,1,1,0,1,0,1,1,1,0,1,0,0,0,1,0,0,0,1,1,0,1,1,1,0,1,0,1,0,1,1,0,1,1,1,1,0,0,0};
	int dis[n][m] = {0};
	for(int i = 0; i < n; i++)       //初始化dis数组,将初始距离定为99999999 
	{
		for(int j = 0; j < m; j++)
		{
			dis[i][j] = 99999999;
		}
	}
	Point* path = new Point[n*m];
	int pathLen = 0;
	bfs(map,dis,path,pathLen);
	cout<<endl;
	for(int i = 0; i < 30; i++)
	{
		for(int j = 0; j < 50; j++)
		{
			if(dis[i][j]==99999999)
				cout<<setw(3)<<"*";
			else
				cout<<setw(3)<<dis[i][j]; 
		}
		cout<<endl;
	}
	return 0;

}  

答案:
DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDR



2019-12-18:
突然发现之前写的最后一步没走,也没剪枝(可以不剪)
更新代码:

#include<iostream>
#include<iomanip>
#include<queue>
#include<stack>
#include<string>
using namespace std;

const int n = 30, m = 50;
const int dx[4] = {1,0,0,-1};			// D,L,R,U   下左右上 
const int dy[4] = {0,-1,1,0};

class Point
{
public:
	int x;
	int y;
};

void bfs(int map[n][m],int dis[n][m],Point path[],int& pathLen)
{
	dis[29][49]=0;			//将最后一点的坐标dis记为0 
	Point start,end;
	start.x = 0; start.y = 0; 
	end.x = 29; end.y = 49;
	queue<Point>q;
	q.push(end);		//最后一个点进入队列 
	Point cur,next;   //当前坐标,下一个点的坐标 
	while(!q.empty())				//找最短路径 
	{
		cur = q.front();
		map[cur.x][cur.y] = 1;
		q.pop();
		for(int i = 0; i < 4; i++)
		{
			next.x = cur.x + dx[i];
			next.y = cur.y + dy[i];
			if(next.x >= 0 && next.x < 30 && next.y >= 0 
				&& next.y <= 49 && map[next.x][next.y] != 1)
			{
				q.push(next);
				dis[next.x] [next.y] = dis[cur.x][cur.y] + 1; 
				if(next.x == start.x && next.y == start.y)  //到达出口 
				{
					break;
				}
			} 
		}
		if(next.x == start.x && next.y == start.y)  //到达出口 
		{
			break;
		}
	}
	
	//记录路径
	pathLen = dis[0][0];
	cur = start;
	path[0] = cur; 
	for(int i = 0; i < pathLen ; i++)
	{
		for(int j = 0; j < 4; j++)
		{
			next.x = cur.x + dx[j];
			next.y = cur.y + dy[j];
			if(next.x >= 0 && next.x < 30 && next.y >= 0 && next.y < 50 
					&& dis[next.x][next.y] < dis[cur.x][cur.y])
			{
				break;
			} 
		}
		cur = next;
		path[i+1] = cur;		//path[186]要终点,这样才能打印方向 ***更新后加的
	}
	//输出路径
	for(int i = 0; i < pathLen; i++)
	{
		cout<<"path["<<i<<"]="<<path[i].x<<","<<path[i].y<<endl;
	} 
	string res;
	int dx = 0,dy = 0; 
	for(int i = 0; i < pathLen; i++)
	{//最后面一步要走,i < pathlen而不是pathlen-1   ***更新后加的
		cur = path[i];
		next = path[i+1];
		dx = next.x - cur.x;
		dy = next.y - cur.y;
		if(dx == 1 && dy == 0)
		{
			res += 'D';
		}
		else if(dx == -1 && dy == 0)
		{
			res += 'U'; 
		}
		else if(dx == 0 && dy == 1)
		{
			res += 'R'; 
		}
		else
		{
			res += 'L'; 
		}
	}
	cout<<pathLen; 
	cout<<res;
}
int main()
{
	int map[n][m]=
{
0,1,0,1,0,1,0,1,0,0,1,0,1,1,0,0,1,0,0,1,0,1,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,0,0,0,1,0,0,0,1,0,1,0,1,0,
0,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,1,1,0,0,1,1,0,1,0,0,1,0,1,
0,1,1,1,1,0,1,1,0,1,0,0,1,0,0,0,1,0,0,0,0,0,1,1,0,1,0,0,1,0,1,1,1,0,0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0,
0,1,0,0,0,0,0,0,0,0,1,0,1,0,1,0,0,0,1,1,0,1,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,1,0,1,0,1,1,0,0,1,0,1,1,
0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,0,1,0,0,0,0,1,0,0,1,0,1,0,0,0,1,0,1,0,0,0,0,0,1,0,1,1,0,0,0,0,0,0,0,0,
1,1,0,0,1,0,0,0,1,1,0,1,0,1,0,0,0,0,1,0,1,0,1,1,0,0,0,1,1,0,1,0,0,1,1,0,1,0,1,0,1,0,1,1,1,1,0,1,1,1,
0,0,0,1,1,0,1,1,0,1,0,1,0,1,0,0,1,0,0,1,0,0,1,0,1,0,0,0,0,0,0,1,0,0,0,1,0,1,0,0,1,1,1,0,0,0,0,0,0,0,
1,0,1,0,0,0,0,0,1,0,1,0,0,0,1,0,0,1,1,0,1,0,1,0,1,0,1,1,1,1,1,0,0,1,1,0,0,0,0,1,0,0,0,0,1,1,1,0,1,0,
0,0,1,1,1,0,0,0,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0,1,1,0,1,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,1,1,
1,0,0,0,0,0,0,1,1,0,0,1,1,1,0,1,0,1,1,1,0,1,0,0,0,1,0,0,0,1,1,0,1,1,1,0,1,0,1,0,1,1,0,1,1,1,1,0,0,0};
	int dis[n][m] = {0};
	for(int i = 0; i < n; i++)       //初始化dis数组,将初始距离定为99999999 
	{
		for(int j = 0; j < m; j++)
		{
			dis[i][j] = 99999999;
		}
	}
	Point* path = new Point[n*m];
	int pathLen = 0;
	bfs(map,dis,path,pathLen);
	cout<<endl;
	for(int i = 0; i < 30; i++)
	{
		for(int j = 0; j < 50; j++)
		{
			if(dis[i][j]==99999999)
				cout<<setw(3)<<"*";
			else
				cout<<setw(3)<<dis[i][j]; 
		}
		cout<<endl;
	}
	return 0;

}  

DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值