kZjPBD.jpg

【 2018南京 】Kangaroo Puzzle (思维+暴力模拟)

Your friend has made a computer video game called "Kangaroo Puzzle" and wants you to give it a try for him. As the name of this game indicates, there are some (at least 2) kangaroos stranded in a puzzle and the player's goal is to control them to gather. As long as all the kangaroos in the puzzle get together, they can escape the puzzle by the miraculous power of kangaroos.

The puzzle is a n×mn \times mn×m grid consisting of nmnmnm cells. There are walls in some cells and the kangaroos cannot enter these cells. The other cells are empty. The kangaroos can move in the following direction: up, down, left and right. It is guaranteed that one kangaroo can move from an empty cell to any other. It is also guaranteed that there is no cycle in the puzzle —\text{---}— that is, it's impossible that one kangaroo can move from an empty cell, pass by several distinct empty cells, and then back to the original cell.

There is exactly one kangaroo in every empty cell at the beginning. You can control the kangaroos by pressing the button U,D,L,R\texttt{U}, \texttt{D}, \texttt{L}, \texttt{R}U,D,L,R on your keyboard. The kangaroos will move simultaneously according to the button you press. For instance, if you press the button U\texttt{U}U, a kangaroo would move to the upper cell if it exists and is empty; otherwise, the kangaroo will stay still. You can press the buttons for at most 500005000050000 times. If there are still two kangaroos standing in different cells after 500005000050000 steps, you will lose the game.

Input

The first line contains two integers, nnn and mmm (1≤n,m≤201 \leq n,m \leq 201n,m20), the height and the width of the puzzle, respectively. Each of the next nnn lines contains a (0,1\texttt{0,1}0,1)-string of length mmm, representing the puzzle. If the jjj-th character of the i+1i+1i+1-th line is 1\texttt{1}1, then the cell at the iii-th row and the jjj-th column is empty;otherwise (i.e. it is 0\texttt{0}0), the corresponding cell is blocked and cannot be entered.

Output

Print a string consisting of U,D,L,R\texttt{U}, \texttt{D}, \texttt{L}, \texttt{R}U,D,L,R, such that all kangaroos will get together after pressing the buttons in the order of this string. The length of the string should not exceed 500005000050000. There are many possible valid answers, so just print any of them.

本题答案不唯一,符合要求的答案均正确

样例输入1

4 4
1111
1001
1001
1110

样例输出1

LLUUURRRDD

样例输入2

2 15
111111111111111
101010101010101

样例输出2

ULLLLLLLLLLLLLL


SOLUTION:
每次选定两个1进行合并,因为一个点走,其他的点也走
我们每次都让选定的第一个点走到第二个点的位置,
进行若干次后,一定会成功追及

CODE:
#include"bits/stdc++.h"
using namespace std;

int n,m;
char a[30][30];
char b[30][30];
int ok[30][30];
int X[23],Y[32];
string ans;
struct node
{
    int x,y;
    string s;
};
int vis[21][21];
int safe(int x,int y)
{
    return x>=1&&x<=n&&y>=1&&y<=m&&ok[x][y]==1;
}
void work(int i,int j,string s)
{
    int len=s.length();
  //  cout<<i<<" "<<j<<endl;
            for(int k=0;k<len;k++)
            {
                if(s[k]=='1')
                {
                    if(safe(i-1,j))i--;
                }
                if(s[k]=='2')
                {
                    if(safe(i+1,j))i++;
                }
                if(s[k]=='3')
                {
                    if(safe(i,j-1))j--;
                }
                if(s[k]=='4')
                {
                    if(safe(i,j+1))j++;
                }
            }
            //cout<<"!!!"<<i<<" "<<j<<endl;
        b[i][j]='1';

}
string tmp;
inline void Move()
{
    queue<node> q;q.push({X[1],Y[1],""});
    memset(vis,0,sizeof vis);

    while(!q.empty())
    {
        node t=q.front();q.pop();
        if(vis[t.x][t.y])continue;
        vis[t.x][t.y]=1;
        if(t.x==X[2]&&t.y==Y[2])
        {
            X[1]=X[2];
            Y[1]=Y[2];
            tmp=tmp+t.s;
            int len=t.s.length();
            for(int i=0;i<len;i++)
            {
                if(t.s[i]=='1')
                {
                    if(safe(X[2]-1,Y[2]))X[2]--;
                }
                if(t.s[i]=='2')
                {
                    if(safe(X[2]+1,Y[2]))X[2]++;
                }
                if(t.s[i]=='3')
                {
                    if(safe(X[2],Y[2]-1))Y[2]--;
                }
                if(t.s[i]=='4')
                {
                    if(safe(X[2],Y[2]+1))Y[2]++;
                }
            }
            break;
        }
        if(safe(t.x-1,t.y))q.push({t.x-1,t.y,(string)(t.s+"1")});
        if(safe(t.x+1,t.y))q.push({t.x+1,t.y,(string)(t.s+"2")});
        if(safe(t.x,t.y-1))q.push({t.x,t.y-1,(string)(t.s+"3")});
        if(safe(t.x,t.y+1))q.push({t.x,t.y+1,(string)(t.s+"4")});
    }



}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)cin>>a[i][j],ok[i][j]=(a[i][j]=='1');
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++);//cout<<ok[i][j]; cout<<endl;
    }
    //cout<<safe(2,2)<<endl;
    while(1)
    {
        int cnt=0;
        for(int i=1;i<=n&&cnt<2;i++)
        {
            for(int j=1;j<=m&&cnt<2;j++)
            {
                if(a[i][j]=='1')
                {
                    X[++cnt]=i;
                    Y[cnt]=j;
                }
            }
        }
        if(cnt<2)break;
        //cout<<X[1]<<Y[1]<<X[2]<<Y[2]<<endl;
       while(X[1]!=X[2] || Y[1]!=Y[2]) Move();

        for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) b[i][j]='0';
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(a[i][j]=='1')
                {
                   work(i,j,tmp);
                }
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            { a[i][j]=b[i][j];
            }
        }


       // cout<<tmp<<endl;
    ans+=tmp;
    tmp="";
    }
    map<char,char> mp;
    mp['1']='U';
    mp['2']='D';
    mp['3']='L';
    mp['4']='R';
    int len=ans.length();
    for(int i=0;i<len;i++)
    {
        printf("%c",mp[ans[i]]);
    }

}

  







posted @ 2019-07-23 12:53  Through_The_Night  阅读(673)  评论(0编辑  收藏  举报