Pusher

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others)
Total Submission(s): 754    Accepted Submission(s): 264
Special Judge

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.

 
Source
 
Recommend
gaojie   |   We have carefully selected several similar problems for you:  2822 2819 2820 2818 2824 
 

题意:

做了这题后告诉我一个真理,题意真的很重要!!

开始WA以为自己搞错题意了,然后上网看别人解题报告里的题意,让我迷惑的就是在边界的撞过去会怎样,- -本来是觉得应该会飞出去,这样挺好的~~

后来忘了是什么原因错了,一直TLE,就怀疑这个想法(好像没关系...),看到别人的是不能有这种情况,就按此思路再写再改,后来还是一直WA;于是再看一解题报告,

发现本来的才是对的:http://www.2cto.com/kf/201208/149755.html

题意大概如此,有然若干个可能叠起的方格放在某些格子上,选择任意点作为始点,Pusher可以沿某方向前进把格子推向正对方向的前一格,每次能减少1个,如果前一格有方格就叠起,如果前面一格已出界,就全部推掉了,减少n个。求一个可行解的始点及其操作。

dfs:

弄清楚提议后就好写了,弄了半天,心力交瘁= =我的代码可以优化,不过暂时不想看它了。

暴力n*m个点然后深搜可行解!

 //15MS    240K    2251 B    G++
#include<stdio.h>
#include<string.h>
char c[]="RDLU";
int mov[][]={,,,,,-,-,};
char g[][];
char root[];
int n,m,flag,sum;
int sx,sy;
int judge(int x,int y)
{
if(x>=&&x<n&&y>=&&y<m) return ;
return ;
}
void dfs(int x,int y,int cnt,int s)
{
if(flag) return;
if(sum==s){
flag=; root[cnt]='\0'; return;
}
for(int i=;i<;i++){
int tx=x+mov[i][];
int ty=y+mov[i][];
if(!judge(tx,ty) || g[tx][ty]) continue;
while(judge(tx,ty) && !g[tx][ty]){
tx+=mov[i][];ty+=mov[i][];
}
if(judge(tx,ty) && g[tx][ty]){
int ta=tx+mov[i][];
int tb=ty+mov[i][];
root[cnt]=c[i];
int temp=g[tx][ty];
if(g[tx][ty]==){
g[tx][ty]=;
dfs(tx,ty,cnt+,s+);
if(flag) return;
g[tx][ty]=;
}else{
if(judge(ta,tb)){
g[ta][tb]+=temp-;
g[tx][ty]=;
dfs(tx,ty,cnt+,s+);
if(flag)return;
g[ta][tb]-=temp-;
g[tx][ty]=temp;
}else{
g[tx][ty]=;
dfs(tx,ty,cnt+,s+temp);
if(flag)return;
g[tx][ty]=temp;
}
}
}
}
return;
}
int main(void)
{
while(scanf("%d%d",&m,&n)!=EOF)
{
int i,j;
flag=;
sum=;
memset(g,,sizeof(g));
memset(root,,sizeof(root));
for(i=;i<n;i++){
scanf("%s",g[i]);
for(j=;j<m;j++){
if(g[i][j]=='.') g[i][j]=;
else{
g[i][j]=g[i][j]-'a'+;
sum+=g[i][j];
}
}
}
for(i=;i<n;i++) if(!flag)
for(int j=;j<m;j++){
sx=i;sy=j;
if(!g[i][j])
dfs(i,j,,);
if(flag){
printf("%d\n%d\n",i,j);
puts(root);
break;
}
}
}
return ;
}
/*
3
7
...
...
.b.
...
...
.a.
...
*/

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

  1. hdu 2821 Pusher(dfs)

    Problem Description PusherBoy is an online game http://www.hacker.org/push . There is an R * C grid, ...

  2. hdu 2821 Pusher (dfs)

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

  3. HDU 2821 Pusher

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2821 首先,题目描述给的链接游戏很好玩,建议先玩几关,后面越玩越难,我索性把这道题A了,也就相当于通关 ...

  4. hdu 3500 Fling (dfs)

    Fling Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submi ...

  5. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

  6. hdu 2821(dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2821 思路:一开始的时候没注意到,必须从map[i][j]==0的位置开始,然后就是dfs了,回溯的时 ...

  7. hdu 2821 学习一点dfs的小技巧吧。。 还是自己太弱了

    #include<iostream> #include<cstdio> #include<cstring> using namespace std; int r,c ...

  8. hdu 5727 Necklace dfs+二分图匹配

    Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...

  9. hdu 4499 Cannon dfs

    Cannon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4499 D ...

随机推荐

  1. 一篇SSM框架整合友好的文章(一)

    转载请标明出处: http://blog.csdn.net/forezp/article/details/53730333 本文出自方志朋的博客 最近实在太忙,之前写的<rxjava系列文章&g ...

  2. 谈谈两种标准库类型---string和vector

    两种最重要的标准库---string和vector string和vector是两种最重要的标准库类型,string表示可变长的字符序列,vector存放的是某种给定类型对象的可变长序列. 一.标准库 ...

  3. 【杂题总汇】NOIP2013(洛谷P1967) 货车运输

    [洛谷P1967] 货车运输 重做NOIP提高组ing... +传送门-洛谷P1967+ ◇ 题目(copy from 洛谷) 题目描述 A国有n座城市,编号从1到n,城市之间有m条双向道路.每一条道 ...

  4. Ubuntu安装MySQL及使用Xshell连接MySQL出现的问题(2003-Can't connect to MySql server及1045错误)

    不管在什么地方,什么时候,学习是快速提升自己的能力的一种体现!!!!!!!!!!! 以下所有的命令都是在root用户下操作(如果还没有设置root密码)如下: 安装好Ubuntu系统之后,打开终端先设 ...

  5. django+xadmin在线教育平台(十二)

    6-4 用form实现登录-1 上面我们的用户登录的方法是基于函数来做的.本节我们做一个基于类方法的版本. 要求对类的继承有了解. 基础教程中基本上都是基于函数来做的,其实更推荐基于类来做.基于类可以 ...

  6. NuGet管理和还原程序包

    在很多开源的程序下载下来不能使用,一般都是平台X86 和X64没有修改,还一个就是程序缺少资源包文件.用Nuget还原即可: 一般建议先修改好平台,然后用NuGet还原程序包.

  7. Linux实战教学笔记15:用户管理初级(下)

    第十四节 用户管理初级(下) 标签(空格分隔): Linux实战教学笔记-陈思齐 ---更多资料点我查看 1,用户查询相关命令id,finger,users,w,who,last,lastlog,gr ...

  8. webpack最小化lodash

    lodash作为一个比较常用的前端开发工具集,在使用webpack进行vendor分离的实践中,会遇到将整个lodash文件分离到vendor.js的问题.这样会使vendor.js文件变得特别大. ...

  9. (转)数据库老兵:NewSQL才是未来

    编者按:在数据库技术领域,Michael Stonebraker几乎是无人不知无人不晓的人物.现年70岁的Stonebraker不仅是Ingres和PostgreSQL的创始人,同时在Informix ...

  10. 阿里云Linux服务器,挂载硬盘并将系统盘数据迁移到数据盘

    因为之前用宝塔上线,宝塔只挂载了系统盘50G,打开阿里云云盘列表发现系统盘无法直接升级,故另买一块数据盘挂载到Linux服务器下,下面根据网上教程再结合我实际情况讲解一下实际操作,其实非常easy l ...