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 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...
随机推荐
- 淘宝自己的前端框架KISSY(类似jquery) - 简易指南
KISSY 是由阿里集团前端工程师们发起创建的一个开源 JS 框架. 具备模块化.高扩展性.组件齐全,接口一致.自主开发.适合多种应用场景等特性. 在以下方面具有一定优势: A.拥有大量的中文文档: ...
- Mac - 更新 Ruby
因为准备在项目中使用bootstrap,在安装bootstrap过程中提示需要Ruby的版本在1.9.2以上,而目前使用的Ruby版本是Mac系统自带的1.8.7.所以需要对Ruby进行升级.这里使用 ...
- Apple移动设备处理器指令集 armv6、armv7、armv7s及arm64-b
Arm处理器,因为其低功耗和小尺寸而闻名,几乎所有的手机处理器都基于arm,其在嵌入式系统中的应用非常广泛,它的性能在同等功耗产品中也很出色. Armv6.armv7.armv7s.arm64都是ar ...
- JSP内置对象详解
jsp中内置对象:request.response.session.applecation.out.pagecontesx.config.page.exception.cookie 1.request ...
- httpmime-session 会话保持
sesion在浏览器和web服务器直接是通过一个叫做name为sessionid的cookie来传递的,所以只要在每次数据请求时保持sessionid是同一个不变就可以用到web的session了,做 ...
- Maven 执行Javadoc时控制台输出乱码问题
1.0 Maven 执行Javadoc时控制台输出乱码问题 问题描述 最近项目中使用maven-javadoc-plugin生成javadoc时,myEclipse控制台乱码. 插件配置 问题分析 ...
- 在SpringMVC利用MockMvc进行单元测试
spring在线文档:https://docs.spring.io/spring/docs/current/javadoc-api/index.html?index-files/index-13.ht ...
- datagridview 右键选中行 并弹出菜单
private void dataGridView_OLUsers_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) { i ...
- PAT-乙级-1001. 害死人不偿命的(3n+1)猜想 (15)
1001. 害死人不偿命的(3n+1)猜想 (15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 卡拉兹(Ca ...
- iphone绘图的几个基本概念CGPoint、CGSize、CGRect、CGRectMake、window(窗口)、视图(view)
我一般情况下不会使用interface builder去画界面,而是用纯代码去创建界面,不是装B,而是刚从vi转到xcode不久,不太习惯interface builder而已.当然如果需要我也会使用 ...