HDU 3567 Eight II BFS预处理
题意:就是八数码问题,给你开始的串和结束的串,问你从开始到结束的最短且最小的变换序列是什么
分析:我们可以预处理打表,这里的这个题可以和HDU1430魔板那个题采取一样的做法
预处理打表,因为八数码问题实际上是每个小块位置的变化,上面的数字只是用来标记位置的,
所以通过映射将初末序列进行置换就好了,然后因为每次的x字符的置换位置不一样
所以需要以123456789这个初始串打9遍表就好了733ms
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#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(int pos)
{
Node a;
for(int i=; i<=; ++i)
a.s[i]=''+i;
aim=a.hs=cantor(a.s);
o[pos][a.hs].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]==''+pos)x=i;
if(x+<)
{
bool flag=;
swap(a.s[x],a.s[x+]);
a.hs=cantor(a.s);
if(o[pos][a.hs].vis)
flag=;
if(!flag)
{
o[pos][a.hs].vis=;
o[pos][a.hs].c='d';
o[pos][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[pos][a.hs].vis)
flag=;
if(!flag)
{
o[pos][a.hs].vis=;
o[pos][a.hs].c='l';
o[pos][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[pos][a.hs].vis)
flag=;
if(!flag)
{
o[pos][a.hs].vis=;
o[pos][a.hs].c='r';
o[pos][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[pos][a.hs].vis)
flag=;
if(!flag)
{
o[pos][a.hs].vis=;
o[pos][a.hs].c='u';
o[pos][a.hs].pre=now;
q.push(a);
}
swap(a.s[x],a.s[x-]);
}
}
}
char s[],t[];
string res;
void getres(int u,int pos)
{
while(u!=aim)
{
res=res+o[pos][u].c;
u=o[pos][u].pre;
}
}
char mat[];
int main()
{
for(int i=;i<;++i)
for(int j=;j<=;++j)
o[j][i].vis=;
for(int i=;i<=;++i)
bfs(i);
int T,cas=;
scanf("%d",&T);
while(T--)
{
scanf("%s%s",s+,t+);
int flag;
for(int i=;i<=;++i)
{
if(s[i]=='X')s[i]='',flag=i;
if(t[i]=='X')t[i]='';
mat[s[i]-'']=i+'';
}
for(int i=;i<=;++i)
t[i]=mat[t[i]-''];
int ans=cantor(t);
res.clear();
getres(ans,flag);
printf("Case %d: %d\n",++cas,res.size());
reverse(res.begin(),res.end());
cout<<res<<endl;
}
return ;
}
HDU 3567 Eight II BFS预处理的更多相关文章
- HDU - 3567 Eight II (bfs预处理 + 康托) [kuangbin带你飞]专题二
类似HDU1430,不过本题需要枚举X的九个位置,分别保存状态,因为要保证最少步数.要保证字典序最小的话,在扩展节点时,方向顺序为:down, left, right, up. 我用c++提交1500 ...
- HDU 3567 Eight II(八数码 II)
HDU 3567 Eight II(八数码 II) /65536 K (Java/Others) Problem Description - 题目描述 Eight-puzzle, which is ...
- HDU 3533 Escape(BFS+预处理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3533 题目大意:给你一张n* m的地图,人在起点在(0,0)要到达终点(n,m)有k(k<=10 ...
- HDU - 1430 魔板 (bfs预处理 + 康托)
对于该题可以直接预处理初始状态[0, 1, 2, 3, 4, 5, 6, 7]所有可以到达的状态,保存到达的路径,直接打印答案即可. 关于此处的状态转换:假设有初始状态为2,3,4,5,0,6,7,1 ...
- HDU 3567 Eight II 打表,康托展开,bfs,g++提交可过c++不可过 难度:3
http://acm.hdu.edu.cn/showproblem.php?pid=3567 相比Eight,似乎只是把目标状态由确定的改成不确定的,但是康托展开+曼哈顿为h值的A*和IDA*都不过, ...
- HDU 3567 Eight II
Eight II Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 3 ...
- hdu 1430 魔板 (BFS+预处理)
Problem - 1430 跟八数码相似的一题搜索题.做法可以是双向BFS或者预处理从"12345678"开始可以到达的所有状态,然后等价转换过去直接回溯路径即可. 代码如下: ...
- POJ-1077 HDU 1043 HDU 3567 Eight (BFS预处理+康拓展开)
思路: 这三个题是一个比一个令人纠结呀. POJ-1077 爆搜可以过,94ms,注意不能用map就是了. #include<iostream> #include<stack> ...
- HDU 4856 Tunnels(BFS+状压DP)
HDU 4856 Tunnels 题目链接 题意:给定一些管道.然后管道之间走是不用时间的,陆地上有障碍.陆地上走一步花费时间1,求遍历全部管道须要的最短时间.每一个管道仅仅能走一次 思路:先BFS预 ...
随机推荐
- 【BZOJ 1015】[JSOI2008]星球大战starwar
Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过 ...
- kindeditor-4.1.10 结合 Asp.Net MVC 添加图片功能
KindEditor是一套开源的HTML可视化编辑器,现在我要结合Asp.Net MVC4 上传图片功能,做相应的配置和修改, 其实网上也有人写过类似的文章了,我写出来是以防以后使用的时候出现这样的问 ...
- dictionary 和 hashtable 区别
区别:1,Dictionary支持泛型,而Hashtable不支持. 2,Dictionary没有装填因子(Load Facto)概念,当容量不够时才扩容(扩容跟Hashtable一样,也是两倍于当前 ...
- js实现网页图片延时加载的原理和代码 提高网站打开速度
有时我们看到一些大型网站,页面如果有很多图片的时候,当你滚动到相应的行时,当前行的图片才即时加载的,这样子的话页面在打开只加可视区域的图片,而其它隐藏的图片则不加载,一定程序上加快了页面加载的速度,对 ...
- centos64位安装32位C/c++库
yum install glibc.i686 glibc-devel.i686 yum install libstdc++.i686yum install libstdc++-devel.i686yu ...
- ubuntu的vi
ubuntu12.04的vi 1. 安装vim full版本由于Ubuntu预安装的是tiny版本,就会导致我们在使用上的产生不便.所以我们要安装vim的full版本.首先,先卸掉旧版的vi,输入以下 ...
- 服务器程序源代码分析之三:gunicorn
服务器程序源代码分析之三:gunicorn 时间:2014-05-09 11:33:54 类别:网站架构 访问: 641 次 gunicorn是一个python web 服务部署工具,类似flup,完 ...
- python读写配置文件
#coding:utf-8 import ConfigParser class Conf(): def __init__(self,name): self.name = name self.cp = ...
- 修改MYSQL数据库表的字符集
MySQL 乱码的根源是的 MySQL 字符集设置不当的问题,本文汇总了有关查看 MySQL 字符集的命令.包括查看 MySQL 数据库服务器字符集.查看 MySQL 数据库字符集,以及数据表和字段的 ...
- Qt:QT右键菜单
Qt QTableView 上加右键弹出菜单, 并复制选中的单元格内容到剪贴板中 http://wenku.baidu.com/view/c51cfb63cf84b9d528ea7a29.html h ...