bfs+回溯+路徑存盤
bfs怎樣存取路徑呢 這里我參考一個2019年第十屆藍橋杯省賽的題目來作為例子
問題描述
下圖給出了一個迷宮的平面圖,其中標記為 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
思路
這里我選擇用一個結構體陣列來存盤路徑
struct node
{
int x,y;
char c;
}pre[60][60];
在這里的x是這個點之前點的橫坐標 y是這個點之前點的縱坐標 c是方位字符
我們可以在更新bfs的時候同時更新pre陣列,代碼如下
void bfs()
{
q.push(st);
dis[st.x][st.y]=0;
while(!q.empty())
{
PII t=q.front();
q.pop();
for(int i=0;i<4;i++)
{
int x=t.x+dx[i];
int y=t.y+dy[i];
if(x>=0&&x<n&&y>=0&&y<m&&!dis[x][y]&&f[x][y]=='0')
{
dis[x][y]=dis[t.x][t.y]+1;
pre[x][y]={t.x,t.y,dir[i]};//更新并保存
q.push({x,y});
}
}
}
}
回溯
因為我們在回溯的程序中遍歷的路徑是與原路徑相反的,所以我們有兩個方法
-
用一個string來存盤路徑,最后輸出的時候reverse就可以啦
-
用堆疊來存盤路徑之后直接輸出
這兩個方法都是可以的看個人啦(個人覺得第一種更簡單)
兩種方法
第一種方法
while((pre[n][m].x!=0||pre[n][m].y!=0))
{
str+=pre[n][m].c;
int te1=pre[n][m].x;
int te2=pre[n][m].y;
n=te1;
m=te2;
}
str+=pre[n][m].c;
reverse(str.begin(),str.end());
cout<<str;
第二種方法
當沒有走到起點的時候一直回溯入堆疊,while回圈結束后將最后一個點的資訊入堆疊
while((pre[n][m].x!=0||pre[n][m].y!=0))
{
path.push(pre[n][m].c);
int te1=pre[n][m].x;
int te2=pre[n][m].y;
n=te1;
m=te2;
}
path.push(pre[n][m].c);
while(!path.empty())
{
cout<<path.top();
path.pop();
}
完整版代碼
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<string>
using namespace std;
typedef pair<int,int> PII;
#define x first
#define y second
char f[60][60];
int dis[60][60];
PII st={0,0},ed={29,49};
PII before[60][60];
int dx[4]={1,0,0,-1};
int dy[4]={0,-1,1,0};
char dir[4]={'D','L','R','U'};//注意這里是有順序的
int n=30,m=50;
struct node
{
int x,y;
char c;
}pre[60][60];
queue<PII>q;
void bfs()
{
q.push(st);
dis[st.x][st.y]=0;
while(!q.empty())
{
PII t=q.front();
q.pop();
for(int i=0;i<4;i++)
{
int x=t.x+dx[i];
int y=t.y+dy[i];
if(x>=0&&x<n&&y>=0&&y<m&&!dis[x][y]&&f[x][y]=='0')
{
dis[x][y]=dis[t.x][t.y]+1;
pre[x][y]={t.x,t.y,dir[i]};
q.push({x,y});
}
}
}
}
int main()
{
freopen("1.txt","r",stdin);
for(int i=0;i<n;i++)
{
scanf("%s",f[i]);
}
bfs();
stack<char> path;
n--;
m--;
while((pre[n][m].x!=0||pre[n][m].y!=0))
{
path.push(pre[n][m].c);
int te1=pre[n][m].x;
int te2=pre[n][m].y;
n=te1;
m=te2;
}
path.push(pre[n][m].c);
while(!path.empty())
{
cout<<path.top();
path.pop();
}
return 0;
}
本題答案
DDDDRRURRRRRRDRRRRDDDLDDRDDDDDDDDDDDDRDDRRRURRUURRDDDDRDRRRRRRDRRURRDDDRRRRUURUUUUUUULULLUUUURRRRUULLLUUUULLUUULUURRURRURURRRDDRRRRRDDRRDDLLLDDRRDDRDDLDDDLLDDLLLDLDDDLDDRRRRRRRRRDDDDDDRR
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/277084.html
標籤:其他
