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中的所谓观察者模式
生活中我们常认定某些人很有才,但什么是有才呢?明朝的王守仁曾这样解释:才,是所谓天理,应用到物上,便成了才.凡事凡物,只要掌握了所谓科学的方法,并能灵活运用,那么你也可以成为一个有才的人. 观察者模式 ...
- C++调试 输出数组内容和数组名
#include <cstdio> using namespace std; //函数定义 #define printArr(arr,n,format) \ printf("%s ...
- C++ STL疑惑知识点
1.remove的问题 用法参考:http://www.cnblogs.com/heyonggang/p/3263568.html
- eclipse 报错汇总
1.Eclipse 启动时,报错: Fail to create the java virtual machine 已解决.方法:eclipse.ini 中-vmargs-Dosgi.requir ...
- air手势代码
//下列2句谁放上面谁生效要么触控生效,要么手势生效 Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT; Multitouch.inputM ...
- CSRF的攻击与防御(转)
add by zhj:CSRF之所有发生,是因为http请求中会自动带上cookies,我的解决办法是:前端不要将数据放在cookie中,而是放在其它本地存储 (HTML5中称之为Web Storag ...
- 2010“架构师接龙”问答--杨卫华VS赵劼(转)
add by zhj:虽然是几年前的文章,但还是很有参考价值的 原文:http://blog.zhaojie.me/2010/05/programmer-magazine-2010-5-archite ...
- vim之pydiction插件
[vim之pydiction插件] It consists of three main files: python_pydiction.vim -- Vim plugin. complete-dict ...
- Azure中国版 制作镜像 捕捉镜像
因为项目需要需要部署多台功能一样的服务器,简单来说是多台nginx服务器.如果按照原始的做法,是新建vm,然后一台台部署相关服务. 现在Azrue已经可以通过捕获镜像的方式创建vm镜像模板,然后按照创 ...
- JavaIO(06)文件复制
文件复制一般是采用两种方式进行操作: 1:将源文件中的内容全部读取到内存中,并一次性的写入到目标文件中:(不常用这种方式) 2:不将源文件中的内容全部读取进来,而是采用边读边写的方式: 实例01: ...