https://cn.vjudge.net/problem/HDU-3567

#include <stdio.h>
#include <math.h>
#include <queue>
#include <stack>
#include <string.h>
using namespace std;

struct Node
{
int s[9];
int loc;//存放0的位置
int state;//存放康托展开后的值
char d;
int fa;
}n[370000];
int len;
int dx[] = {1,0,0,-1};
int dy[] = {0,-1,1,0};
//按照字典序最小来搜索
char dir[] = "dlru";

int vis[9][370000];
long long path[9][370000];

int lev[] = {1,1,2,6,24,120,720,5040,40320,362880};
int cantor(int s[9])
{
int sum = 0;
for(int i = 0;i<9;i++)
{
int num = 0;
//计算逆序数
for(int j = i+1;j<9;j++)
{
if(s[j] < s[i]) num++;
}
sum+=num*lev[9 - i - 1];
}
return sum + 1;
}
void find_path(int pos,int e)
{
long long road = path[pos][e];
stack<int> sta;
int a;
for(int i = 0;i<vis[pos][e] - 1;i++)
{
a = road%4;
road = road>>2;
sta.push(a);
}
while(!sta.empty())
{
a = sta.top();sta.pop();
printf("%c",dir[a]);
}
printf("\n");

}
void bfs(int pos)
{
int head = 0,tail = 0;
n[0].fa = 0;
n[0].d = '\0';
n[0].loc = pos;
n[0].state = cantor(n[0].s);
vis[pos][n[0].state] = 1;
Node cur;
while(head <= tail)
{
cur = n[head];
//if(pos == 6) printf("Cur loc:: %d\n",cur.loc);
int x = cur.loc / 3;
int y = cur.loc % 3;
for(int i = 0 ;i < 4;i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if(nx>=0&&ny>=0&&nx<3&&ny<3)
{
Node nex = cur;
nex.fa = head;
nex.s[cur.loc] = nex.s[nx*3 + ny];
nex.loc = nx*3 + ny;
nex.s[nex.loc] = 0;
nex.state = cantor(nex.s);

if(!vis[pos][nex.state])
{
vis[pos][nex.state] = vis[pos][cur.state] + 1;
path[pos][nex.state] = (path[pos][cur.state]<<2) + i;
//if(pos == 6) printf("Nex loc:: %d\n",nex.loc);
n[++tail] = nex;
}
}
}
head++;
}
}

void init(int pos)
{
for(int i = 0,j = 1;i<9;i++)
{
if(i == pos)//pos代表X的位置
{
n[0].s[i] = 0;//x用0表示
}
else
{
n[0].s[i] = j++;
}
}
//printf("POS :: %d\n",pos);
bfs(pos);
}

int main()
{
int T,t;
int exc[10];
char src[10];
char des[10];
memset(path,0,sizeof path);
memset(vis,0,sizeof vis);
for(int i = 0;i<9;i++)
{
init(i);
}
scanf("%d",&T);
t = T;
while(T--){
scanf("%s",src);
int pos = 0;
for(int i = 0,j = 1;i<9;i++)
{
if(src[i] == 'X'){
src[i] = '0';
pos = i;
exc[0] = 0;
}
else
{
exc[src[i] - '0'] = j++;
}
}
scanf("%s",des);
int s[9];
for(int i = 0;i<9;i++)
{
if(des[i] == 'X')
{
s[i] = 0;
}
else
{
s[i] = exc[des[i] - '0'];
}
}
int tt = cantor(s);
printf("Case %d: %d\n",t-T,vis[pos][tt] - 1);
find_path(pos,tt);
}
return 0;
}

