HDU 1043 Eight BFS
题意:就是恢复成1,2,3,4,5,6,7,8,0;
分析:暴力BFS预处理,所有路径,用康拓展开判重,O(1)打印 93ms 还是很快的
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int N=;
int fac[]= {,,,,,,,,,};
int aim;
int cantor(char s[])
{
int ans=;
for(int i=,j=; i<=; ++i,--j)
{
int tmp=;
for(int k=i+; k<=; ++k)
if(s[i]>s[k])++tmp;
ans+=(tmp*fac[j]);
}
return ans;
}
struct Node
{
char s[];
int hs;
};
struct asd
{
bool vis;
char c;
int pre;
}o[];
queue<Node>q;
void bfs()
{
Node a;
for(int i=; i<=; ++i)
a.s[i]=''+i;
a.s[]='';
aim=a.hs=cantor(a.s);
o[aim].vis=;
q.push(a);
while(!q.empty())
{
a=q.front();
q.pop();
int now=a.hs;
int x;
for(int i=; i<=; ++i)
if(a.s[i]=='')x=i;
if(x->)
{
bool flag=;
swap(a.s[x],a.s[x-]);
a.hs=cantor(a.s);
if(o[a.hs].vis)
flag=;
if(!flag)
{
o[a.hs].vis=;
o[a.hs].c='d';
o[a.hs].pre=now;
q.push(a);
}
swap(a.s[x],a.s[x-]);
}
if(x+<)
{
bool flag=;
swap(a.s[x],a.s[x+]);
a.hs=cantor(a.s);
if(o[a.hs].vis)
flag=;
if(!flag)
{
o[a.hs].vis=;
o[a.hs].c='u';
o[a.hs].pre=now;
q.push(a);
}
swap(a.s[x],a.s[x+]);
}
if(x%!=)
{
bool flag=;
swap(a.s[x],a.s[x-]);
a.hs=cantor(a.s);
if(o[a.hs].vis)
flag=;
if(!flag)
{
o[a.hs].vis=;
o[a.hs].c='r';
o[a.hs].pre=now;
q.push(a);
}
swap(a.s[x],a.s[x-]);
}
if(x%)
{
bool flag=;
swap(a.s[x],a.s[x+]);
a.hs=cantor(a.s);
if(o[a.hs].vis)
flag=;
if(!flag)
{
o[a.hs].vis=;
o[a.hs].c='l';
o[a.hs].pre=now;
q.push(a);
}
swap(a.s[x],a.s[x+]);
}
}
}
char str[],tmp[];
void print(int u)
{
while(u!=aim)
{
printf("%c",o[u].c);
u=o[u].pre;
}
printf("\n");
}
int main()
{
for(int i=;i<;++i)
o[i].vis=;
bfs();
while(gets(str))
{
int l=;
for(int i=; str[i]!=''; ++i)
{
if(str[i]=='x')tmp[++l]='';
else if(str[i]>=''&&str[i]<='')tmp[++l]=str[i];
}
int ans=cantor(tmp);
if(!o[ans].vis)
printf("unsolvable\n");
else
print(ans);
}
return ;
}
HDU 1043 Eight BFS的更多相关文章
- HDU 1043 Eight (BFS·八数码·康托展开)
题意 输出八数码问题从给定状态到12345678x的路径 用康托展开将排列相应为整数 即这个排列在全部排列中的字典序 然后就是基础的BFS了 #include <bits/stdc++.h ...
- Eight HDU - 1043 (双向BFS)
记得上人工智能课的时候老师讲过一个A*算法,计算估价函数(f[n]=h[n]+g[n])什么的,感觉不是很好理解,百度上好多都是用逆向BFS写的,我理解的逆向BFS应该是从终点状态出发,然后把每一种状 ...
- POJ-1077 HDU 1043 HDU 3567 Eight (BFS预处理+康拓展开)
思路: 这三个题是一个比一个令人纠结呀. POJ-1077 爆搜可以过,94ms,注意不能用map就是了. #include<iostream> #include<stack> ...
- HDU 1043 Eight(八数码)
HDU 1043 Eight(八数码) 00 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Descr ...
- Eight POJ - 1077 HDU - 1043 八数码
Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...
- HDU - 1043 - Eight / POJ - 1077 - Eight
先上题目: Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- HDU 1043 Eight 八数码问题 A*算法(经典问题)
HDU 1043 Eight 八数码问题(经典问题) 题意 经典问题,就不再进行解释了. 这里主要是给你一个状态,然后要你求其到达\(1,2,3,4,5,6,7,8,x\)的转移路径. 解题思路 这里 ...
- hdu 1043 pku poj 1077 Eight (BFS + 康拓展开)
http://acm.hdu.edu.cn/showproblem.php?pid=1043 http://poj.org/problem?id=1077 Eight Time Limit: 1000 ...
- HDU 1043 Eight(双向BFS+康托展开)
http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...
随机推荐
- 基于Vuforia的Hololens图像识别
微软官方Hololens开发文档中有关于Vuforia的内容,https://developer.microsoft.com/en-us/windows/holographic/getting_sta ...
- (转)关于ARM9协处理器CP15及MCR和MRC指令
网址:http://blog.csdn.net/zhou1232006/article/details/6150198 在基于ARM的嵌入式应用系统中,存储系统通常是通过系统控制协处理器CP15完成的 ...
- bnuoj 1071 拼图++(BFS+康拓展开)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=1071 [题意]:经过四个点的顺逆时针旋转,得到最终拼图 [题解]:康拓展开+BFS,注意先预处理,得 ...
- Spark的TorrentBroadcast:概念和原理
依据Spark 1.4.1源码 SparkContext的broadcast方法 注释 可以用SparkContext将一个变量广播到所有的executor上,使得所有executor都能获取这个变量 ...
- Creating a new Signiant Transfer Engine because the previous transfer had to be canceled.
From: http://stackoverflow.com/questions/10548196/application-loader-new-weird-warning-about-signian ...
- Tomcat Java内存溢出 PermGen space 解决方案
-Xms300m -Xmx400m -XX:PermSize=128M -XX:MaxNewSize=256m -XX:MaxPermSize=256M
- MVC开发过程中的疑难杂症
MVC使用客户端验证 <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type=& ...
- json中jobject
Json.net codeplex :http://www.codeplex.com/Json 原本感觉Newtonsoft.Json和.net自己的JavaScriptSerializer相差无几, ...
- thinkphp 定制错误页面
在前台配置文件里加上: 'TMPL_EXCEPTION_FILE' => '.Public/tpl/error.html',// 异常cuowu页面的模板文件 然后在Public下新建一个tpl ...
- 转:数据包经由路由转发时源、目的IP地址及MAC地址变化情况
数据包经由路由转发时源.目的IP地址及MAC地址变化情况. IP数据包经由路由转发的时候源ip,目的ip,源MAC,目的mac是否发生改变,如何改变? A—–(B1-B2)—–(C1-C2)—— ...