hdu 2821 Pusher (dfs)
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
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.
7
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.
题意:
做了这题后告诉我一个真理,题意真的很重要!!
开始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)的更多相关文章
- hdu 2821 Pusher(dfs)
Problem Description PusherBoy is an online game http://www.hacker.org/push . There is an R * C grid, ...
- hdu 2821 Pusher (dfs)
把这个写出来是不是就意味着把 http://www.hacker.org/push 这个游戏打爆了? ~啊哈哈哈 其实只要找到一个就可以退出了 所以效率也不算很低的 可以直接DFS呀呀呀呀 ...
- HDU 2821 Pusher
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2821 首先,题目描述给的链接游戏很好玩,建议先玩几关,后面越玩越难,我索性把这道题A了,也就相当于通关 ...
- hdu 3500 Fling (dfs)
Fling Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submi ...
- HDU.5692 Snacks ( DFS序 线段树维护最大值 )
HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...
- hdu 2821(dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2821 思路:一开始的时候没注意到,必须从map[i][j]==0的位置开始,然后就是dfs了,回溯的时 ...
- hdu 2821 学习一点dfs的小技巧吧。。 还是自己太弱了
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int r,c ...
- hdu 5727 Necklace dfs+二分图匹配
Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...
- hdu 4499 Cannon dfs
Cannon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4499 D ...
随机推荐
- 使用 W3C Performance 对象通过 R 和 JavaScript 将浏览器内的性能数据可视化[转]
当考虑 Web 性能指标时,需要关注的目标数字应该是从您自己的用户那里获得的实际用户指标.最常见的方法是利用 Splunk 之类的工具来分析您的机器数据,该工具支持您分析和可视化您的访问权限和错误日志 ...
- PowerDesigner生成sql脚本
1.打开PowerDesigner->New Project; 2.填写项目名称,选择文件的存放路径: 3.新建一个模型,New Model: 4.选择概念模型,填写模型名称: 5.选择enti ...
- HTML5一些标签和属性
<bdo> 元素 可以覆盖默认文本的方向 根据dir 属性来控制文字的排序方向 属性:dir="rtl" ...
- 自定义动画函数JQuery实现
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- centos 7 ifconfig 命令找不到
最近在配置linux 环境: 在官网看到centOS除了最新版本7,那就尝试一下吧.最小安装centOS 7之后发现没有ifconfig命令,在网上找了一下都说是路径的路问题. 我用echo $PAT ...
- Yii2实现跨mysql数据库关联查询排序功能
遇到一个项目,需要跨表网上找了很多的资料,整理一下,方便以后再次使用 背景:在一个mysql服务器上(注意:两个数据库必须在同一个mysql服务器上)有两个数据库: memory (存储常规数据表) ...
- 天气预报api-汇总
和风天气 https://www.heweather.com/
- php图片上传旋转压缩方法
用到php的exif扩展,需要开启exif 在php.ini文件中去掉exif组件的注释 extension=php_mbstring.dll //要放在php_exif.dll前面让它先加载 ext ...
- My jdbc 错误
jdbc mysql插入数据提示Parameter index out of range (1 > number of parameters, which is 0). SqlStatement ...
- MAC下MySQL初始密码忘记修改初始密码
解决MAC下MySQL忘记初始密码的方法分享给大家,供大家参考,具体内容如下 第一步: 点击系统偏好设置->最下边点MySQL,在弹出页面中,点击stop MySQL Servier,输入密码关 ...