您的位置:首页 > 其它

HDU 2821 Pusher

2017-07-18 16:29 453 查看

Pusher

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others)

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

//读题有点费劲,自己YY了一下午还是WA,最后还是看了看题解
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int MAXN = 30;
int C,R,num[MAXN][MAXN];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
char Dir[] = "DURL";
int rec[1000],ansx,ansy,all;
bool flag;
bool Judge(int x,int y){
if(x<0||x>=R||y<0||y>=C) return false;
else return true;
}
void DFS(int x,int y,int anssum){
if(all==anssum){ flag=true; return ; }
if(flag) return;
for(int i=0;i<4&&!flag;i++){
if(num[x+dir[i][0]][y+dir[i][1]]) continue;//紧挨着格子
for(int j=1;!flag;j++){
int x3=x+(j+2)*dir[i][0],y3=y+(j+2)*dir[i][1];
if(!Judge(x3,y3)) break;
int x1=x+j*dir[i][0],y1=y+j*dir[i][1];
int x2=x+(j+1)*dir[i][0],y2=y+(j+1)*dir[i][1];
if(num[x2][y2]>0&&num[x1][y1]==0){
rec[anssum]=i;
int old=num[x2][y2];num[x3][y3]+=(old-1);num[x2][y2]=0;
DFS(x2,y2,anssum+1);
num[x2][y2]=old;num[x3][y3]-=(old-1);
break;
}
}
}
}
int main(){
while(scanf("%d%d",&C,&R)!=EOF){
getchar();all=0;
for(int i=0;i<R;i++){
for(int j=0;j<C;j++){
char c=getchar();
if(c>='a'&&c<='z'){
num[i][j]=(c-'a'+1);all+=num[i][j];
}
else num[i][j]=0;
}
getchar();
}
flag=false;
for(int i=0;i<R&&!flag;i++){
for(int j=0;j<C&&!flag;j++){
if(num[i][j]==0){
for(int k=0;k<4&&!flag;k++){
int x=i+dir[k][0],y=j+dir[k][1];
int x2=i+2*dir[k][0],y2=j+2*dir[k][1];
int x
4000
3=i+3*dir[k][0],y3=j+3*dir[k][1];
if(!Judge(x3,y3)) continue;
if(num[x2][y2]>0&&num[x][y]==0){
memset(rec,-1,sizeof rec );
rec[0]=k;
int old=num[x2][y2];
num[x3][y3] += (old-1);
num[x2][y2]=0;
DFS(x2,y2,1);
if(flag) ansx=i,ansy=j;
num[x2][y2]=old;
num[x3][y3] -= (old-1);
}
}
}
}
}
printf("%d\n%d\n",ansx,ansy);
for(int i=0;i<all;i++) printf("%c",Dir[rec[i]]);
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  搜索