过题如练级。去成都打了个boss被完虐。so开始好好刷怪了……大概。
|
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others) |
Total Submission(s): 45 Accepted Submission(s): 20 |
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 |
Source
2009 Multi-University Training Contest 1 - Host by TJU
|
实在是……膜拜下……抄都抄的头晕……果然菜啊菜………………写了些注释……英文什么的只是懒得改输入法而已……
//枚举每一个有方块的格子周围距离为2的空地作为初始点
#include <iostream>
using namespace std;
int m,n;
char map[30][30];
int num;//the number of places of boxes,don't change
int cnt;//the number of places of boxes, change
int a[30][30];//show the situation of boxes
int dir[4][2]={0,1,0,-1,1,0,-1,0};//this is match to p[],can't write randomly!:P
char p[]="RLDU";
char path[1000];//record the path walked
int ok(int x,int y)//check if the place is empty and not beyond
{
if(x<0||y<0||x>=n||y>=m)//beyond
return 0;
if(a[x][y]==0)//no boxes
return 1;
return -1;//have boxes
}
//so if(ok())means there is boxes or flat
int dfs(int x,int y,int pos)//pos is the number of path
{
int i,xx,yy,tx,ty;
for(i=0;i<4;i++)
{
xx=x+dir[i][0];
yy=y+dir[i][1];
if(ok(xx,yy)!=1)
continue;
do//at least do once
{
xx+=dir[i][0];
yy+=dir[i][1];
}while(ok(xx,yy)==1);//(xx,yy) beyond or meet the boxes
if(ok(xx,yy)&&ok(tx=xx+dir[i][0],ty=yy+dir[i][1])&&a[xx][yy])//take care of the brackets!!! and tell between 1 and i!!! ...well, if you are tired and sleepy, just go to sleep . stop coding.please.
{
//(xx,yy) is box and next is not beyond
int t=a[xx][yy];
path[pos]=p[i];//record the path
if(t==1||a[tx][ty])//xx,yy is ONE box or next place have box
cnt--;//place of box minus 1
if(cnt==0)
{
num=pos;//record the whole number of path
return 1;
}
a[tx][ty]+=t-1;//next place update the situation of box
a[xx][yy]=0;//this place have no box
if(dfs(xx,yy,pos+1))
return 1;
//recovery
a[xx][yy]=t;
a[tx][ty]-=t-1;
if(t==1||a[tx][ty])
cnt++;
}
}
return 0;
}
int main()
{
while(cin>>m>>n)
{
int i,j,k;
int x,y;
int flag=1;//see if find a solution
int f[35][35]={0};//visited or not
num=0;
for(i=0;i<n;i++)
{
cin>>map[i];
for(j=0;j<m;j++)
{
if(map[i][j]>='a'&&map[i][j]<='z')
{
a[i][j]=map[i][j]-'a'+1;
num++;
}
else
a[i][j]=0;
}
}
for(i=0;i<n&&flag;i++)
for(j=0;j<m&&flag;j++)
if(a[i][j])//find a place where there is box(es)
for(k=0;k<4&&flag;k++)//find the places 2 lattice away from the box(es)
{
x=i+dir[k][0]*2;
y=j+dir[k][1]*2;
if(ok(x,y)>0&&!f[x][y])
{
f[x][y]=1;
cnt=num;
if(dfs(x,y,0))
flag=0;
}
}
cout<<x<<endl<<y<<endl;
for(i=0;i<=num;i++)
cout<<path[i];
cout<<endl;
}
}
什么时候能写出自己的dfs捏……