HDU - 3567的更多相关文章

  1. HDU 3567 Eight II(八数码 II)

    HDU 3567 Eight II(八数码 II) /65536 K (Java/Others)   Problem Description - 题目描述 Eight-puzzle, which is ...

  2. POJ-1077 HDU 1043 HDU 3567 Eight (BFS预处理+康拓展开)

    思路: 这三个题是一个比一个令人纠结呀. POJ-1077 爆搜可以过,94ms,注意不能用map就是了. #include<iostream> #include<stack> ...

  3. hdu 1430+hdu 3567(预处理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1430 思路:由于只是8种颜色,所以标号就无所谓了,对起始状态重新修改标号为 12345678,对目标状 ...

  4. HDU 3567 Eight II 打表,康托展开,bfs,g++提交可过c++不可过 难度:3

    http://acm.hdu.edu.cn/showproblem.php?pid=3567 相比Eight,似乎只是把目标状态由确定的改成不确定的,但是康托展开+曼哈顿为h值的A*和IDA*都不过, ...

  5. HDU 3567 Eight II

    Eight II Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 3 ...

  6. HDU 3567 Eight II BFS预处理

    题意:就是八数码问题,给你开始的串和结束的串,问你从开始到结束的最短且最小的变换序列是什么 分析:我们可以预处理打表,这里的这个题可以和HDU1430魔板那个题采取一样的做法 预处理打表,因为八数码问 ...

  7. HDU - 3567 Eight II (bfs预处理 + 康托) [kuangbin带你飞]专题二

    类似HDU1430,不过本题需要枚举X的九个位置,分别保存状态,因为要保证最少步数.要保证字典序最小的话,在扩展节点时,方向顺序为:down, left, right, up. 我用c++提交1500 ...

  8. HDU - 3567 IDA* + 曼哈顿距离 + 康托 [kuangbin带你飞]专题二

    这题难度颇大啊,TLE一天了,测试数据组数太多了.双向广度优先搜索不能得到字典序最小的,一直WA. 思路:利用IDA*算法,当前状态到达目标状态的可能最小步数就是曼哈顿距离,用于搜索中的剪枝.下次搜索 ...

  9. Eight II HDU - 3567

    Eight II Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 130000/65536 K (Java/Others)Total S ...

随机推荐

  1. Charles 注册码/破解/激活

    Charles 注册码 软件去官网下载安装即可. 适用于Charles任意版本的注册码 Charles 4.2.7 目前是最新版,可用.   Registered Name: https://zhil ...

  2. python3 re模块

    一.常用正则表达式符号和语法: '.' 匹配所有字符串,除\n以外 ‘-’ 表示范围[0-9] '*' 匹配前面的子表达式零次或多次.要匹配 * 字符,请使用 \*. '+' 匹配前面的子表达式一次或 ...

  3. jumpserver+Keepalived中一些配置

    haproxy的配置 这里只代理了luna coco的2222端口暂时没代理.后期有需求再改造 (py3) [root@dawn-jump-2 /app]# cat /etc/haproxy/hapr ...

  4. 左耳听风-ARTS-第4周(2019/4/21-2019/4/27)

    Algorithm 本周的算法题是删除已排序数据中的重复数字(https://leetcode.com/problems/remove-duplicates-from-sorted-array/).这 ...

  5. update_engine-FilesystemVerifierAction和PostinstallRunnerAction

    在介绍完了DownloadAction之后,还剩下FilesystemVerifierAction和PostinstallRunnerAction,下面开始对其进行分析. FilesystemVeri ...

  6. DNS搭建

    构建主从服务DNS 1.主服务名字:ns1.amber.com #hostname ns1.amber.com bash 刷新一下 #bash 2.Vim /etc/hosts 3.Vim /etc/ ...

  7. 【C++】Lambda表达式

    转自 https://www.cnblogs.com/DswCnblog/p/5629165.html 作者:dsw846 C++11的一大亮点就是引入了Lambda表达式.利用Lambda表达式,可 ...

  8. ISE_pll_ip的建立

    创建clk的ip核以及设置PLL的时钟输出 原理:外部晶振输入50M的频率,由ip核输出想要的频率 1.新建工程model再在"芯片"名称上建立clk的ip核 2.设置输入写为50 ...

  9. SSO单点登录三种情况的实现方式详解

    单点登录(SSO——Single Sign On)对于我们来说已经不陌生了.对于大型系统来说使用单点登录可以减少用户很多的麻烦.就拿百度来说吧,百度下面有很多的子系统——百度经验.百度知道.百度文库等 ...

  10. sql 查询结果自定义排序

    sqlserver 使用case when then 语句来实现 select name from fruit order by case name end oracle 使用decode实现 ,,, ...