POJ1077 八数码 BFS
BFS 几天的超时...
A*算法不会,哪天再看去了.
- /*
- 倒搜超时,
- 改成顺序搜超时
- 然后把记录路径改成只记录当前点的操作,把上次的位置记录下AC..不完整的人生啊
- */
- #include <iostream>
- #include <queue>
- #include <vector>
- #include <iterator>
- #include <string>
- using namespace std;
- const int MAXX_SIZE = ;
- int fac[] = {,,,,,,,,};
- bool used[MAXX_SIZE];
- struct Nod
- {
- string str; //数组
- int postion9; //可交换的位置
- }nod[MAXX_SIZE];
- struct Step
- {
- int last;
- char c; //l0, r1, u2, d3
- }step[MAXX_SIZE];
- int dir[][] = {,, -,, ,, ,- };
- char st[] = {'d', 'u', 'r', 'l'};
- int Kt(string str, int n)
- {
- int i, j, cnt, sum=;
- for (i=; i<n; i++)
- {
- cnt = ;
- for (j=i+; j<n; j++)
- if (str[i] > str[j])
- cnt++;
- sum += cnt * fac[n--i];
- }
- return sum;
- }
- string InverKt(int sum, int n)
- {
- int i, j, t;
- bool Int[];
- string str;
- for (i=; i<n; i++)
- {
- t = sum / fac[n--i];
- for (j=; j<n; j++)
- {
- if (Int[j])
- {
- if (t == )
- break;
- t--;
- }
- }
- str += j++'';
- Int[j] = false;
- sum %= fac[n--i];
- }
- //str += '\0';
- return str;
- }
- void bfs(string str, int pos)
- {
- queue<int>que;
- //string str = "123456789";
- int i, m, n = Kt(str, );
- int ii, jj, ti, tj, t;
- nod[n].str = str;
- nod[n].postion9 = pos;
- step[n].last = -;
- que.push(n);
- used[n] = true;
- while (!que.empty())
- {
- n = que.front();
- que.pop();
- ii = nod[n].postion9 / ;
- jj = nod[n].postion9 % ;
- for (i=; i<; i++)
- {
- ti = ii + dir[i][];
- tj = jj + dir[i][];
- if (ti < || ti > || tj < || tj > )
- continue;
- t = ti*+tj;
- swap(nod[n].str[nod[n].postion9], nod[n].str[t]);
- m = Kt(nod[n].str, );
- if (!used[m])
- {
- used[m] = true;
- nod[m].str = nod[n].str;
- nod[m].postion9 = t;
- step[m].last = n;
- step[m].c = st[i] ;
- //step[m].str = step[n] + st[i]; 超时!
- if (m == )
- return ;
- que.push(m);
- }
- swap(nod[n].str[nod[n].postion9], nod[n].str[t]);
- }
- }
- }
- void show(int m)
- {
- if (step[m].last == -)
- return;
- show(step[m].last);
- cout<<step[m].c;
- }
- int main()
- {
- int i, n=, m, pos;
- char c;
- string str;
- for (i=; i<n; i++)
- {
- cin>>c;
- if (c == 'x')
- {
- pos = i;
- c = '';
- }
- str+= c;
- }
- bfs(str, pos);
- // m = Kt(str, n);
- if (!used[])
- cout<<"unsolvable"<<endl;
- else
- show();
- return ;
- }
POJ1077 八数码 BFS的更多相关文章
- hdu-1043(八数码+bfs打表+康托展开)
参考文章:https://www.cnblogs.com/Inkblots/p/4846948.html 康托展开:https://blog.csdn.net/wbin233/article/deta ...
- HDU1043 八数码(BFS + 打表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 , 康托展开 + BFS + 打表. 经典八数码问题,传说此题不做人生不完整,关于八数码的八境界 ...
- 紫书p199 八数码(BFS,hash)
八数码问题 紫书上的简单搜索 渣渣好久才弄懂 #include<cstdio> #include<cstring> using namespace std; const i ...
- POJ1077 八数码问题
题目:八数码 网址:http://poj.org/problem?id=1077 在一个3×3的网格中,1~8这8个数字和一个"X"恰好不重不漏地分布在这3×3的网格中. 例如: ...
- code1225 八数码Bfs
Bfs搜索 1.把棋盘直接作为状态: #include<iostream> #include<cstring> #include<queue> #include&l ...
- luogu_1379 八数码难题
八数码-->BFS+set #include<iostream> #include<cstdlib> #include<cstdio> #include< ...
- ACM/ICPC 之 BFS-广搜进阶-八数码(经典)(POJ1077+HDU1043)
八数码问题也称为九宫问题.(本想查查历史,结果发现居然没有词条= =,所谓的历史也就不了了之了) 在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的某一数字,不同棋子上标的数字不相同.棋盘上还有一个 ...
- 由八数码问题引入。对BFS有更深考虑
12号到今天共研究八数码问题poj1077,首先用的是普通BFS,遇到很多问题,开始用一个二级指针作为结构成员,知道了二级指针与二维数值名的不同!http://write.blog.csdn.net/ ...
- BFS(八数码) POJ 1077 || HDOJ 1043 Eight
题目传送门1 2 题意:从无序到有序移动的方案,即最后成1 2 3 4 5 6 7 8 0 分析:八数码经典问题.POJ是一次,HDOJ是多次.因为康托展开还不会,也写不了什么,HDOJ需要从最后的状 ...
随机推荐
- 5-1 变量与常量 & 6-1课程总结
变量与常量 常量就是变量定义的的前面加上final final关键字定义常量 新建类FinalDemo 更新常量n的值会报错.常量不可以被修改 常量有个命名规则 一般以大写字母去表示 final in ...
- msq 表操作与其数据类型
一:表介绍 表相当于文件, 表中的一条记录就相当于文件的一行内容, 不同的是,表中的一条记录有对应的标题,称为表的字段: id,name, age, sex,称为字段, 其余的一行内容称为一条记录. ...
- TP5之发送邮件
1.下载扩展,vendor\phpmailer 文件结构: 2.话不多说,上代码 注意点: · 需要提前开通对应邮箱的SMTP服务 · $mail->Host = " & ...
- 使用you-get下载网页小视频(实际上你可以下载任意你想要的web网页中的内容)
1. 什么是you-get? You-Get是一个小型的命令行实用程序,用于从Web下载媒体内容(视频,音频,图像),如果没有其他方便的方法可以尝试使用you-get. 2.安装you-get 打开命 ...
- Bloomberg 的一些功能
FFLO: 查看ETF流动,注意在View点击Contries后选择Asia,查看亚洲流动. 随后对感兴趣的国家点击查看具体股票的流动 关闭Launchpad View之后再次打开: BLP 修改La ...
- hdu1848(sg函数打表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1848 题意:中文题诶- 思路:直接sg函数打表就好了 代码: #include <iostrea ...
- IT兄弟连 JavaWeb教程 Servlet会话跟踪 创建Cookie
Tomcat作为Web服务器,对Cookie提供了良好的支持.那么,运行在Tomcat的Servlet该如何访问Cookie呢?幸运的是,Servlet无需直接和HTTP请求或响应中的原始Cookie ...
- 【渗透测试】如何利用burpsuite测试无回显漏洞
前面的文章讲了在windows和linux上的不同的无文件渗透测试的方法,那么这篇文章给大家讲解如何在漏洞没有回显的情况下,利用burpsuite自带插件进行测试的方式. 首先我们稍微提一下有哪些无回 ...
- iOS app支付宝接口调用的一点总结(补充支付宝SDK&Demo下载地址)
由于app内需要用到支付功能,选择了当前最流行的支付宝进行支付.在进行内嵌支付宝功能开发时,被它狠狠的耍了一把. 根据支付宝开发文档,参考demo代码.将相关支付功能加到了自己的代码中.一些根据文档来 ...
- DBUtils学习一 增删该查
package com.mozq.jdbc.test; import java.sql.SQLException; import java.util.List; import java.util.Ma ...