I-Penguins 2021牛客暑期多校训练营2训练
在近几天打牛客网的比赛的时候,发现了一道bfs路径输出的题目,在自己写的时候并没有写出来,先贴一下相应的题目。
链接:https://ac.nowcoder.com/acm/contest/11253/I
来源:牛客网
Lovely penguins is a tiny game in which the player controls two penguins.
The game holds in two 20×2020 \times 2020×20 grids (the left one and the right one), and each has some blocked places.
We number the grid in xthx^{th}xth row (starting from the up), ythy^{th}yth column (starting from the left) (x,y){(x,y)}(x,y).
The penguins move in four directions: up, down, left, right, exactly one step once. If its way is blocked, or it reaches the border, then this movement is omitted.
The player also moves the penguins in four directions, but the behavior of the two penguins is mirrored:
- L : left penguin moves to left, right penguin moves to right.
- R : left penguin moves to right, right penguin moves to left.
- U : both move upwards.
- D : both move downwards.
An operation can be omitted on one penguin but works on another.
The left penguin starts from (20,20){(20,20)}(20,20) and wants to move to (1,20){(1,20)}(1,20).The right penguin starts from (20,1){(20,1)}(20,1) and wants to move to (1,1){(1,1)}(1,1).If both penguin reach there destination, thay win the game.
Find out the shortest way to win the game.If there are many shortest ways to win, find the one with minimum lexicographical order(D<L<R<U).
Note: When one penguin reaches the destination and the other does not, the penguin that has reached the destination may still move out of the destination.
输入描述:
The input consists of 20 lines, each line contains 41 characters, describing the grids, separated by a space.
'.' means the grid is empty.
'#' means the grid is blocked.
输出描述:
Output 22 lines.
The first line contains the least number of steps to win the game.
The second line contains a string consisting of 'L','R','U','D', describing a way to win.
There may be many ways to win, output the one with minimum lexicographical order.
Then output 20 lines, describing the track of the two penguins, mark the track with character 'A', and print as input.
题目大意则是两只企鹅走迷宫,走到相应点的最短距离。
先分析这道题的突破点在哪里
首先就是两只🐧是镜像的,那么他们移动的方向也将会是镜像的,对于这个移动我们只需要开两个不同的方向移动数组去解决即可
其次因为某一只企鹅可以在一定的位置不动(因为撞墙等因素),而另外一只🐧可以移动,那么我们在处理边界的时候,可以采用即使此刻移动后的坐标已经超出表盘了,我们将他们的坐标按照之前的来算
然后就是BFS的路径输出,bfs路径输出可以从末尾到之前来输出,这就要求我们需要在走路径的时候相应的存储是由哪个位置进入的,那么在循环的时候我们能够将这个步数给输出
(最后我是伞兵,给地图开了int导致一开始地图没读进来,低级错误导致大大浪费了时间)
接下来贴一下代码
#include <bits/stdc++.h> using namespace std; const int maxn = 25; const int inf = 0x3f3f3f3f; int dis[maxn][maxn][maxn][maxn]; char mp1[maxn][maxn]; char mp2[maxn][maxn]; int vis[maxn][maxn][maxn][maxn]; int tx1[]={0,1,0,0,-1}; int ty1[]={0,0,-1,1,0}; int tx2[]={0,1,0,0,-1}; int ty2[]={0,0,1,-1,0}; char move[]={"?DLRU"}; typedef struct { int px1,py1,px2,py2; }node; node pre[maxn][maxn][maxn][maxn]; int step[maxn][maxn][maxn][maxn]; void BFS() { node ss; ss.px1=20; ss.py1=20; ss.px2=20; ss.py2=1; queue<node> q; dis[20][20][20][1]=0; vis[20][20][20][1]=1; while(!q.empty()) q.pop(); q.push(ss); while(!q.empty()) { node route=q.front(); q.pop(); int xx1=route.px1,xx2=route.px2,yy1=route.py1,yy2=route.py2; if(xx1==1&&yy1==20&&xx2==1&&yy2==1) break; for(int i=1;i<=4;i++) { int pxx1 = xx1+tx1[i]; int pyy1 = yy1+ty1[i]; int pxx2 = xx2+tx2[i]; int pyy2 = yy2+ty2[i]; if(pxx1==1&&pyy1==20&&pxx2==1&&pyy2==20) break; if(pxx1<1||pxx1>20||pyy1<1||pyy1>20||mp1[pxx1][pyy1]=='#') pxx1=xx1,pyy1=yy1; if(pxx2<1||pxx2>20||pyy2<1||pyy2>20||mp2[pxx2][pyy2]=='#') pyy2=yy2,pxx2=xx2;//调整相应撞墙的路径不变 if(pxx1>=1&&pxx1<=20&&pxx2>=1&&pxx2<=20&&pyy1>=1&&pyy1<=20&&pyy2>=1&&pyy2<=20&&dis[pxx1][pyy1][pxx2][pyy2] == inf) { // cout<<pxx1<<" "<<pyy1<<" "<<pxx2<<" "<<pyy2<<endl; dis[pxx1][pyy1][pxx2][pyy2] = dis[xx1][yy1][xx2][yy2]+1; // cout<<dis[pxx1][pyy1][pxx2][pyy2]<<endl; step[pxx1][pyy1][pxx2][pyy2] = i; pre[pxx1][pyy1][pxx2][pyy2]={xx1,yy1,xx2,yy2}; node nextt; nextt.px1 = pxx1; nextt.px2 = pxx2; nextt.py1 = pyy1; nextt.py2 = pyy2; q.push(nextt); } } } } char fst[10000]; int main() { char zf; memset(dis,inf,sizeof dis); for(int i=1;i<=20;i++) { scanf("%s %s",mp1[i]+1,mp2[i]+1); } BFS(); // printf("%c %c",mp1[1][20],mp2[1][1]); printf("%d\n",dis[1][20][1][1]); node p; p.px1=1,p.py1=20; p.px2=1,p.py2=1; for(int i=dis[1][20][1][1];i>=0;i--) { int yx1=p.px1,yy1=p.py1; int yx2=p.px2,yy2=p.py2; int tot=step[yx1][yy1][yx2][yy2]; if(tot==1) fst[i]='D'; else if(tot==2) fst[i]='L'; else if(tot==3) fst[i]='R'; else fst[i]='U'; mp1[yx1][yy1]='A'; mp2[yx2][yy2]='A'; p=pre[yx1][yy1][yx2][yy2]; }//BFS路径记录 printf("%s\n",fst+1); for(int i=1;i<=20;i++) printf("%s %s\n",mp1[i]+1,mp2[i]+1); return 0; }

浙公网安备 33010602011771号