Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u

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


思路:枚举起点,DFS。其实起点的设定虽然有条件限制,实际操作的时候是没必要判断的,因为不符合条件的起点扩展不出去,自然就筛掉了。

AC代码:

#include <cstdio>
#include <cstring>
using namespace std;
int c, r;
int map[30][30];
int dir[900];
int tot;
bool ok;
const int dx[] = {0, 0, 1, -1},
			dy[] = {1, -1, 0, 0};
const char dirch[] = "RLDU";
void init()
{
	memset(map, 0, sizeof(map));
	tot = 0;
	ok = 0;
}
void input()
{
	char tem[30];
	for (int i = 0; i < r; i++)
	{
		scanf("%s", tem);
		for (int j = 0; j < c; j++)
			if (tem[j] != '.')
			{
				map[i][j] = tem[j] - 'a' + 1;
				tot += map[i][j];
			}
	}
}
inline bool check(int x, int y)
{
	if (x < 0 || x >= r || y < 0 || y >= c)
		return 0;
	return 1;
}
void dfs(int x, int y, int cur)
{
	if (cur >= tot)
	{
		ok = 1;
		return;
	}
	for (int i = 0; i < 4; i++)
	{
		int newx = x + dx[i],
			newy = y + dy[i];
		if (!check(newx, newy) || map[newx][newy])
			continue;
		while (check(newx, newy) && !map[newx][newy])
			newx += dx[i], newy += dy[i];
		if (!check(newx + dx[i], newy + dy[i]))
			continue;
		int now = map[newx][newy];
		map[newx + dx[i]][newy + dy[i]] += now - 1;
		map[newx][newy] = 0;
		dir[cur] = i;
		dfs(newx, newy, cur + 1);
		if (ok)
			return;
		map[newx][newy] = now;
		map[newx + dx[i]][newy + dy[i]] -= now - 1;
	}
}
void solve()
{
	for (int i = 0; i < r; i++)
	{
		for (int j = 0; j < c; j++)
			if (!map[i][j])
			{
				dfs(i, j, 0);
				if (ok)
				{
					printf("%d\n%d\n", i, j);
					for (int k = 0; k < tot; k++)
						printf("%c", dirch[dir[k]]);
					printf("\n");
					break;
				}
			}
		if (ok)
			break;
	}
}
int main()
{
	while (scanf("%d%d", &c, &r) == 2)
	{
		init();
		input();
		solve();
	}
	return 0;
}

 

分类: ACM/ICPC

发表评论

电子邮件地址不会被公开。 必填项已用*标注