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
题目大意:在R*C的网格里,有的地方有石块,用a~z表示1~26个石块,每次只能隔一个网格清除一个石块,然后把剩下的往前移,注意每次只能选一个方向,直到碰到石块位置,如果朝这个方向一直走没遇到石块而出界了,那么这个方向就是不对的,再选另一个方向,直到所有的石块全被处理掉,只要找到一种方法就直接输出,这题细节挺多的

解题思路:这是典型的dfs,不过我自己还不会写,我看了别人的然后自己写的,有什么不当的望各位大牛指出;

AC代码:

#include<stdio.h>
char p[4]={'R','L','U','D'};
int dir[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
char path[1024];//这个是用来记录每次行走的方向
int map[30][30];//在每个点石块的数量
char a[30][30];
int r,c,sum;
int check(int x,int y)//判断是否越界
{
if(x>=0&&x<r&&y>=0&&y<c) return 1;
else return 0;
}
int dfs(int x,int y,int s)
{
int i,tx,ty,k;
if(s==sum)//全部清理完了
{
path[s]='\0';
return 1;
}
for(i=0;i<4;i++)
{
tx=x+dir[i][0];
ty=y+dir[i][1];
if(!check(tx,ty)||map[tx][ty]) continue;//越界了或者该点是石块
while(check(tx,ty)&&!map[tx][ty])//一直往前走遇到石块或者越界
{
tx+=dir[i][0];
ty+=dir[i][1];
}
if(check(tx,ty)==0) continue;//越界了方向不对
k=map[tx][ty];
if(k>1&&check(tx+dir[i][0],ty+dir[i][1])==0) continue;//不止一个石块而且往前一步就越界了
if(k>1) map[tx+dir[i][0]][ty+dir[i][1]]+=k-1;
map[tx][ty]=0;
path[s]=p[i];//记录方向
if(dfs(tx,ty,s+1)) return 1;
map[tx][ty]=k;//还原
map[tx+dir[i][0]][ty+dir[i][1]]-=(k-1);
}
return 0;
}
int main()
{
int i,j;
while(scanf("%d%d",&c,&r)!=EOF)
{
sum=0;
for(i=0;i<r;i++)
{
getchar();
scanf("%s",a[i]);
for(j=0;j<c;j++)
{
if(a[i][j]=='.') map[i][j]=0;
else map[i][j]=a[i][j]-'a'+1;
sum+=map[i][j];//表示十块的总数量
}
}
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
if(map[i][j]==0)
if(dfs(i,j,0))
{
printf("%d\n%d\n%s\n",i,j,path);
i=r;
j=c;
}
}
}
return 0;
}

hdu 2821 Pusher(dfs)的更多相关文章

  1. hdu 2821 Pusher (dfs)

    把这个写出来是不是就意味着把   http://www.hacker.org/push  这个游戏打爆了? ~啊哈哈哈 其实只要找到一个就可以退出了  所以效率也不算很低的  可以直接DFS呀呀呀呀 ...

  2. HDU 5965 扫雷(dfs)题解

    题意:给你一个3*n的格子,中间那行表明的是周围8格(当然左右都没有)的炸弹数量,上下两行都可以放炸弹,问你有几种可能,对mod取模 思路:显然(不),当i - 1和i - 2确定时,那么i的个数一定 ...

  3. HDU 1518 Square(DFS)

    Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-end ...

  4. HDU 1015 Safecracker (DFS)

    题意:给一个数字n(n<=12000000)和一个字符串s(s<=17),字符串的全是有大写字母组成,字母的大小按照字母表的顺序,比如(A=1,B=2,......Z=26),从该字符串中 ...

  5. Hdu 1175 连连看(DFS)

    Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1175 因为题目只问能不能搜到,没问最少要几个弯才能搜到,所以我采取了DFS. 因为与Hdu ...

  6. HDU 1501 Zipper(DFS)

    Problem Description Given three strings, you are to determine whether the third string can be formed ...

  7. HDU 5305 Friends(dfs)

    Friends Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su ...

  8. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

  9. HDU 5934 Bomb(炸弹)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

随机推荐

  1. iOS 之 多线程一

    iOS中实现多线程的技术方案 pthread 实现多线程操作 代码实现: void * run(void *param) {    for (NSInteger i = 0; i < 1000; ...

  2. android SurfaceView绘制 重新学习--控制动画移动

    直接上demo,图是自己切的,将就用吧.点击左右两边分别向左右移动. public class MySurfaceView extends SurfaceView implements Callbac ...

  3. LightOj_1317 Throwing Balls into the Baskets

    题目链接 题意: 有N个人, M个篮框, 每个人投进球的概率是P. 问每个人投K次后, 进球数的期望. 思路: 每个人都是相互独立的, 求出一个人进球数的期望即可. 进球数和篮框的选择貌似没有什么关系 ...

  4. spring mvc 配置

    之前配置spring mvc 怎么都访不到对应的jsp,后来把prefix里面的jsp改为views,就能访问到了,然后再改回jsp也可以访问到 搞了两天,都崩溃了,不管怎样先把没问题的例子给记录下来 ...

  5. Bootstrap 轮播(Carousel)插件

    Bootstrap 轮播(Carousel)插件是一种灵活的响应式的向站点添加滑块的方式.除此之外,内容也是足够灵活的,可以是图像.内嵌框架.视频或者其他您想要放置的任何类型的内容. 如果您想要单独引 ...

  6. 限制sqlserver最大内存后无法连接-EXEC sp_configure max server memory

    在sql server 中设置了过小的 "max server memory"最大内存后,sqlserver可启动,但是无法连接. 网络上流行的"sqlserver 内存 ...

  7. 反射实体自动生成EasyUi DataGrid模板 第二版--附项目源码

    之前写过一篇文章,地址 http://www.cnblogs.com/Bond/p/3469798.html   大概说了下怎么通过反射来自动生成对应EasyUi datagrid的模板,然后贴了很多 ...

  8. 玩转createjs

    标题党"玩转", 真的是在玩怎么转... 参考一篇很经典的博文:createjs入门 做移动版(750x1334)的时候出来不居中啊, 不是掉在下面就是滑到右面, canvas里面 ...

  9. VMware配置回环地址用于测试

           我们在开发过程中,很可能需要一台服务器用于测试,在这种环境下,我们很可能需要用到vmware来构建这样的开发环境.但如果当前处在一个离线,或是不在网内的环境下,我们所搭建的环境有可能无法 ...

  10. Team Foundation Server操作说明

    目录 一. Visual Studio 2010团队协作管理 1.1团队模型及角色 1.1.1创建角色 1.1.2创建工作组 1.1.3工作组成员及权限 1.2团队成员利用VS2010实现协同办公 1 ...