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.
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 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.
3
7
…
…
.b.
…
…
.a.
…
4
1
UDU
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.
这道题有毒(o(>﹏<)o)
在ACM steps中的4.3.8和这道题一样,但是同样的代码在ACM steps中就能AC,在http://acm.hdu.edu.cn/showproblem.php?pid=2821 里就是WA,好郁闷啊!!纠结了一下午
说说题意吧:n*m的格子上有一些方块放在某些格子上,一个格子可能有几个方块,用’a’-‘z’来表示方块数量。
初始的时候可以选择任意空地作为Pusher初始点,Pusher选择一个方向,然后向这个方向前进直到遇到有方块的格子,Pusher把这个格子的方块清除一个,并把它向前推一格(向前不能出界),如果前面一格有方块,那么这些方合起来放在这个格子中。Pusher和有方块的格子之间至少要有一个空地才能推动
分析:直接dfs,因为是special judge 找到一组解即可。
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#define N 28
const int mm = 1000000007;
using namespace std;
const int dir[4][2]={1, 0, -1, 0, 0, 1, 0, -1};
char dd[] = "DURL";
char map[N][N], str[1111];
int r, c, all;
bool flag;
void dfs(int x, int y, int clear)
{
int i, xx, yy, tmp;
if (clear == all)
{
flag = true;
str[clear] = 0;
return ;
}
for (i = 0; i < 4; i++)
{
xx = x + dir[i][0];
yy = y + dir[i][1];
if (map[xx][yy] || xx < 0 || xx >= r || yy < 0 || yy >= c) continue;
while(xx >= 0 && xx < r && yy >= 0 && yy < c && !map[xx][yy])
xx += dir[i][0], yy += dir[i][1];
xx += dir[i][0], yy += dir[i][1];
if (xx < 0 || xx >= r || yy < 0 || yy >= c) continue;
tmp = map[xx-dir[i][0]][yy-dir[i][1]];
map[xx][yy] += tmp - 1;
map[xx-dir[i][0]][yy-dir[i][1]] = 0;
str[clear] = dd[i];
dfs(xx-dir[i][0], yy-dir[i][1], clear+1);
if (flag) return ;
map[xx][yy] -= tmp - 1;
map[xx-dir[i][0]][yy-dir[i][1]] = tmp;
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int i, j, t;
while(cin >> c >> r)
{
all = 0;
for(i = 0; i < r; i++)
{
cin >> map[i];
for (j = 0; j < c; j++)
{
if (map[i][j] == '.') map[i][j] = 0;
else map[i][j] -= 'a'-1;
all += map[i][j];
}
}
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
flag = false;
dfs(i, j, 0);
if (flag)
{
cout << i << endl << j << endl << str << endl;
}
}
}
}
return 0;
}