多校联赛2 Penguins

链接: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:

  1. L : left penguin moves to left, right penguin moves to right.
  2. R : left penguin moves to right, right penguin moves to left.
  3. U : both move upwards.
  4. 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.

示例1

输入

#................... .............##...#.
.................... .......#.....#.....#
.........#...#.#.... ...#....#...........
#........#.......... ...#..#.............
........#......#.... ..#.#......#.#.....#
......#.#..#.#....#. .......##.....##...#
....#...........#..# ....................
.##................. ...........#..#...#.
.....#.#........#.#. #.........#.#.......
.................... ..#....#..........#.
....#.#..........#.. .#.........#..#..#..
.........#.......#.. ..#.................
...#..#......#...#.. ......#.............
...........#...#.... ....................
..##..#.#....#..#... ..............#...#.
.#..#...#.#.....##.. .........#.#...#....
.#.........#........ ..............#.#...
..##.#........#...#. ##..................
....##.#............ .......#.....#......
..........##........ .#..#.#...........#.

输出

27
LULLUURRUUUUUUULUUUUURRUUUU
#..................A A............##...#.
...................A A......#.....#.....#
.........#...#.#...A A..#....#...........
#........#.........A A..#..#.............
........#......#.AAA AA#.#......#.#.....#
......#.#..#.#...A#. .A.....##.....##...#
....#...........#A.# .A..................
.##..............A.. .A.........#..#...#.
.....#.#........#A#. #A........#.#.......
.................AA. AA#....#..........#.
....#.#..........#A. A#.........#..#..#..
.........#.......#A. A.#.................
...#..#......#...#A. A.....#.............
...........#...#..A. A...................
..##..#.#....#..#.A. A.............#...#.
.#..#...#.#.....##A. A........#.#...#....
.#.........#....AAA. AAA...........#.#...
..##.#........#.A.#. ##A.................
....##.#........AAA. AAA....#.....#......
..........##......AA A#..#.#...........#.

备注:

It's guaranteed that there always exists a way to win, and the penguins' initial position is not blocked.

 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<set>

using namespace std;

typedef long long LL;

const int n=20;
int ans=0;
string strans;
char mp1[23][23];
char mp2[23][23];
bool book[23][23][23][23]={false};
struct node
{
    int x1,x2,y1,y2;
    int step=0;
    string path;
}nex,cur;
string dir[]={"D","L","R","U"};
int dx1[]={1,0,0,-1};
int dx2[]={1,0,0,-1};
int dy1[]={0,-1,1,0};
int dy2[]={0,1,-1,0};
queue<node>q;

void bfs()
{
    cur.step=0;
    cur.x1=cur.y1=cur.x2=20;
    cur.y2=1;
    cur.path="";
    book[20][20][20][1]=true;
    q.push(cur);

    while(!q.empty())
    {
        cur=q.front();
        q.pop();


        for(int i=0;i<4;i++)
        {
            nex.x1=cur.x1+dx1[i];
            nex.x2=cur.x2+dx2[i];
            nex.y1=cur.y1+dy1[i];
            nex.y2=cur.y2+dy2[i];
            nex.step=cur.step+1;
            nex.path=cur.path+dir[i];

            if(nex.x1<1||nex.x1>20) nex.x1=cur.x1;
            if(nex.x2<1||nex.x2>20) nex.x2=cur.x2;
            if(nex.y1<1||nex.y1>20) nex.y1=cur.y1;
            if(nex.y2<1||nex.y2>20) nex.y2=cur.y2;

            if(mp1[nex.x1][nex.y1]=='#') nex.x1=cur.x1,nex.y1=cur.y1;
            if(mp2[nex.x2][nex.y2]=='#') nex.x2=cur.x2,nex.y2=cur.y2;

            if(!book[nex.x1][nex.y1][nex.x2][nex.y2])
            {
                q.push(nex);
                book[nex.x1][nex.y1][nex.x2][nex.y2]=true;
            }

        }
        if(cur.x1==1&&cur.y1==20&&cur.x2==1&&cur.y2==1)
        {
            ans=cur.step;
            strans=cur.path;
            return;
        }
    }
}



int main()
{
	for(int i=1;i<=20;i++)
    {
        scanf("%s",mp1[i]+1);
        scanf("%s",mp2[i]+1);
    }

    bfs();
    printf("%d\n",ans);
    cout<<strans<<endl;

    int tx1=20,tx2=20,ty1=20,ty2=1;
    mp1[tx1][ty1]='A';
    mp2[tx2][ty2]='A';

    for(int i=0;i<strans.length();i++)
    {
        if(strans[i]=='D')
        {
            tx1++;
            tx2++;
            if(tx1>20||mp1[tx1][ty1]=='#')tx1--;
            if(tx2>20||mp2[tx2][ty2]=='#')tx2--;
        }
        else if(strans[i]=='L')
        {
            ty1--;
            ty2++;
            if(ty1<1||mp1[tx1][ty1]=='#')ty1++;
            if(ty2>20||mp2[tx2][ty2]=='#')ty2--;
        }
        else if(strans[i]=='R')
        {
            ty1++;
            ty2--;
            if(ty1>20||mp1[tx1][ty1]=='#')ty1--;
            if(ty2<1||mp2[tx2][ty2]=='#')ty2++;
        }
        else
        {
            tx1--;
            tx2--;
            if(tx1<1||mp1[tx1][ty1]=='#')tx1++;
            if(tx2<1||mp2[tx2][ty2]=='#')tx2++;
        }
        mp1[tx1][ty1]='A';
        mp2[tx2][ty2]='A';
    }
    for(int i=1;i<=20;i++)
    {
        printf("%s",mp1[i]+1);
        printf(" %s\n",mp2[i]+1);
    }
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值