hdu 2821 Pusher (dfs)
把这个写出来是不是就意味着把 http://www.hacker.org/push 这个游戏打爆了? ~啊哈哈哈
其实只要找到一个就可以退出了 所以效率也不算很低的 可以直接DFS呀呀呀呀
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector> using namespace std; int dx[] = {-1,1,0,0};
int dy[] = {0,0,1,-1}; vector <char> ans;
int n,m;
char map[30][30];
char t[30][30];
bool found;
void copy() //每一次都需要复制一下地图
{
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
map[i][j]=t[i][j];
} void debug(int ax,int ay) //调试函数~~~
{
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(i==ax && j==ay){printf("@");continue;}
printf("%c",map[i][j]);
}
puts("");
}
puts("");
} bool ok(int xx,int yy)
{
if(xx<n && xx>=0 && yy<m && yy>=0)return true; return false;
} bool tag() //全部砖块都清除了
{
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(map[i][j]!='.')return false; return true;
} bool dfs(int posx,int posy)
{
if(found)return true;
if(tag())
{
found=true;
return true;
}
//debug(posx,posy);
for(int k=0;k<4;k++)
{
if(map[posx+dx[k]][posy+dy[k]]!='.' || !ok(posx+dx[k],posy+dy[k]))continue;
for(int i=1;i<=25;i++)
{
if(map[posx+i*dx[k]][posy+i*dy[k]]!='.' && ok(posx+i*dx[k],posy+i*dy[k]))
{
int tx=posx+i*dx[k];
int ty=posy+i*dy[k];
char cur1=map[tx][ty];
char cur2=map[tx+dx[k]][ty+dy[k]];
map[tx][ty]='.';
if(cur1!='a')
{
if(cur2=='.')
{
map[tx+dx[k]][ty+dy[k]]=cur1-1; //没有就减一级
}
else
{
map[tx+dx[k]][ty+dy[k]]=cur1-'a'+cur2; //如果后面有东西就要合体
}
} if(k==0)ans.push_back('U');
else if(k==1)ans.push_back('D');
else if(k==2)ans.push_back('R');
else ans.push_back('L'); dfs(tx,ty);
if(found)return true;
map[tx][ty]=cur1;
map[tx+dx[k]][ty+dy[k]]=cur2;
ans.pop_back(); break;
}
}
}
return false;
} int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
found=false;
ans.clear();
for(int i=0;i<n;i++)
scanf("%s",t[i]); for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(t[i][j]!='.')continue;
copy();
if(dfs(i,j))
{
printf("%d\n%d\n",i,j);
break;
}
}
if(found)break;
}
for(int sb=0;sb<ans.size();sb++)
printf("%c",ans[sb]);
puts("");
}
return 0;
} /*
13 3
.............
...bb..bc....
............. 7 8
.......
.......
.....b.
.......
.aa.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 5965 扫雷(dfs)题解
题意:给你一个3*n的格子,中间那行表明的是周围8格(当然左右都没有)的炸弹数量,上下两行都可以放炸弹,问你有几种可能,对mod取模 思路:显然(不),当i - 1和i - 2确定时,那么i的个数一定 ...
- HDU 1518 Square(DFS)
Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-end ...
- HDU 1015 Safecracker (DFS)
题意:给一个数字n(n<=12000000)和一个字符串s(s<=17),字符串的全是有大写字母组成,字母的大小按照字母表的顺序,比如(A=1,B=2,......Z=26),从该字符串中 ...
- Hdu 1175 连连看(DFS)
Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1175 因为题目只问能不能搜到,没问最少要几个弯才能搜到,所以我采取了DFS. 因为与Hdu ...
- HDU 1501 Zipper(DFS)
Problem Description Given three strings, you are to determine whether the third string can be formed ...
- HDU 5305 Friends(dfs)
Friends Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Su ...
- HDU 2553 N皇后问题(dfs)
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 在 ...
- HDU 5934 Bomb(炸弹)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
随机推荐
- Android Retrofit实现原理分析
retrofit有几个关键的地方. 1.用户自定义的接口和接口方法.(由动态代理创建对象.) 2.converter转换器.(把response转换为一个具体的对象) 3.注解的使用. 让我们跟随Ap ...
- 有趣的js题目
([]).length ({}).length // undefined ([] + {}).length ({} + {}).length 具体解释见此.
- Node.js的循环依赖
我们知道在实际编程过程中,要尽可能的减少或者规避循环依赖情况的发生.但在现实环境中,有时却不得不产生循环依赖.Node.js不提倡使用循环依赖,但真有如此情况发生时Node.js也有办法解决.这篇博文 ...
- 【转】Undefined symbols for architecture i386:和"_OBJC_CLASS_$_xx", referenced from:问题解决方法
多个人共同操作同一个项目或拷贝项目时,经常会出现类似这样的问题: Undefined symbols for architecture i386: "_OBJC_CLASS_$_xx文件名& ...
- beginUpdates和endUpdates
我们在做UITableView的修改,删除,选择时,需要对UITableView进行一系列的动作操作. 这样,我们就会用到 [tableView beginUpdates]; if (newCount ...
- Dev GridView 获取选中分组下的所有数据行 z
现在要在DevExpress 的GridView 中实现这样一个功能.就是判断当前的选中行是否是分组行,如果是的话就要获取该分组下的所有数据信息. 如下图(当选中红框中的分组行事.程序要获取该分组下的 ...
- Android开发中这些小技巧
http://blog.csdn.net/guxiao1201/article/details/40655661 http://blog.csdn.net/guxiao1201/article/det ...
- 用physdiskwrite在VMware虚拟机上安装m0n0wall【转】
在一台PC机上安装m0n0wall,相信大家都有经验.一般采用两种方法:1.在一台Windows XP或Windows 2000的PC上,下载physdiskwrite软件和m0n0wall映像文件( ...
- div错位解决IE6、IE7、IE8样式不兼容问题
IE6里DIV错位的问题 采用”FLOAT:LEFT“的DIV在IE8.IE7.都没问题,IE6下却向下移动,出现空白.这是因为,IE6采用的内核默认把DIV之间的距离增加了3~5个PX, ...
- C语言 百人拉百灯问题
题目: 有100人,编号从1到100; 另有100盏灯,编号也从 1到100. 现要求每人去拉能被自己编号整除的所有电灯, 例如编号为1者应把所有的灯都拉一遍, 编号为2者应把所有编号为偶数的灯都拉一 ...