BFS 几天的超时...

A*算法不会,哪天再看去了.

  1. /*
  2. 倒搜超时,
  3. 改成顺序搜超时
  4. 然后把记录路径改成只记录当前点的操作,把上次的位置记录下AC..不完整的人生啊
  5. */
  6.  
  7. #include <iostream>
  8. #include <queue>
  9. #include <vector>
  10. #include <iterator>
  11. #include <string>
  12. using namespace std;
  13.  
  14. const int MAXX_SIZE = ;
  15.  
  16. int fac[] = {,,,,,,,,};
  17. bool used[MAXX_SIZE];
  18.  
  19. struct Nod
  20. {
  21. string str; //数组
  22. int postion9; //可交换的位置
  23. }nod[MAXX_SIZE];
  24.  
  25. struct Step
  26. {
  27. int last;
  28. char c; //l0, r1, u2, d3
  29.  
  30. }step[MAXX_SIZE];
  31.  
  32. int dir[][] = {,, -,, ,, ,- };
  33.  
  34. char st[] = {'d', 'u', 'r', 'l'};
  35.  
  36. int Kt(string str, int n)
  37. {
  38. int i, j, cnt, sum=;
  39. for (i=; i<n; i++)
  40. {
  41. cnt = ;
  42. for (j=i+; j<n; j++)
  43. if (str[i] > str[j])
  44. cnt++;
  45. sum += cnt * fac[n--i];
  46. }
  47. return sum;
  48. }
  49.  
  50. string InverKt(int sum, int n)
  51. {
  52. int i, j, t;
  53. bool Int[];
  54. string str;
  55. for (i=; i<n; i++)
  56. {
  57. t = sum / fac[n--i];
  58. for (j=; j<n; j++)
  59. {
  60. if (Int[j])
  61. {
  62. if (t == )
  63. break;
  64. t--;
  65. }
  66. }
  67. str += j++'';
  68. Int[j] = false;
  69. sum %= fac[n--i];
  70. }
  71. //str += '\0';
  72. return str;
  73. }
  74.  
  75. void bfs(string str, int pos)
  76. {
  77. queue<int>que;
  78.  
  79. //string str = "123456789";
  80. int i, m, n = Kt(str, );
  81. int ii, jj, ti, tj, t;
  82.  
  83. nod[n].str = str;
  84. nod[n].postion9 = pos;
  85. step[n].last = -;
  86. que.push(n);
  87. used[n] = true;
  88.  
  89. while (!que.empty())
  90. {
  91. n = que.front();
  92. que.pop();
  93. ii = nod[n].postion9 / ;
  94. jj = nod[n].postion9 % ;
  95. for (i=; i<; i++)
  96. {
  97. ti = ii + dir[i][];
  98. tj = jj + dir[i][];
  99. if (ti < || ti > || tj < || tj > )
  100. continue;
  101. t = ti*+tj;
  102. swap(nod[n].str[nod[n].postion9], nod[n].str[t]);
  103. m = Kt(nod[n].str, );
  104.  
  105. if (!used[m])
  106. {
  107. used[m] = true;
  108. nod[m].str = nod[n].str;
  109. nod[m].postion9 = t;
  110. step[m].last = n;
  111. step[m].c = st[i] ;
  112. //step[m].str = step[n] + st[i]; 超时!
  113. if (m == )
  114. return ;
  115. que.push(m);
  116. }
  117. swap(nod[n].str[nod[n].postion9], nod[n].str[t]);
  118. }
  119. }
  120. }
  121.  
  122. void show(int m)
  123. {
  124. if (step[m].last == -)
  125. return;
  126. show(step[m].last);
  127. cout<<step[m].c;
  128. }
  129.  
  130. int main()
  131. {
  132.  
  133. int i, n=, m, pos;
  134. char c;
  135. string str;
  136. for (i=; i<n; i++)
  137. {
  138. cin>>c;
  139. if (c == 'x')
  140. {
  141. pos = i;
  142. c = '';
  143. }
  144. str+= c;
  145. }
  146. bfs(str, pos);
  147. // m = Kt(str, n);
  148. if (!used[])
  149. cout<<"unsolvable"<<endl;
  150. else
  151. show();
  152. return ;
  153. }

