这个速度比分步快一点,内存占的稍微多一点

Problem Description
Solitaire is a game played on a chessboard 8x8. The rows and columns of the chessboard are numbered from 1 to 8, from the top to the bottom and from left to right respectively. There are four identical  pieces on the board. In one move it is allowed to: > move a piece to  an empty neighboring field (up, down, left or right), > jump over one  neighboring piece to an empty field (up, down, left or right).There are 4 moves  allowed for each piece in the configuration shown above. As an example let's  consider a piece placed in the row 4, column 4. It can be moved one row up, two  rows down, one column left or two columns right. Write a program  that: > reads two chessboard configurations from the standard  input, > verifies whether the second one is reachable from the first  one in at most 8 moves, > writes the result to the standard  output.
 
Input
Each of two input lines contains 8 integers a1, a2,  ..., a8 separated by single spaces and describes one configuration of pieces on  the chessboard. Integers a2j-1 and a2j (1 <= j <= 4) describe the position  of one piece - the row number and the column number respectively. Process to the  end of file.
 
Output
The output should contain one word for each test case -  YES if a configuration described in the second input line is reachable from the  configuration described in the first input line in at most 8 moves, or one word  NO otherwise.
 
Sample Input
4 4 4 5 5 4 6 5 2 4 3 3 3 6 4 6
 
Sample Output
YES
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <queue>
  4. #include <map>
  5. using namespace std;
  6.  
  7. const int dir[][] = {,,-,,,,,-};
  8.  
  9. struct point
  10. {
  11. int x,y;
  12. };
  13. struct node
  14. {
  15. point chess[];
  16.  
  17. bool check(int j)
  18. {
  19. if(chess[j].x>= && chess[j].x<= && chess[j].y>= && chess[j].y<=)
  20. {
  21. for(int i=;i<;i++)
  22. {
  23. if(i!=j && chess[i].x==chess[j].x && chess[i].y==chess[j].y)
  24. {
  25. return false;
  26. }
  27. }
  28. return true;
  29. }
  30. return false;
  31. }
  32. }s,e;
  33.  
  34. bool cmp(const struct point& a,const struct point& b)
  35. {
  36. if(a.x == b.x)
  37. {
  38. return a.y<b.y;
  39. }
  40. return a.x<b.x;
  41. }
  42. int gethash(node& a)
  43. {
  44. sort(a.chess, a.chess+, cmp);
  45. int hash = ;
  46. for(int i=; i<; i++)
  47. {
  48. hash |= a.chess[i].x << (*i);
  49. hash |= a.chess[i].y << (*i+);
  50. }
  51. return hash;
  52. }
  53.  
  54. map<int,int>mapint;
  55. map<int,int>::iterator it_1,it_2;
  56.  
  57. bool BFS(void)
  58. {
  59. queue<node>que[];
  60.  
  61. que[].push(s);
  62. que[].push(e);
  63. mapint[gethash(s)] = *+;
  64. mapint[gethash(e)] = *+;
  65.  
  66. int sign;
  67. while(!que[].empty() || !que[].empty())
  68. {
  69. if(que[].size() < que[].size())
  70. {
  71. sign = que[].empty() ? :;
  72. }
  73. else
  74. {
  75. sign = que[].empty() ? :;
  76. }
  77.  
  78. node temp = que[sign].front();
  79. que[sign].pop();
  80.  
  81. it_1 = mapint.find(gethash(temp));
  82.  
  83. if((it_1->second)% >= ) //移动步数超过4步
  84. {
  85. continue;
  86. }
  87. for(int i=;i<;i++)
  88. {
  89. for(int j=;j<;j++)
  90. {
  91. node next = temp;
  92. next.chess[i].x += dir[j][];
  93. next.chess[i].y += dir[j][];
  94.  
  95. if(!next.check(i)) //重叠或者越界
  96. {
  97. next.chess[i].x += dir[j][];
  98. next.chess[i].y += dir[j][];
  99. if(!next.check(i)) //重叠或者越界
  100. {
  101. continue;
  102. }
  103. }
  104.  
  105. int hash = gethash(next);
  106. it_2 = mapint.find(hash);
  107.  
  108. if(it_2 == mapint.end())
  109. {
  110. mapint[hash] = it_1->second + ;
  111. que[sign].push(next);
  112. }
  113. else if(it_2->second/ == -sign)
  114. {
  115. return true;
  116. }
  117. }
  118. }
  119. }
  120. return false;
  121. }
  122.  
  123. int main()
  124. {
  125. while(cin>>s.chess[].x>>s.chess[].y)
  126. {
  127. for(int i=; i<; i++)
  128. {
  129. cin>>s.chess[i].x>>s.chess[i].y;
  130. }
  131. for(int i=; i<; i++)
  132. {
  133. cin>>e.chess[i].x>>e.chess[i].y;
  134. }
  135. for(int i=;i<;i++)
  136. {
  137. s.chess[i].x--; s.chess[i].y--;
  138. e.chess[i].x--; e.chess[i].y--;
  139. }
  140.  
  141. if(BFS())
  142. {
  143. cout<<"YES"<<endl;
  144. }
  145. else
  146. {
  147. cout<<"NO"<<endl;
  148. }
  149. mapint.clear();
  150. }
  151. return ;
  152. }

