程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU-2821-Pusher(DFS)

HDU-2821-Pusher(DFS)

編輯:C++入門知識

HDU-2821-Pusher(DFS)


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
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.
 
\
<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcHJlPgoKIAo8YnI+ClNvdXJjZQoyMDA5IE11bHRpLVVuaXZlcnNpdHkgVHJhaW5pbmcgQ29udGVzdAogMSAtIEhvc3QgYnkgVEpVIAo8cD48YnI+CjwvcD4KPHA+y7zCt6O6vPK1pURGU6GjuPjI9LjJuPa/ydbYtf61xCYjMjY2ODQ719OjrMbwteO/ydLUyM7S4tGh1PGjrL/J0tTN+cvEuPa3vc/y0rvWsdfftb3T0CYjMjY2ODQ719O1xLXYt73Oqta5o6zDv8X20ru0zr7Nz/u19NK7uPYmIzI2Njg0O9fTo6yyosfSsNHKo8/CtcTSxrW9z8LSu7j2zrvWw6GjPC9wPgo8cD7XotLio7qi2cbwteOyu8Tc09AmIzI2Njg0O9fToaOi2rHY0OvSqrj00ru49s671sOyxcTcxfaho6Lbxfa1xCYjMjY2ODQ719PU2rHfyc/KsaOsyqPPwrXEJiMyNjY4NDvX07K7xNzSxrP2vtjQzre2zqeho6LcJiMyNjY4NDvX09Tasd/Jz8qxo6zF9tauuvPI57n7u7nT0CYjMjY2ODQ719PKo9Pgo6xwdXNoZXK1xM671sO+zbK71Nqx38nPo6y38dTyvs3U2rHfyc+hozwvcD4KPHA+PGJyPgo8L3A+CjxwPjwvcD4KPHByZSBjbGFzcz0="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%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+10) break; if(i0) break; if(i

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved