HDU-2821-Pusher (DFS)
Problem DescriptionPusherBoy 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 shoshould 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.
InputThere 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 .)
OutputOutput 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
37.......b........a....
Sample Output
41UDUHintHint: 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.
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink">
Expires + expires + cve-vcd4kpha + expires/J0tTN + cvEuPa3vc/expires + hozwvcD4KPHA + PGJyPgo8L3A + expires = "brush: java; "> # include
Int n, m, total, sx, sy; char mp [25] [26], ans [10000]; bool flag; void dfs (int x, int y, int cnt) {int I, t1, t2; if (flag) return; if (cnt = total) {printf ("% d \ n", sx, sy ); ans [cnt] = 0; puts (ans); flag = 1; return;} if (x-1> 0 &&! Mp [x-1] [y]) // U {for (I = X-2; I> = 0; I --) if (mp [I] [y]> 0) break; if (I> 0) {t1 = mp [I-1] [y]; t2 = mp [I] [y]; mp [I-1] [y] + = mp [I] [y]-1; mp [I] [y] = 0; ans [cnt] = 'U '; dfs (I, y, cnt + 1); mp [I-1] [y] = t1; mp [I] [y] = t2;} else if (! I) {mp [I] [y] --; ans [cnt] = 'U'; if (mp [I] [y]) dfs (1, y, cnt + 1); else dfs (0, y, cnt + 1); mp [I] [y] ++ ;}} if (Y-1> 0 &&! Mp [x] [Y-1]) // L {for (I = y-2; I> = 0; I --) if (mp [x] [I]> 0) break; if (I> 0) {t1 = mp [x] [I-1]; t2 = mp [x] [I]; mp [x] [I-1] + = mp [x] [I]-1; mp [x] [I] = 0; ans [cnt] = 'l '; dfs (x, I, cnt + 1); mp [x] [I-1] = t1; mp [x] [I] = t2;} else if (! I) {mp [x] [I] --; ans [cnt] = 'l'; if (mp [x] [I]) dfs (x, 1, cnt + 1); else dfs (x, 0, cnt + 1); mp [x] [I] ++ ;}} if (y + 1
0) break; if (I
0) break; if (I