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 ...
随机推荐
- es6-promise.auto.js
使用sweetalert2的IE浏览器报错,导入文件 链接:https://pan.baidu.com/s/1mOcsN_o8m-7I7Rej1NPkiw 提取码:9xsj
- [MYSQL笔记0]MYSQL的安装
mysql是一种关系型数据库管理系统.以mysql5.7版本为例,安装过程如下: 首先百度出mysql的官网,进入:(以下是自己安装失败的过程,直接下拉最后看大佬的安装过程吧,就是那个红红的网址) 找 ...
- form表单submit按钮提交页面不跳转
方案一 <html> <body> <form action="" method="post" target="nm_i ...
- ethereum(以太坊)(十)--函数修饰符
pragma solidity ^0.4.0; contract modifierTest{ uint public v1; uint constant v2 =10; //uint constant ...
- Oauth2.0协议 http://www.php20.com/forum.php?mod=viewthread&tid=28 (出处: 码农之家)
概要 OAuth2.0是OAuth协议的下一版本,但不向后兼容OAuth 1.0即完全废止了OAuth1.0. OAuth 2.0关注客户端开发者的简易性.要么通过组织在资源拥有者和HTTP服 ...
- Google Compute Engine VM自动调节
现象:利用google云搭建VM服务,在搭建实例组有一个"自动调节"功能,可以自动添加/删除MV,当自动添加VM时可能新添加的VM就是一个新的VM,你部署的代码或者环境都没了.现在 ...
- 《python编程从入门到实践》第七章笔记
用户输入和while循环 1.函数input():让程序停止运行,等待用户输入一些文本.接受一个参数,既即要向用户显示的提示或说明. 2.将数值输入用于计算和比较前,务必将其转换为数值表示. 3.fo ...
- 关于SSM框架项目中jsp页面EL表达式使用的一些疑问(一)
问题 ssm框架整合中,jsp页面中EL表达式所引用的对象“page”可以在controller中使用mav.addObject(“page”,pag )进行添加,如果省略mav.addObject( ...
- 大话CNN经典模型:VGGNet
2014年,牛津大学计算机视觉组(Visual Geometry Group)和Google DeepMind公司的研究员一起研发出了新的深度卷积神经网络:VGGNet,并取得了ILSVRC20 ...
- issubclasss/type/isinstance/callable/super
issubclass() : 方法用于判断第一个参数是否是第二个参数的子子孙孙类. 语法:issubclass(sub, super) 检查sub类是否是 super 类的派生类 class A: p ...