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. 写给iOS小白的MVVM教程(一): 从MVC到MVVM之一个典型的MVC应用场景

    前言 本着实践为主的原则,此系列文章不做过多的概念性的阐述和讨论;更多的代码和篇幅用来展示MVC和MVVC下的基础代码结构与具体实现,来展示各自优劣.这篇文章,更多的在于发掘MVC与MVVC的共性,以 ...

  2. 爬虫学习(十五)——json解析

    json与jsonpath 对象{}:jsonobject 对象:对象在js中表现为{}括起来的内容,数据结构为{key:value,key:value...}键值对的结构,在面向对象的结构中,key ...

  3. 硬件中断--DEBUG系列

    问题描述: 在线调试时,全速运行,程序进入硬件中断,查看堆栈窗口,发现是从A函数进去的.但是A函数应该没有问题的: 再次重复,发现是从B函数进去的,但是B函数之前运行起来也没有问题的,而且没有传入参数 ...

  4. 笔记-爬虫-selenium常用方法

    笔记-爬虫-selenium常用方法 1.      查找元素 常用的查找方法 find_element_by_name find_element_by_xpath find_element_by_l ...

  5. 笔记-select,poll,epoll

    笔记-select,poll,epoll 1.      I/O多路复用 I/O多路复用是指:通过一种机制或一个进程,可以监视多个文件描述符,一旦描述符就绪(写或读),能够通知程序进行相应的读写操作. ...

  6. java实时监听日志写入kafka

    目的 实时监听某目录下的日志文件,如有新文件切换到新文件,并同步写入kafka,同时记录日志文件的行位置,以应对进程异常退出,能从上次的文件位置开始读取(考虑到效率,这里是每100条记一次,可调整) ...

  7. P1418 选点问题(黑白染色)

    P1418 选点问题 题目描述 给出n个点,m条边,每个点能控制与其相连的所有的边,要求选出一些点,使得这些点能控制所有的边,并且点数最少.同时,任意一条边不能被两个点控制 输入输出格式 输入格式: ...

  8. getElementByName????????,????????,

    getElementByName可以获取多个元素,获得的是一个数组, getElementById只能获取一个,是dom从上往下的第一个元素.

  9. Android stadio 自定义debug release keystore

    1.添加siggnig name 随意,不过按我写的就可以了.设置完成之后,你的build.grade就会多出来一些: android { signingConfigs { signingConfig ...

  10. java前台传参json,后台用map或者实体对象接收

    (一)前台传js对象,里面包含数组,后台用map接收 (1)第一种情况:数组里不包含js对象 var param ={}: param.id=id; param.name=name; var scor ...