天天看点

HDU 2821 Pusher

Problem Description

PusherBoy is an online game http://www.hacker.org/push . There is an R * C grid, and there are piles of blocks on some positions. The goal is to clear the blocks by pushing into them. 

You should choose an empty area as the initial position of the PusherBoy. Then you can choose which direction (U for up, D for down, L for left and R for right) to push. Once the direction is chosen, the PusherBoy will walk ahead until he met a pile of blocks (Walking outside the grid is invalid). Then he remove one block from the pile (so if the pile contains only one block, it will become empty), and push the remaining pile of blocks to the next area. (If there have been some blocks in the next area, the two piles will form a new big pile.)

Please note if the pusher is right up against the block, he can't remove and push it. That is, there must be a gap between the pusher and the pile. As the following figure, the pusher can go up, but cannot go down. (The cycle indicates the pusher, and the squares indicate the blocks. The nested squares indicate a pile of two blocks.)

And if a whole pile is pushed outside the grid, it will be considered as cleared.

Input

There are several test cases in each input. The first two lines of each case contain two numbers C and R. (R,C <= 25) Then R lines follow, indicating the grid. '.' stands for an empty area, and a lowercase letter stands for a pile of blocks. ('a' for one block, 'b' for two blocks, 'c' for three, and so on.)

Output

Output three lines for each case. The first two lines contains two numbers x and y, indicating the initial position of the PusherBoy. (0 <= x < R, 0 <= y < C). The third line contains a moving sequence contains 'U', 'D', 'L' and 'R'. Any correct answer will be accepted.

Sample Input

3

7

...

...

.b.

...

...

.a.

...

Sample Output

Hint

按照规则来,状态数应该非常少,所以直接枚举起点,然后dfs就好了。

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=30;
int n,m,len;
char s[N],ans[N*N],g[4]={'D','U','R','L'};
int d[4][2]={{1,0},{-1,0},{0,1},{0,-1}};

struct mp
{
    int a[N][N];
};

bool dfs(int x,int y,int D,mp now)
{
    if (len==D) return true;
    for (int i=0,j;i<4;i++)
    {
        if (now.a[x+d[i][0]][y+d[i][1]]) continue;
        for (j=1;!now.a[x+d[i][0]*j][y+d[i][1]*j];j++);
        if (now.a[x+d[i][0]*j][y+d[i][1]*j]>0)
        {
            mp nt=now;
            if (nt.a[x+d[i][0]*(j+1)][y+d[i][1]*(j+1)]==-1) break;
            nt.a[x+d[i][0]*(j+1)][y+d[i][1]*(j+1)]+=nt.a[x+d[i][0]*j][y+d[i][1]*j]-1;
            nt.a[x+d[i][0]*j][y+d[i][1]*j]=0;
            ans[D]=g[i];
            if (dfs(x+d[i][0]*j,y+d[i][1]*j,D+1,nt)) return true;
        }
    }
    return false;
}

int main()
{
    while (~scanf("%d%d",&m,&n))
    {
        mp now;    len=0;
        memset(now.a,-1,sizeof(now.a));
        for (int i=1;i<=n;i++)
        {
            scanf("%s",s+1);
            for (int j=1;j<=m;j++)
            {
                now.a[i][j]=s[j]=='.'?0:s[j]-'a'+1;
                len+=now.a[i][j];
            }
        }
        int flag=0,x,y;
        for (int i=1;i<=n&&!flag;i++)
        {
            for (int j=1;j<=m&&!flag;j++)
            {
                if (!now.a[i][j])
                {
                    flag=dfs(i,j,0,now);
                    x=i,y=j;
                }
            }
        }
        ans[len]=0;
        printf("%d\n%d\n%s\n",x-1,y-1,ans);
    }
    return 0;
}