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需要从最后的状 ...
随机推荐
- 给source insight添加.cc的C++文件后缀识别(转载)
转自:http://blog.chinaunix.net/uid-9950859-id-99172.html 今天在读mysql代码的时候,发现.cc结尾的文件都没有添加进来,google下了,发现原 ...
- 201621123016 《Java程序设计》第5周学习总结
1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 关键字:接口,多态,Comparable,Comparator 1.2 尝试使用思维导图将这些关键词组织起来.注:思维导图一般不 ...
- ThinkPHP3.2.3学习笔记5---模板(一)
一.模板简介 此文中的学习笔记部分资料是来自于thinkphp官方文档,http://document.thinkphp.cn/manual_3_2.html#template 本章的内容主要讲述了如 ...
- hoj2798 Globulous Gumdrops
Globulous Gumdrops My Tags (Edit) Source : 2008 Stanford Programming Contest Time limit : 1 se ...
- 2014-7-17 NOIP模拟赛
czy的后宫3(莫队) [题目描述] 上次czy在机房妥善安排了他的后宫之后,他发现可以将他的妹子分为c种,他经常会考虑这样一个问题:在[l,r]的妹子中间,能挑选出多少不同类型的妹子呢? 注意:由于 ...
- IT兄弟连 JavaWeb教程 EL表达式中的运算
EL语言支持算符运算符.关系运算符和逻辑运算符等,以完成常见的数据处理操作.所有的运算符说明见表7.2. 表2 El表达式中的运算符
- 如何在普通 UIViewController 中使用 UITableView
本系列文章 <Swift on iOS 学习笔记> 将以不定长度.不定内容.不定形式的方式对外发布,主要记录一些 “可重用” 的知识,感谢你的阅读. 在继承自 UIViewControll ...
- Python 学习 Part1
1. 斐波那契数序列 >>> a,b=0,1 >>> a 0 >>> b 1 >>> while b<10: print( ...
- [題解](貪心/堆)luogu_P2107小Z的AK計劃
清明講過一道類似的,難度略大的:P3545 [POI2012]HUR-Warehouse Store 兩道題類似,都是暫時先把前面的加進候選集合里,如果超出限制的話就拿現在這個和前面的交換, 相當於不 ...
- Linux (三)
chmod :用来改变文件或目录的访问权限 语法格式:chmod [参数选项] [mode][文件或者目录] 参数选项: -v :显示权限变更的详细信息 -R :对当前目录下的所有文件以及子目录进行相 ...