POJ1077 八数码 BFS的更多相关文章

  1. hdu-1043(八数码+bfs打表+康托展开)

    参考文章:https://www.cnblogs.com/Inkblots/p/4846948.html 康托展开:https://blog.csdn.net/wbin233/article/deta ...

  2. HDU1043 八数码(BFS + 打表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 , 康托展开 + BFS + 打表. 经典八数码问题,传说此题不做人生不完整,关于八数码的八境界 ...

  3. 紫书p199 八数码(BFS,hash)

    八数码问题  紫书上的简单搜索  渣渣好久才弄懂 #include<cstdio> #include<cstring> using namespace std; const i ...

  4. POJ1077 八数码问题

    题目:八数码 网址:http://poj.org/problem?id=1077 在一个3×3的网格中,1~8这8个数字和一个"X"恰好不重不漏地分布在这3×3的网格中. 例如: ...

  5. code1225 八数码Bfs

    Bfs搜索 1.把棋盘直接作为状态: #include<iostream> #include<cstring> #include<queue> #include&l ...

  6. luogu_1379 八数码难题

    八数码-->BFS+set #include<iostream> #include<cstdlib> #include<cstdio> #include< ...

  7. ACM/ICPC 之 BFS-广搜进阶-八数码(经典)(POJ1077+HDU1043)

    八数码问题也称为九宫问题.(本想查查历史,结果发现居然没有词条= =,所谓的历史也就不了了之了) 在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的某一数字,不同棋子上标的数字不相同.棋盘上还有一个 ...

  8. 由八数码问题引入。对BFS有更深考虑

    12号到今天共研究八数码问题poj1077,首先用的是普通BFS,遇到很多问题,开始用一个二级指针作为结构成员,知道了二级指针与二维数值名的不同!http://write.blog.csdn.net/ ...

  9. BFS(八数码) POJ 1077 || HDOJ 1043 Eight

    题目传送门1 2 题意:从无序到有序移动的方案,即最后成1 2 3 4 5 6 7 8 0 分析:八数码经典问题.POJ是一次,HDOJ是多次.因为康托展开还不会,也写不了什么,HDOJ需要从最后的状 ...

随机推荐

  1. 5-1 变量与常量 & 6-1课程总结

    变量与常量 常量就是变量定义的的前面加上final final关键字定义常量 新建类FinalDemo 更新常量n的值会报错.常量不可以被修改 常量有个命名规则 一般以大写字母去表示 final in ...

  2. msq 表操作与其数据类型

    一:表介绍 表相当于文件, 表中的一条记录就相当于文件的一行内容, 不同的是,表中的一条记录有对应的标题,称为表的字段: id,name, age, sex,称为字段, 其余的一行内容称为一条记录. ...

  3. TP5之发送邮件

    1.下载扩展,vendor\phpmailer 文件结构: 2.话不多说,上代码    注意点: ·   需要提前开通对应邮箱的SMTP服务 ·  $mail->Host = "  & ...

  4. 使用you-get下载网页小视频(实际上你可以下载任意你想要的web网页中的内容)

    1. 什么是you-get? You-Get是一个小型的命令行实用程序,用于从Web下载媒体内容(视频,音频,图像),如果没有其他方便的方法可以尝试使用you-get. 2.安装you-get 打开命 ...

  5. Bloomberg 的一些功能

    FFLO: 查看ETF流动,注意在View点击Contries后选择Asia,查看亚洲流动. 随后对感兴趣的国家点击查看具体股票的流动 关闭Launchpad View之后再次打开: BLP 修改La ...

  6. hdu1848(sg函数打表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1848 题意:中文题诶- 思路:直接sg函数打表就好了 代码: #include <iostrea ...

  7. IT兄弟连 JavaWeb教程 Servlet会话跟踪 创建Cookie

    Tomcat作为Web服务器,对Cookie提供了良好的支持.那么,运行在Tomcat的Servlet该如何访问Cookie呢?幸运的是,Servlet无需直接和HTTP请求或响应中的原始Cookie ...

  8. 【渗透测试】如何利用burpsuite测试无回显漏洞

    前面的文章讲了在windows和linux上的不同的无文件渗透测试的方法,那么这篇文章给大家讲解如何在漏洞没有回显的情况下,利用burpsuite自带插件进行测试的方式. 首先我们稍微提一下有哪些无回 ...

  9. iOS app支付宝接口调用的一点总结(补充支付宝SDK&Demo下载地址)

    由于app内需要用到支付功能,选择了当前最流行的支付宝进行支付.在进行内嵌支付宝功能开发时,被它狠狠的耍了一把. 根据支付宝开发文档,参考demo代码.将相关支付功能加到了自己的代码中.一些根据文档来 ...

  10. DBUtils学习一 增删该查

    package com.mozq.jdbc.test; import java.sql.SQLException; import java.util.List; import java.util.Ma ...