HDU-2821-Pusher (DFS)

Source: Internet
Author: User
Problem descriptionpusherboy 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 shoshould 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.
Inputthere 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 .)

Outputoutput 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
Sample output
41UDUHintHint: 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. 
Source2009 multi-university training contest 1-host by tju

Idea: simple DFS. There are several overlapping grids. The starting point can be any choice. You can go to the place where there is a grid in four directions, and a grid will be removed every time you touch it, and move the rest to the next position.

Note: ① the start point cannot have a grid. ② It must be located separately before it can be touched. ③ When the grid is on the edge, the remaining grid cannot be out of the rectangle range. ④ When a grid is on the edge, if there is still a grid remaining after it is touched, pusher is not on the edge; otherwise, it is on the edge.

#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);                }            }        }    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.