把这个写出来是不是就意味着把   http://www.hacker.org/push  这个游戏打爆了? ~啊哈哈哈

其实只要找到一个就可以退出了  所以效率也不算很低的  可以直接DFS呀呀呀呀

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. int dx[] = {-1,1,0,0};
  11. int dy[] = {0,0,1,-1};
  12.  
  13. vector <char> ans;
  14. int n,m;
  15. char map[30][30];
  16. char t[30][30];
  17. bool found;
  18. void copy() //每一次都需要复制一下地图
  19. {
  20. for(int i=0;i<n;i++)
  21. for(int j=0;j<m;j++)
  22. map[i][j]=t[i][j];
  23. }
  24.  
  25. void debug(int ax,int ay) //调试函数~~~
  26. {
  27. for(int i=0;i<n;i++)
  28. {
  29. for(int j=0;j<m;j++)
  30. {
  31. if(i==ax && j==ay){printf("@");continue;}
  32. printf("%c",map[i][j]);
  33. }
  34. puts("");
  35. }
  36. puts("");
  37. }
  38.  
  39. bool ok(int xx,int yy)
  40. {
  41. if(xx<n && xx>=0 && yy<m && yy>=0)return true;
  42.  
  43. return false;
  44. }
  45.  
  46. bool tag() //全部砖块都清除了
  47. {
  48. for(int i=0;i<n;i++)
  49. for(int j=0;j<m;j++)
  50. if(map[i][j]!='.')return false;
  51.  
  52. return true;
  53. }
  54.  
  55. bool dfs(int posx,int posy)
  56. {
  57. if(found)return true;
  58. if(tag())
  59. {
  60. found=true;
  61. return true;
  62. }
  63. //debug(posx,posy);
  64. for(int k=0;k<4;k++)
  65. {
  66. if(map[posx+dx[k]][posy+dy[k]]!='.' || !ok(posx+dx[k],posy+dy[k]))continue;
  67. for(int i=1;i<=25;i++)
  68. {
  69. if(map[posx+i*dx[k]][posy+i*dy[k]]!='.' && ok(posx+i*dx[k],posy+i*dy[k]))
  70. {
  71. int tx=posx+i*dx[k];
  72. int ty=posy+i*dy[k];
  73. char cur1=map[tx][ty];
  74. char cur2=map[tx+dx[k]][ty+dy[k]];
  75. map[tx][ty]='.';
  76. if(cur1!='a')
  77. {
  78. if(cur2=='.')
  79. {
  80. map[tx+dx[k]][ty+dy[k]]=cur1-1; //没有就减一级
  81. }
  82. else
  83. {
  84. map[tx+dx[k]][ty+dy[k]]=cur1-'a'+cur2; //如果后面有东西就要合体
  85. }
  86. }
  87.  
  88. if(k==0)ans.push_back('U');
  89. else if(k==1)ans.push_back('D');
  90. else if(k==2)ans.push_back('R');
  91. else ans.push_back('L');
  92.  
  93. dfs(tx,ty);
  94. if(found)return true;
  95. map[tx][ty]=cur1;
  96. map[tx+dx[k]][ty+dy[k]]=cur2;
  97. ans.pop_back();
  98.  
  99. break;
  100. }
  101. }
  102. }
  103. return false;
  104. }
  105.  
  106. int main()
  107. {
  108. while(scanf("%d%d",&m,&n)!=EOF)
  109. {
  110. found=false;
  111. ans.clear();
  112. for(int i=0;i<n;i++)
  113. scanf("%s",t[i]);
  114.  
  115. for(int i=0;i<n;i++)
  116. {
  117. for(int j=0;j<m;j++)
  118. {
  119. if(t[i][j]!='.')continue;
  120. copy();
  121. if(dfs(i,j))
  122. {
  123. printf("%d\n%d\n",i,j);
  124. break;
  125. }
  126. }
  127. if(found)break;
  128. }
  129. for(int sb=0;sb<ans.size();sb++)
  130. printf("%c",ans[sb]);
  131. puts("");
  132. }
  133. return 0;
  134. }
  135.  
  136. /*
  137. 13 3
  138. .............
  139. ...bb..bc....
  140. .............
  141.  
  142. 7 8
  143. .......
  144. .......
  145. .....b.
  146. .......
  147. .aa.b..
  148. .....a.
  149. .......
  150. .......
  151. */

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 5965 扫雷(dfs)题解

    题意:给你一个3*n的格子,中间那行表明的是周围8格(当然左右都没有)的炸弹数量,上下两行都可以放炸弹,问你有几种可能,对mod取模 思路:显然(不),当i - 1和i - 2确定时,那么i的个数一定 ...

  3. HDU 1518 Square(DFS)

    Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-end ...

  4. HDU 1015 Safecracker (DFS)

    题意:给一个数字n(n<=12000000)和一个字符串s(s<=17),字符串的全是有大写字母组成,字母的大小按照字母表的顺序,比如(A=1,B=2,......Z=26),从该字符串中 ...

  5. Hdu 1175 连连看(DFS)

    Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1175 因为题目只问能不能搜到,没问最少要几个弯才能搜到,所以我采取了DFS. 因为与Hdu ...

  6. HDU 1501 Zipper(DFS)

    Problem Description Given three strings, you are to determine whether the third string can be formed ...

  7. HDU 5305 Friends(dfs)

    Friends Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su ...

  8. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

  9. HDU 5934 Bomb(炸弹)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

随机推荐

  1. android中的所谓观察者模式

    生活中我们常认定某些人很有才,但什么是有才呢?明朝的王守仁曾这样解释:才,是所谓天理,应用到物上,便成了才.凡事凡物,只要掌握了所谓科学的方法,并能灵活运用,那么你也可以成为一个有才的人. 观察者模式 ...

  2. C++调试 输出数组内容和数组名

    #include <cstdio> using namespace std; //函数定义 #define printArr(arr,n,format) \ printf("%s ...

  3. C++ STL疑惑知识点

    1.remove的问题 用法参考:http://www.cnblogs.com/heyonggang/p/3263568.html

  4. eclipse 报错汇总

    1.Eclipse 启动时,报错: Fail to create the java virtual machine   已解决.方法:eclipse.ini 中-vmargs-Dosgi.requir ...

  5. air手势代码

    //下列2句谁放上面谁生效要么触控生效,要么手势生效 Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT; Multitouch.inputM ...

  6. CSRF的攻击与防御(转)

    add by zhj:CSRF之所有发生,是因为http请求中会自动带上cookies,我的解决办法是:前端不要将数据放在cookie中,而是放在其它本地存储 (HTML5中称之为Web Storag ...

  7. 2010“架构师接龙”问答--杨卫华VS赵劼(转)

    add by zhj:虽然是几年前的文章,但还是很有参考价值的 原文:http://blog.zhaojie.me/2010/05/programmer-magazine-2010-5-archite ...

  8. vim之pydiction插件

    [vim之pydiction插件] It consists of three main files: python_pydiction.vim -- Vim plugin. complete-dict ...

  9. Azure中国版 制作镜像 捕捉镜像

    因为项目需要需要部署多台功能一样的服务器,简单来说是多台nginx服务器.如果按照原始的做法,是新建vm,然后一台台部署相关服务. 现在Azrue已经可以通过捕获镜像的方式创建vm镜像模板,然后按照创 ...

  10. JavaIO(06)文件复制

    文件复制一般是采用两种方式进行操作: 1:将源文件中的内容全部读取到内存中,并一次性的写入到目标文件中:(不常用这种方式) 2:不将源文件中的内容全部读取进来,而是采用边读边写的方式:   实例01: ...