题目描述
下图给出了一个迷宫的平面图,其中标记为 1 的为障碍,标记为 0 的为可以通行的地方。
010000
000100
001001
110000
110000迷宫的入口为左上角,出口为右下角,在迷宫中,只能从一个位置走到这 个它的上、下、左、右四个方向之一。
对于上面的迷宫,从入口开始,可以按 DRRURRDDDR 的顺序通过迷宫, 一共 10 步。其中 D、U、L、R 分别表示向下、向上、向左、向右走。 对于下面这个更复杂的迷宫(30 行 50 列),请找出一种通过迷宫的方式,其使用的步数最少,在步数最少的前提下,请找出字典序最小的一个作为答案。
请注意在字典序中 D
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
运行限制
• 最大运行时间:1s
• 最大运行内存: 256M
题目过于复杂,接下来我将分为四步依次讲解,想直接看代码的直接点击目录哈~
第一步 简化迷宫,输出步数
我们先用dfs做,在迷宫中,建立坐标系,x轴正方向向下,y轴正方向向右,一共有上下左右四个方向,那么就可以建立三个数组,前两个表示在坐标中的位移,第三个表示方向,上下一一对应,比如R表示右,也就是x不变,y位移加一。
const int dirx[4]={0,0,1,-1};
const int diry[4]={1,-1,0,0};
const char dir[5]={'R','L','D','U'};
建立数组存迷宫,为了使起点为(1,1),补齐首行0,和首列0,所以row和col比实际的大1个
int maze[row+1][col+1]=
{
0,0,0,0,0,0,0,
0,0,1,0,0,0,0,
0,0,0,0,1,0,0,
0,0,0,1,0,0,1,
0,1,1,0,0,0,0,
};
dfs(x,y,pos),pos为起点到x,y的步数,从(x,y)到(tox,toy)需要一步,所以起点到(tox,toy)需要pos+1步。
接下来的讲解都在代码注释中:
#include
#include
using namespace std;
const int dirx[4]={0,0,1,-1};
const int diry[4]={1,-1,0,0};
const char dir[5]={'R','L','D','U'};
const int row =4,col =6;
const int maxrc=6;
int maze[row+1][col+1]=
{
0,0,0,0,0,0,0,
0,0,1,0,0,0,0,
0,0,0,0,1,0,0,
0,0,0,1,0,0,1,
0,1,1,0,0,0,0,
};
int best;
int mins[maxrc+5][maxrc+5];
bool judge(int x,int y) {
if(x>0&&x<=row&&y>0&&y<=col&&!maze[x][y])
return true;
return false;
}
void dfs(int x,int y,int pos) {
if(pos>best)
return ;
if(x==row&&y==col) {
if(pos<best) {
best=pos;
}
return ;
}
for(int i=0;i<4;i++) {
int tox=x+dirx[i];
int toy=y+diry[i];
if(judge(tox,toy)&&pos+1<=mins[tox][toy]) {
maze[tox][toy]=1;
mins[tox][toy]=pos+1;
dfs(tox,toy,pos+1);
maze[tox][toy]=0;
}
}
}
int main()
{
memset(mins,0x3f,sizeof(mins));
best=1<<28;
maze[1][1]=1;
dfs(1,1,1);
cout<<best-1<<endl;
return 0;
}
有了这个代码,就可以把代码中的迷宫换成30行50列的迷宫,将row和col的值修改,可以得出步数。
第二步 加路径
定义string ans,temp和char a[row*col+5],在深搜的时候,数组a记录第pos步的方位,到达终点时,从1遍历到pos,把所有的方位连接到temp字符串中,如果pos
完整代码
#include
#include
using namespace std;
const int dirx[4]={0,0,1,-1};
const int diry[4]={1,-1,0,0};
const char dir[5]={'R','L','D','U'};
const int row =4,col =6;
const int maxrc=6;
int maze[row+1][col+1]=
{0,0,0,0,0,0,0,
0,0,1,0,0,0,0,
0,0,0,0,1,0,0,
0,0,0,1,0,0,1,
0,1,1,0,0,0,0,
};
int mins[maxrc+5][maxrc+5];
char a[row*col+5];
int best;
string ans;
bool judge(int x,int y) {
if(x>0&&x<=row&&y>0&&y<=col&&!maze[x][y])
return true;
return false;
}
void dfs(int x,int y,int pos) {
if(pos>best)
return ;
if(x==row&&y==col) {
string temp;
for(int i=1;i<pos;i++)
temp+=a[i];
if(pos<best) {
ans=temp;
best=pos;
}
else if(pos==best&&temp<ans) ans=temp;
return ;
}
for(int i=0;i<4;i++) {
int tox=x+dirx[i];
int toy=y+diry[i];
if(judge(tox,toy)&&pos+1<=mins[tox][toy]) {
maze[tox][toy]=1;
mins[tox][toy]=pos+1;
a[pos]=dir[i];
dfs(tox,toy,pos+1);
maze[tox][toy]=0;
}
}
}
int main()
{
memset(mins,1,sizeof(mins));
best=1<<28;
maze[1][1]=1;
dfs(1,1,1);
cout<<ans<<endl;
cout<<best-1<<endl;
return 0;
}
第三步 本题完整代码
#include
#include
using namespace std;
const int dirx[4]={0,0,1,-1};
const int diry[4]={1,-1,0,0};
const char dir[5]={'R','L','D','U'};
const int row =30,col =50;
const int maxrc=50;
int maze[row+1][col+1]=
{
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,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,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,0,1,1,1,1,0,1,1,0,1,0,0,1,0,0,0,1,0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,1,0,0,1,0,0,0,1,0,0,0,0,1,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1,1,1,1,1,0,1,0,0,1,0,
0,0,0,0,0,0,1,0,0,1,0,1,0,0,0,0,0,0,1,1,0,0,1,0,1,0,0,1,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,
0,1,1,0,1,0,0,0,0,0,0,1,0,0,1,1,1,0,1,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,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,
0,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,
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,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,
0,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 mins[maxrc+5][maxrc+5];
char a[row*col+5];
int best;
string ans;
bool judge(int x,int y) {
if(x>0&&x<=row&&y>0&&y<=col&&!maze[x][y])
return true;
return false;
}
void dfs(int x,int y,int pos) {
if(pos>best)
return ;
if(x==row&&y==col) {
string temp;
for(int i=1;i<pos;i++)
temp+=a[i];
if(pos<best) {
ans=temp;
best=pos;
}
else if(pos==best&&temp<ans) ans=temp;
return ;
}
for(int i=0;i<4;i++) {
int tox=x+dirx[i];
int toy=y+diry[i];
if(judge(tox,toy)&&pos+1<=mins[tox][toy]) {
maze[tox][toy]=1;
mins[tox][toy]=pos+1;
a[pos]=dir[i];
dfs(tox,toy,pos+1);
maze[tox][toy]=0;
}
}
}
int main()
{
memset(mins,1,sizeof(mins));
best=1<<28;
maze[1][1]=1;
dfs(1,1,1);
cout<<ans<<endl;