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的更多相关文章

  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. 常用模块 re模块与正则表达式

    re模块 正则: 正则就是用一些具有特殊含义的符号组合到一起(称之为正则表达式)来描述字符或字符串的方法.或者说:正则就是用描述一类事物的规则.(在python中) 它内嵌在python中,并通过re ...

  2. Android 4.2开发环境搭建

    一.工具 jdk1.7; eclipse 4.3(for java ee); Android SDK; 二.安装JDK并配置 安装略,配置如下: 右击 “我的电脑”->属性->高级系统设置 ...

  3. C++设计模式之工厂方法模式

    来自:http://blog.csdn.net/pangshaohua/article/details/38912555 参考写的一个工厂demo 1.定义"背景风格的抽象类".& ...

  4. OpenCV第一课

    1.OpenCV下载地址:http://opencv.org/downloads.html 因为本人电脑装的是vs2010,所以下载的是opencv-2.4.11.exe(vc10.vc11.vc12 ...

  5. 进击python第一篇:相遇

    1.第一句Python代码 在 当前目录下创建 hello.py 文件,内容如下: print "hello,world!" 执行 hello.py 文件,即: python he ...

  6. [HNOI2010] 平面图判定 planar

    标签:二分图判定.题解: 首先可以把题目中给你的那个环给画出来,这样就可以发现对于任意一个图来说,如果两条边要相交,就不能让他们相交,那么这两条边就要一条在里面一条在外面,如果把环画成一条链,那么就是 ...

  7. 【UVA - 156 】Ananagrams (set,map,vector)

    Ananagrams  Descriptions: Most crossword puzzle fans are used to anagrams--groups of words with the ...

  8. JS与JQ的对比与提高

    来吧, 案例1:先上个例子js写的省市二级联动 <!DOCTYPE html><html> <head> <meta charset="UTF-8& ...

  9. PostgreSQL - 模糊查询

    前言 like.not like在SQL中用于模糊查询,%表示任意个字符,_表示单个任意字符,如果需要在模糊查询中查询这两个通配符,需要用ESCAPE进行转义,如下: select * from ta ...

  10. python 基础(十) 面向对象

    面向对象 一.概念 类(class): 用来描述具有相同属性和方法的对象的集合 对象是类的实例化 类变量:类变量在整个实例化的对象中是共用的.定义在类中 并且是函数体外的 实例变量:只能作用于 当前类 ...