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 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...
随机推荐
- C# Windows - TabControl
TabControl控件的属性 - 一般用于控制TabPages对象容器的外观,特别是显示的选项卡的外观 属性 说明 Alignment 控制选项卡在选项卡控件的什么位置显示 Appearance 控 ...
- 在树莓派上 搭建sqlite数据库
最近找工作需要学习一些数据库方面的知识,所以就在实验室的树莓派上准备装个数据库试试,刚开始准备装一个mysql数据库,出现了很多问题,放弃了,后来查了一些资料原来还有很多可以用的小巧实用的数据库,sq ...
- WinForm 换行问题 textbox (转)
WinForm 换行问题 textbox 今天碰到一段string在label中能正常换行,但是在textbox中却无法换行的问题. 首先考虑是换行符的问题.在网上查了些资料: 1.TextBox 中 ...
- Cookie Session Cache
二. 工作机制 Ø Cookie :采用的是客户端保存信息的方案. Ø Session :采用服务器端保存信息的方案. Ø Cache :利用缓存 SRAM 来"静态"的保存写入信 ...
- 1064: [Noi2008]假面舞会 - BZOJ
Description 一年一度的假面舞会又开始了,栋栋也兴致勃勃的参加了今年的舞会.今年的面具都是主办方特别定制的.每个参加舞会的人都可以在入场时选择一 个自己喜欢的面具.每个面具都有一个编号,主办 ...
- CSS 命名规范及标题供参考与学习
一.CSS 命名规范 XHTML-CSS写作建议 所有的xhtml代码小写 属性的值一定要用双引号("")括起来,且一定要有值 每个标签都要有开始和结束,且要有正确的层次 空元 ...
- WCF获取客户端IP和端口
//提供方法执行的上下文环境 OperationContext context = OperationContext.Current; //获取传进的消息属性 MessageProperties pr ...
- 数据聚合 & 分组:新一代系统监控的核心功能
遥想 2015 年 8 月 17 日,Cloud Insight 还在梳理功能原型,畅想 Cloud Insight 存在的意义:为什么阿里云用户需要使用 Cloud Insight 来加强管理. 而 ...
- 【mysql的设计与优化专题(4)】表的垂直拆分和水平拆分
垂直拆分 垂直拆分是指数据表列的拆分,把一张列比较多的表拆分为多张表 通常我们按以下原则进行垂直拆分: 把不常用的字段单独放在一张表; 把text,blob等大字段拆分出来放在附表中; 经常组合查询的 ...
- Struts 2 + Spring2.5 + Hibernate3整合例子
一.效果 1. 2. 二.结构 1. 2.用到jar包 antlr-2.7.6.jaraspectjrt.jaraspectjweaver.jarc3p0-0.9.1.jarcglib-nodep-2 ...