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. [python]MS17-010自动化扫描脚本

    一种是3gstudent分享的调用Nsa泄露的smbtouch-1.1.1.exe实现验证,另一种是参考巡风的poc.这里整合学习了下两种不同的方法. import os import fileinp ...

  2. roguelike地图的随机生成算法

    如果要想自己设计一个roguelike游戏,那么需要你有一个随机地图生成,我在indienova上看到一篇文章,描述了一个roguelike算法,然后自己用unity实现了一个下. 原文地址:随机生成 ...

  3. E20180519-hm

    distinct adj. 明显的,清楚的; 卓越的,不寻常的; 有区别的; 确切的;

  4. win7 mongod不是内部命令

    1.下载MongoDB 1.1 MongoDB下载 1.2 选择Server下面的 Community 2.安装MongoDB 2.1 注意事项:一直下一步就行了,但是遇到下面这个界面,注意一定要去掉 ...

  5. lightoj1047 【简单线性DP】

    因为写不出hdu3401...好像要用单调队列优化...这边水了一个... 题意: 给n个房子涂色,给出n个房子对应颜色的花费,问涂完所有颜色花费最少. 保证相邻房子颜色不能相同. 思路: dp[i] ...

  6. vr的延迟和渲染效率优化与Nvidia VRWorks

    http://blog.csdn.net/leonwei/article/details/50966071 vr现在正处于风生水起的阶段,但是vr的性能一直是大问题,最主要的问题就是响应延迟,玩家改变 ...

  7. sql server随机排序和随机取出n条数据

    问题:博主在2010-2011学年,广东技术师范大学大四的时候,去过红海人力集团面试数据库职位,很清楚记得当时有一道笔试题目是:编写sql从表里面随机取出10条记录. 解决方案:在sql server ...

  8. ISO 8 自适应cell

    原文网址: http://www.cocoachina.com/ios/20141218/10687.html 在使用 table view 的时侯经常会遇到这样的需求:table view 的 ce ...

  9. Promise对象深入理解

     目录 基本用法 返回另一个 Promise 实例 Promise.prototypeof.then Promise.prototype.catch Promise.prototype.finally ...

  10. 关于web常见的安全问题

    一. Web攻击动机: 1.恶作剧: 2.关闭Web站点,拒绝正常服务: 3.篡改Web网页,损害企业名誉; 4.免费浏览收费内容; 5.盗窃用户隐私信息,例如手机号.Email等个人信息; 6.以用 ...