HDU 2821 Pusher
来源:互联网 发布:软件取得的阶段性成果 编辑:程序博客网 时间:2024/05/29 17:42
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.
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
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. 按照规则来,状态数应该非常少,所以直接枚举起点,然后dfs就好了。#include<queue>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N=30;int n,m,len;char s[N],ans[N*N],g[4]={'D','U','R','L'};int d[4][2]={{1,0},{-1,0},{0,1},{0,-1}};struct mp{ int a[N][N];};bool dfs(int x,int y,int D,mp now){ if (len==D) return true; for (int i=0,j;i<4;i++) { if (now.a[x+d[i][0]][y+d[i][1]]) continue; for (j=1;!now.a[x+d[i][0]*j][y+d[i][1]*j];j++); if (now.a[x+d[i][0]*j][y+d[i][1]*j]>0) { mp nt=now; if (nt.a[x+d[i][0]*(j+1)][y+d[i][1]*(j+1)]==-1) break; nt.a[x+d[i][0]*(j+1)][y+d[i][1]*(j+1)]+=nt.a[x+d[i][0]*j][y+d[i][1]*j]-1; nt.a[x+d[i][0]*j][y+d[i][1]*j]=0; ans[D]=g[i]; if (dfs(x+d[i][0]*j,y+d[i][1]*j,D+1,nt)) return true; } } return false;}int main(){ while (~scanf("%d%d",&m,&n)) { mp now; len=0; memset(now.a,-1,sizeof(now.a)); for (int i=1;i<=n;i++) { scanf("%s",s+1); for (int j=1;j<=m;j++) { now.a[i][j]=s[j]=='.'?0:s[j]-'a'+1; len+=now.a[i][j]; } } int flag=0,x,y; for (int i=1;i<=n&&!flag;i++) { for (int j=1;j<=m&&!flag;j++) { if (!now.a[i][j]) { flag=dfs(i,j,0,now); x=i,y=j; } } } ans[len]=0; printf("%d\n%d\n%s\n",x-1,y-1,ans); } return 0;}
0 0
- HDU 2821 Pusher
- DFS-hdu-2821-Pusher
- HDU 2821 Pusher
- HDU-2821-Pusher(DFS)
- HDU 2821 Pusher
- HDU 2821 Pusher
- HDU 2821--Pusher
- hdu 2821 Pusher DFS
- HDU 2821 Pusher
- hdu 2821 pusher 4.3.7
- hdu 2821 Pusher 附几组数据
- hdu 2821 Pusher (dfs)
- hdu 2821 Pusher(dfs)
- hdu 2821 Pusher (水dfs)
- 【搜索】 HDU 2821 Pusher DFS
- HDU 2821Pusher(DFS )
- hud-2821-Pusher
- (HDU 2821)Pusher DFS + 在一个方向上可以移动多位
- js 中对象--属性相关操作
- Fibonacci sequence 纪中 1240 矩阵乘法
- 对express中next函数的一些理解
- 内联函数和宏定义的差别
- makefile学习笔记1
- HDU 2821 Pusher
- mysql_unbuffered_query与mysql_query的区别
- Foxmail中QQ企业邮箱只能收取不能发送邮件
- IC卡(Integrated Circuit Card,集成电路卡)(一)
- 一个JavaWeb工程师的进阶之路
- pta 还原二叉树
- (电工基地笔记)介绍用Vivado制作FPGA工程
- 如何基于Docker快速搭建多节点Hadoop集群
- MAC Ruby版本需要升级至2.2.2以上