HDU_1401——同步双向BFS,八进制位运算压缩,map存放hash的更多相关文章

  1. HDU_1401——分步双向BFS,八进制位运算压缩,map存放hash

    Problem Description Solitaire is a game played on a chessboard 8x8. The rows and columns of the ches ...

  2. HDU_1401——分步双向BFS,八进制乘权值压缩,map存放hash

    Problem Description Solitaire is a game played on a chessboard 8x8. The rows and columns of the ches ...

  3. HDU 3605 Escape (网络流,最大流,位运算压缩)

    HDU 3605 Escape (网络流,最大流,位运算压缩) Description 2012 If this is the end of the world how to do? I do not ...

  4. uva 1601 poj 3523 Morning after holloween 万圣节后的早晨 (经典搜索,双向bfs+预处理优化+状态压缩位运算)

    这题数据大容易TLE 优化:预处理, 可以先枚举出5^3的状态然后判断合不合法,但是由于题目说了有很多墙壁,实际上没有那么多要转移的状态那么可以把底图抽出来,然后3个ghost在上面跑到时候就不必判断 ...

  5. poj2965(位运算压缩+bfs+记忆路径)

    题意:有个4*4的开关,里面有着16个小开关 -+-- ---- ---- '+'表示开关是关着的,'-'表示开关是开着的,只有所有的开关全被打开,总开关才会被打开.现在有一种操作,只要改变某个开关, ...

  6. poj1753(位运算压缩状态+bfs)

    题意:有个4*4的棋盘,上面摆着黑棋和白旗,b代表黑棋,w代表白棋,现在有一种操作,如果你想要改变某一个棋子的颜色,那么它周围(前后左右)棋子的颜色都会被改变(白变成黑,黑变成白),问你将所有棋子变成 ...

  7. 【BFS】【位运算】解药还是毒药

    [codevs2594]解药还是毒药 Description Smart研制出对付各种症状的解药,可是他一个不小心,每种药都小小地配错了一点原料,所以这些药都有可能在治愈某些病症的同时又使人患上某些别 ...

  8. HDU5627--Clarke and MST (bfs+位运算)

    http://www.cnblogs.com/wenruo/p/5188495.html Clarke and MST Time Limit: 2000/1000 MS (Java/Others) M ...

  9. POJ 1753 bfs+位运算

    T_T ++运算符和+1不一样.(i+1)%4 忘带小括号了.bfs函数是bool 型,忘记返回false时的情况了.噢....debug快哭了...... DESCRIPTION:求最少的步骤.使得 ...

随机推荐

  1. Android更新UI的几种方式

    之前做过一个Android采集心电图数据的程序,那才是真正的多线程,之前写的小程序:比如下载个文件,从socket接受大一点的数据流然后在ui上更新进度,我都感觉这就叫做多线程了,其实这啥都不算,用个 ...

  2. Android(java)学习笔记214:开源框架的文件上传(只能使用Post)

    1.文件上传给服务器,服务器端必然要写代码进行支持,如下: 我们新建一个FileUpload.jsp的动态网页,同时我们上传文件只能使用post方式(不可能将上传数据拼凑在url路径下),上传数据Ap ...

  3. android SDK开发 -- TitleBar封装(一)

    假设app的title 统一的都是这种左中右结构的 代码如下 <LinearLayout xmlns:android="http://schemas.android.com/apk/r ...

  4. visual studio中的一些实用的快捷键

    重置开发环境:使得开发工具恢复默认状态 方法:工具->导入和导出设置导向->重置所有设置->不保存 显示行号: 方法:工具->选项->所有语言->行号 在编程过程中 ...

  5. (转)PHP中extract()函数的妙用

    近日在看一个牛人的代码时,看到一个非常好用的函数:extract(),它的主要作用是将数组展开,键名作为变量名,元素值为变量值,可以说为数组的操作提供了另外一个方便的工具,比方说,可以很方便的提取$_ ...

  6. linux下系统定时任务配置----crontab(mysql定时备份)

    crontab命令用于设置周期性被执行的指令,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动crond进程,crond进程每分钟会定期检查是否有要执行的任 ...

  7. .net中XML的创建01(传统方法)

    XML传统的创建: 传统的创建主要是依据XmlDocument的对象展开的,通过XmlDocument对象可以创建元素(XmlElement).属性(XmlAttribute)以及文本节点(Creat ...

  8. Eclipse怎么忽略掉报错的js文件

    第一步,我们要先定位错误在哪里,选择菜单里window——show view——other,选择Problems. 第二步,点击有红叉的项目,在Problems视图中,可以看到是什么错,哪个文件夹中的 ...

  9. Javascript模块化编程:模块的写法

    声明:本文转载自:阮一峰的网络日志,原文地址http://www.ruanyifeng.com/blog/2012/10/javascript_module.html,http://www.ruany ...

  10. 谈谈PHP、Python与Ruby

    假如你想帮他尽快找个活儿,赚到钱,推荐PHP. 假如你想让他成为一个高效工程师,推荐 Python. 假如你想让他爱上他的工作,推荐 Ruby. 语言的选择 编程语言非常重要,不要认为他们都图灵等价, ...