Pusher
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others)
Total Submission(s): 646 Accepted Submission(s): 228
Special Judge
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.
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.)
![](https://i-blog.csdnimg.cn/blog_migrate/ac3ba731e60c20995c876d9c47598c4d.jpeg)
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 UDUHintHint: 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
Recommend
gaojie
题意:
进去这个链接玩一下就知道题意了:http://www.hacker.org/push/
思路:
简单dfs,就不说了,注意一下把方块推出边界也算清理干劲就够了。
感想:
做的时候脑残了,搜到答案没退出,WA了,debug半天。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 30
using namespace std;
int n,m,ans,flag;
int sx,sy;
int dx[]= {-1,1,0,0};
int dy[]= {0,0,-1,1};
int r[maxn],c[maxn];
char dir[]={"UDLR"};
char mp[maxn][maxn];
char s[maxn],a[1005];
bool isok()
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(mp[i][j]>='a') return false ;
}
}
return true ;
}
void dfs(int nx,int ny,int pos)
{
if(flag) return ;
if(isok())
{
flag=1;
a[pos]='\0';
printf("%d\n%d\n%s\n",sx-1,sy-1,a);
return ;
}
int i,j,tx,ty;
char c1,c2;
for(i=0; i<4; i++)
{
tx=nx+dx[i];
ty=ny+dy[i];
if(mp[tx][ty]>='a') continue ;
while(tx>=1&&tx<=n&&ty>=1&&ty<=m)
{
tx+=dx[i];
ty+=dy[i];
if(mp[tx][ty]>='a') break ;
}
if(!(tx>=1&&tx<=n&&ty>=1&&ty<=m)) continue ;
c1=mp[tx+dx[i]][ty+dy[i]];
c2=mp[tx][ty];
if(mp[tx+dx[i]][ty+dy[i]]>='a') mp[tx+dx[i]][ty+dy[i]]+=mp[tx][ty]-'a';
else mp[tx+dx[i]][ty+dy[i]]=mp[tx][ty]-1;
mp[tx][ty]='.';
a[pos]=dir[i];
dfs(tx,ty,pos+1);
mp[tx+dx[i]][ty+dy[i]]=c1;
mp[tx][ty]=c2;
}
}
void solve()
{
int i,j;
flag=0;
for(i=1; i<=n; i++)
{
for(j=1; j<=m; j++)
{
if(flag) return ;
if(mp[i][j]>='a') continue ;
if(r[i]||c[j])
{
sx=i,sy=j;
dfs(i,j,0);
}
}
}
}
int main()
{
int i,j,t;
while(~scanf("%d%d",&m,&n))
{
memset(r,0,sizeof(r));
memset(c,0,sizeof(c));
t=0;
for(i=1; i<=n; i++)
{
scanf("%s",s);
for(j=1; j<=m; j++)
{
mp[i][j]=s[j-1];
if(mp[i][j]>='a') t=1,r[i]=1,c[j]=1;
}
}
solve();
}
return 0;
}