PusherTime
limit:2000/1000 MS (java/others) Memory limit:32768/65536 K (java/others)
Total submission (s): 903 Accepted Submission (s): 327
Special Judge
Problem Descriptionpusherboy is an online game http://www.hacker.org/push. There is a R * C grid, and there is piles of blocks on some positions. The goal is to clear the blocks by pushing into them.
You should choose a empty area as the initial position of the pusherboy. Then you can choose which direction (U-up, D for-down, L for-left and R-right) to push. Once the direction is chosen, the Pusherboy would walk ahead until he met a pile of blocks (Walking outside the grid is INV Alid). Then he remove one block from the pile (so if the pile contains only one block, it would become empty), and push the remain ing pile of blocks to the next area. (If There has been some blocks in the next area, the both piles would form a new big pile.)
Please note if the pusher are right up against the block, he can ' t remove and push it. That's, there must be a gap between the pusher and the pile. As the following, 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 the blocks.)
And if a whole pile is pushed outside the grid, it'll be considered as cleared.
Inputthere is several test cases in each input. The first and lines of each case contain a numbers C and R. (R,c <=) then r lines follow, indicating the grid. '. ' stands for the empty area, and a lowercase letter stands for a pile of blocks. (' A ' for-one block, ' B ' for the blocks, ' C ' for three, and so on.)
Outputoutput three lines for each case. The first and lines contains, 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'll 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 nested squares indicating a pile of the blocks). And this is the unique solution.
Source2009 multi-university Training Contest 1-host by Tju
#include <stdio.h> #include <iostream> #include <queue>using namespace std;struct node{int sx,sy; int x,y,n,blocksnumb; Char path[200]; Char map[25][25];}; Char map[27][27];int r,c,dir[4][2]={-1,0,1,0,0,-1,0,1};char dirchar[4]={' U ', ' D ', ' L ', ' R '};int judge (node &tp,int e) {tp.path[tp.n]=dirchar[e]; tp.path[tp.n+1]= ' + '; tp.n++; TP.X=TP.X+DIR[E][0]; TP.Y=TP.Y+DIR[E][1]; if (tp.map[tp.x][tp.y]!= '. ') return 0; while (true) {tp.x=tp.x+dir[e][0]; TP.Y=TP.Y+DIR[E][1]; if (tp.x<0| | tp.x>=r| | tp.y<0| | TP.Y>=C) return 0; if (tp.map[tp.x][tp.y]!= '. ') {if (tp.map[tp.x][tp.y]> ' a ') {tp.map[tp.x][tp.y]-=1; tp.blocksnumb--; int x=tp.x+dir[e][0]; int y=tp.y+dir[e][1]; if (x>=0&&x<r&&y>=0&&y<c) {if (tp.map[x][y]== '. ') TP.MAP[X][Y]=TP.MAP[TP.X][TP.Y]; Else tp.map[x][y]+=tp.map[tp.x][tp.y]-' a ' +1; } else tp.blocksnumb-=tp.map[tp.x][tp.y]-' a ' +1; } else tp.blocksnumb--; Tp.map[tp.x][tp.y]= '. '; return 1; }}}void BFs () {queue<node>q; Node p,tp,pp; int blocksnumb=0; for (int i=0;i<r;i++) for (int j=0;j<c;j++) {pp.map[i][j]=map[i][j]; if (map[i][j]!= '. ') blocksnumb+=map[i][j]-' a ' +1; } Pp.blocksnumb=blocksnumb; pp.n=0; for (int i=0;i<r;i++) for (int j=0;j<c;j++) if (map[i][j]== '. ')//enumerate each starting position {pp.sx=i; p P.sy=j; pp.x= i; Pp.y= J; Q.push (PP); while (!q.empty ()) {P=q.front (); Q.pop (); for (int e=0;e<4;e++) {tp=p; if (judge (Tp,e)) {if (tp.blocksnumb==0) {printf ("%d\n%d\n%s\n", Tp.sx,tp.sy,tp.path); return; } q.push (TP); }}}}}int main () {while (scanf ("%d%d", &c,&r) >0) {for (int i=0;i& Lt r;i++) scanf ("%s", Map[i]); BFS (); }}
Hdu2821pusher (Bfs+ enumeration)