码迷,mamicode.com
首页 > 其他好文 > 详细

HDU-2821-Pusher(DFS)

时间:2014-08-02 23:27:34      阅读:342      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   io   for   cti   

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.)
bubuko.com,布布扣


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.
bubuko.com,布布扣
 

Source


思路:简单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);
                }
            }
        }
    }
}


HDU-2821-Pusher(DFS),布布扣,bubuko.com

HDU-2821-Pusher(DFS)

标签:des   style   http   color   os   io   for   cti   

原文地址:http://blog.csdn.net/faithdmc/article/details/38352683

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!