首页 > 代码库 > HDU-2821-Pusher(DFS)

HDU-2821-Pusher(DFS)

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
4 1 UDU
Hint
Hint: The following figures show the sample. The circle is the position of the pusher. And the squares are blocks (The two nested squares indicating a pile of two blocks). And this is the unique solution for this case.
 

Source
2009 Multi-University Training Contest 1 - Host by TJU 


思路:简单DFS。给若干个可重叠的格子,起点可以任意选择,可以往四个方向一直走到有格子的地方为止,每碰一次就消掉一个格子,并且把剩下的移到下一个位置。

注意:①起点不能有格子。②必须要隔一个位置才能碰。③碰的格子在边上时,剩下的格子不能移出矩形范围。④格子在边上时,碰之后如果还有格子剩余,pusher的位置就不在边上,否则就在边上。


#include <stdio.h>

int n,m,total,sx,sy;
char mp[25][26],ans[10000];
bool flag;

void dfs(int x,int y,int cnt)
{
    int i,t1,t2;

    if(flag) return;

    if(cnt==total)
    {
        printf("%d\n%d\n",sx,sy);

        ans[cnt]=0;

        puts(ans);

        flag=1;

        return;
    }

    if(x-1>0 && !mp[x-1][y])//U
    {
        for(i=x-2;i>=0;i--) if(mp[i][y]>0) break;

        if(i>0)
        {
            t1=mp[i-1][y];
            t2=mp[i][y];
            mp[i-1][y]+=mp[i][y]-1;
            mp[i][y]=0;

            ans[cnt]='U';

            dfs(i,y,cnt+1);

            mp[i-1][y]=t1;
            mp[i][y]=t2;
        }
        else if(!i)
        {
            mp[i][y]--;

            ans[cnt]='U';

            if(mp[i][y]) dfs(1,y,cnt+1);
            else dfs(0,y,cnt+1);

            mp[i][y]++;
        }
    }

    if(y-1>0 && !mp[x][y-1])//L
    {
        for(i=y-2;i>=0;i--) if(mp[x][i]>0) break;

        if(i>0)
        {
            t1=mp[x][i-1];
            t2=mp[x][i];
            mp[x][i-1]+=mp[x][i]-1;
            mp[x][i]=0;

            ans[cnt]='L';

            dfs(x,i,cnt+1);

            mp[x][i-1]=t1;
            mp[x][i]=t2;
        }
        else if(!i)
        {
            mp[x][i]--;

            ans[cnt]='L';

            if(mp[x][i]) dfs(x,1,cnt+1);
            else dfs(x,0,cnt+1);

            mp[x][i]++;
        }
    }

    if(y+1<m-1 && !mp[x][y+1])//R
    {
        for(i=y+2;i<m;i++) if(mp[x][i]>0) break;

        if(i<m-1)
        {
            t1=mp[x][i+1];
            t2=mp[x][i];
            mp[x][i+1]+=mp[x][i]-1;
            mp[x][i]=0;

            ans[cnt]='R';

            dfs(x,i,cnt+1);

            mp[x][i+1]=t1;
            mp[x][i]=t2;
        }
        else if(i==m-1)
        {
            mp[x][i]--;

            ans[cnt]='R';

            if(mp[x][i]) dfs(x,m-2,cnt+1);
            else dfs(x,m-1,cnt+1);

            mp[x][i]++;
        }
    }

    if(x+1<n-1 && !mp[x+1][y])//D
    {
        for(i=x+2;i<n;i++) if(mp[i][y]>0) break;

        if(i<n-1)
        {
            t1=mp[i+1][y];
            t2=mp[i][y];
            mp[i+1][y]+=mp[i][y]-1;
            mp[i][y]=0;

            ans[cnt]='D';

            dfs(i,y,cnt+1);

            mp[i+1][y]=t1;
            mp[i][y]=t2;
        }
        else if(i==n-1)
        {
            mp[i][y]--;

            ans[cnt]='D';

            if(mp[i][y]) dfs(n-2,y,cnt+1);
            else dfs(n-1,y,cnt+1);

            mp[i][y]++;
        }
    }
}

int main()
{
    int i,j;

    while(~scanf("%d%d",&m,&n))
    {
        for(i=0;i<n;i++) scanf("%s",mp[i]);

        total=flag=0;

        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(mp[i][j]!='.')
                {
                    total+=mp[i][j]-'a'+1;

                    mp[i][j]=mp[i][j]-'a'+1;
                }
                else mp[i][j]=0;
            }
        }

        for(i=0;i<n && !flag;i++)
        {
            for(j=0;j<m && !flag;j++)
            {
                if(!mp[i][j])
                {
                    sx=i;
                    sy=j;
                    dfs(i,j,0);
                }
            }
        }
    }
}