题意:http://acm.hdu.edu.cn/showproblem.php?pid=1401

给你8*8的棋盘和4个棋子初始位置、最终位置,问你能否在8次操作后达到该状态。

思路:

双向BFS,起点开始正搜4步,终点倒搜4步,map标记。

  1. #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2. #include <cstdio>//sprintf islower isupper
  3. #include <cstdlib>//malloc exit strcat itoa system("cls")
  4. #include <iostream>//pair
  5. #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\Input.txt","r",stdin);
  6. #include <bitset>
  7. //#include <map>
  8. #include<unordered_map>
  9. #include <vector>
  10. #include <stack>
  11. #include <set>
  12. #include <string.h>//strstr substr strcat
  13. #include <string>
  14. #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
  15. #include <cmath>
  16. #include <deque>
  17. #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
  18. #include <vector>//emplace_back
  19. //#include <math.h>
  20. #include <cassert>
  21. #include <iomanip>
  22. //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
  23. #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
  24. using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
  25. //******************
  26. clock_t __START,__END;
  27. double __TOTALTIME;
  28. void _MS(){__START=clock();}
  29. void _ME(){__END=clock();__TOTALTIME=(double)(__END-__START)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
  30. //***********************
  31. #define rint register int
  32. #define fo(a,b,c) for(rint a=b;a<=c;++a)
  33. #define fr(a,b,c) for(rint a=b;a>=c;--a)
  34. #define mem(a,b) memset(a,b,sizeof(a))
  35. #define pr printf
  36. #define sc scanf
  37. #define ls rt<<1
  38. #define rs rt<<1|1
  39. typedef pair<int,int> PII;
  40. typedef vector<int> VI;
  41. typedef unsigned long long ull;
  42. typedef long long ll;
  43. typedef double db;
  44. const double E=2.718281828;
  45. const double PI=acos(-1.0);
  46. const ll INF=(1LL<<);
  47. const int inf=(<<);
  48. const double ESP=1e-;
  49. const int mod=(int)1e9+;
  50. const int N=(int)1e6+;
  51.  
  52. int mp[][];
  53. unordered_map<ull,bool>mark;
  54. struct node
  55. {
  56. ull id;
  57. int step;
  58. };
  59. ull get()
  60. {
  61. ull now=,temp=;
  62. int cnt=-;
  63. for(int i=;i<=;++i)
  64. {
  65. for(int j=;j<=;++j)
  66. {
  67. ++cnt;
  68. if(mp[i][j])
  69. now|=temp<<cnt;
  70. }
  71. }
  72. return now;
  73. }
  74. bool ok(int x,int y)
  75. {
  76. return x>=&&x<=&&y>=&&y<=;
  77. }
  78. bool ans;
  79. void bfs(ull start,bool f)
  80. {
  81. queue<node>q;
  82. q.push({start,});
  83. while(!q.empty())
  84. {
  85. node now=q.front();q.pop();
  86. if(!f)
  87. {
  88. if(mark[now.id])continue;
  89. mark[now.id]=;
  90. }
  91. else
  92. {
  93. if(mark[now.id])
  94. {
  95. ans=;
  96. return;
  97. }
  98. }
  99. if(now.step==)continue;
  100. int cnt=-;
  101. for(int i=;i<=;++i)
  102. {
  103. for(int j=;j<=;++j)
  104. {
  105. ++cnt;
  106. mp[i][j]=(int)(now.id>>cnt)&;
  107. }
  108. }
  109. for(int i=;i<=;++i)
  110. {
  111. for(int j=;j<=;++j)
  112. {
  113. if(mp[i][j]&&ok(i-,j)&&mp[i-][j]==&&ok(i-,j)&&mp[i-][j]==)
  114. {
  115. mp[i-][j]=,mp[i][j]=;
  116. q.push({get(),now.step+});
  117. mp[i-][j]=,mp[i][j]=;
  118. }
  119. if(mp[i][j]&&ok(i+,j)&&mp[i+][j]==&&ok(i+,j)&&mp[i+][j]==)
  120. {
  121. mp[i+][j]=,mp[i][j]=;
  122. q.push({get(),now.step+});
  123. mp[i+][j]=,mp[i][j]=;
  124. }
  125. if(mp[i][j]&&ok(i,j-)&&mp[i][j-]==&&ok(i,j-)&&mp[i][j-]==)
  126. {
  127. mp[i][j-]=,mp[i][j]=;
  128. q.push({get(),now.step+});
  129. mp[i][j-]=,mp[i][j]=;
  130. }
  131. if(mp[i][j]&&ok(i,j+)&&mp[i][j+]==&&ok(i,j+)&&mp[i][j+]==)
  132. {
  133. mp[i][j+]=,mp[i][j]=;
  134. q.push({get(),now.step+});
  135. mp[i][j+]=,mp[i][j]=;
  136. }
  137. //--------------------------------------------------------------
  138. if(mp[i][j]==&&ok(i-,j)&&mp[i-][j]==)
  139. {
  140. mp[i-][j]=,mp[i][j]=;
  141. q.push({get(),now.step+});
  142. mp[i-][j]=;mp[i][j]=;
  143. }
  144. if(mp[i][j]==&&ok(i+,j)&&mp[i+][j]==)
  145. {
  146. mp[i+][j]=,mp[i][j]=;
  147. q.push({get(),now.step+});
  148. mp[i+][j]=;mp[i][j]=;
  149. }
  150. if(mp[i][j]==&&ok(i,j-)&&mp[i][j-]==)
  151. {
  152. mp[i][j-]=,mp[i][j]=;
  153. q.push({get(),now.step+});
  154. mp[i][j-]=;mp[i][j]=;
  155. }
  156. if(mp[i][j]==&&ok(i,j+)&&mp[i][j+]==)
  157. {
  158. mp[i][j+]=,mp[i][j]=;
  159. q.push({get(),now.step+});
  160. mp[i][j+]=;mp[i][j]=;
  161. }
  162. }
  163. }
  164. }
  165. }
  166.  
  167. int main()
  168. {
  169. int x,y;
  170. while(~sc("%d%d",&x,&y))
  171. {
  172. ans=;
  173. mark.clear();
  174. mem(mp,);
  175. mp[x][y]=;
  176. for(int i=;i<=;++i)
  177. {
  178. sc("%d%d",&x,&y);
  179. mp[x][y]=;
  180. }
  181. bfs(get(),);
  182. mem(mp,);
  183. for(int i=;i<=;++i)
  184. {
  185. sc("%d%d",&x,&y);
  186. mp[x][y]=;
  187. }
  188. bfs(get(),);
  189. if(ans)
  190. pr("YES\n");
  191. else
  192. pr("NO\n");
  193. }
  194. return ;
  195. }
  196.  
  197. /**************************************************************************************/

HDU1401(双向BFS)的更多相关文章

  1. POJ1915Knight Moves(单向BFS + 双向BFS)

    题目链接 单向bfs就是水题 #include <iostream> #include <cstring> #include <cstdio> #include & ...

  2. HDU 3085 Nightmare II 双向bfs 难度:2

    http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...

  3. POJ 3170 Knights of Ni (暴力,双向BFS)

    题意:一个人要从2先走到4再走到3,计算最少路径. 析:其实这个题很水的,就是要注意,在没有到4之前是不能经过3的,一点要注意.其他的就比较简单了,就是一个双向BFS,先从2搜到4,再从3到搜到4, ...

  4. [转] 搜索之双向BFS

    转自:http://www.cppblog.com/Yuan/archive/2011/02/23/140553.aspx 如果目标也已知的话,用双向BFS能很大程度上提高速度. 单向时,是 b^le ...

  5. 双向BFS

    转自“Yuan” 如果目标也已知的话,用双向BFS能很大提高速度 单向时,是 b^len的扩展. 双向的话,2*b^(len/2)  快了很多,特别是分支因子b较大时 至于实现上,网上有些做法是用两个 ...

  6. HDU 3085 Nightmare Ⅱ (双向BFS)

    Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  7. HDU 3085 Nightmare Ⅱ 双向BFS

    题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...

  8. POJ 3126 Prime Path 解题报告(BFS & 双向BFS)

    题目大意:给定一个4位素数,一个目标4位素数.每次变换一位,保证变换后依然是素数,求变换到目标素数的最小步数. 解题报告:直接用最短路. 枚举1000-10000所有素数,如果素数A交换一位可以得到素 ...

  9. Hdu1401-Solitaire(双向bfs)

    Solitaire is a game played on a chessboard 8x8. The rows and columns of the chessboard are numbered ...

随机推荐

  1. Monkey测试感想

    monkey测试主要做随机的黑盒测试,通过不断输入伪随机的事件流来测试应用的稳定性,但是由于monkey太过皮,太过随机,最后根本无法控制,很容易陷于一个页面无法出来,或者陷入某个无关紧要的地方无法出 ...

  2. HTML页面预览表格文件内容

    背景简介 在将一个表格文件上传到服务器上之前,JS读取表格文件并将文件内容输出到页面中 vue项目 第三方 exceljs 安装 npm install exceljs 插件使用 github 中文文 ...

  3. Java操作Cookie方法

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  4. MySQL 创建和删除数据表

    创建MySQL数据表需要以下信息: 表名 表字段名 定义每个表字段 语法 以下为创建MySQL数据表的SQL通用语法: CREATE TABLE table_name (column_name col ...

  5. OpenCL如何判定一个work-group的最大Local Memory大小

    最近有不少朋友提及到如何能在运行时获悉一个GPU的最大local memory的尺寸.由于OpenCL对各类处理器开放,因此不同处理器所拥有的local memory大小也各不相同.即便是GPU,甚至 ...

  6. Eclipse项目修改编译jdk版本(Failed to read candidate component class: file 处理)

    转: Failed to read candidate component class: file 处理 2018年03月09日 07:15:54 爱萨萨 阅读数 10041   出错现象: org. ...

  7. React开发环境配置

    本文以上一篇文章继续配置:npm安装及环境配置<https://www.cnblogs.com/hzb462606/p/11565275.html> 使用 create-react-app ...

  8. 阶段5 3.微服务项目【学成在线】_day05 消息中间件RabbitMQ_4.RabbitMQ研究-安装RabbitMQ

    RabbitMQ由Erlang语言开发,Erlang语言用于并发及分布式系统的开发,在电信领域应用广泛,OTP(Open Telecom Platform)作为Erlang语言的一部分,包含了很多基于 ...

  9. 电力项目十一--js添加浮动框

    1.添加浮动窗口样式 <!-- 浮动窗口样式css begin --> <style type="text/css"> #msg_win{border:1p ...

  10. iOS——plist的创建,数据写入与读取

    iOS中plist的创建,数据写入与读取 Documents:应用将数据存储在Documents中,但基于NSuserDefaults的首选项设置除外Library:基于NSUserDefaults的 ...