Eight II

Time Limit: 2000ms
Memory Limit: 65536KB

This problem will be judged on HDU. Original ID: 3567
64-bit integer IO format: %I64d      Java class name: Main

 
Eight-puzzle, which is also called "Nine grids", comes from an old game.

In this game, you are given a 3 by 3 board and 8 tiles. The tiles are numbered from 1 to 8 and each covers a grid. As you see, there is a blank grid which can be represented as an 'X'. Tiles in grids having a common edge with the blank grid can be moved into that blank grid. This operation leads to an exchange of 'X' with one tile.

We use the symbol 'r' to represent exchanging 'X' with the tile on its right side, and 'l' for the left side, 'u' for the one above it, 'd' for the one below it.

A state of the board can be represented by a string S using the rule showed below.

The problem is to operate an operation list of 'r', 'u', 'l', 'd' to turn the state of the board from state A to state B. You are required to find the result which meets the following constrains:
1. It is of minimum length among all possible solutions.
2. It is the lexicographically smallest one of all solutions of minimum length.

 

Input

The first line is T (T <= 200), which means the number of test cases of this problem.

The input of each test case consists of two lines with state A occupying the first line and state B on the second line.
It is guaranteed that there is an available solution from state A to B.

 

Output

For each test case two lines are expected.

The first line is in the format of "Case x: d", in which x is the case number counted from one, d is the minimum length of operation list you need to turn A to B.
S is the operation list meeting the constraints and it should be showed on the second line.

 

Sample Input

2
12X453786
12345678X
564178X23
7568X4123

Sample Output

Case 1: 2
dd
Case 2: 8
urrulldr

Source

 
解题:经典的八数码。。。。。IDA*大法好。。。。。。注意输出字典序最小的
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct sta{
int x,y;
sta(int a = ,int b = ){
x = a;
y = b;
}
};
char mp[maxn][maxn];
sta s[];
int nowx,nowy;
int h(){
int tmp = ;
for(int i = ; i < ; i++){
int x = i/,y = i%;
if(mp[x][y] == 'X') continue;
tmp += abs(x - s[mp[x][y]-''].x) + abs(y - s[mp[x][y]-''].y);
}
return tmp;
}
int ans[],limit;
const int dir[][] = {,,,-,,,-,};
const char d[] = {'d','l','r','u'};
bool ok;
int IDAstar(int x,int y,int p,int cur){
int bound = INF,tmp;
int hv = h();
if(cur + hv > limit) return cur + hv;
if(hv == ) {ok = true;return cur;}
for(int i = ; i < ; i++){
if(i == p) continue;
int tx = x + dir[i][];
int ty = y + dir[i][];
if(tx < || tx >= || ty < || ty >= ) continue;
swap(mp[x][y],mp[tx][ty]);
ans[cur] = i;
int nbound = IDAstar(tx,ty,-i,cur+);
if(ok) return nbound;
bound = min(bound,nbound);
swap(mp[x][y],mp[tx][ty]);
}
return bound;
}
int main() {
int t,cs = ;
char ch;
scanf("%d",&t);
getchar();
while(t--){
for(int i = ; i < ; i++){
ch = getchar();
if(ch == 'X'){
nowx = i/;
nowy = i%;
}
mp[i/][i%] = ch;
}
getchar();
for(int i = ; i < ; i++){
ch = getchar();
if(ch == 'X') continue;
s[ch-''] = sta(i/,i%);
}
getchar();
limit = h();
ok = false;
while(!ok) limit = IDAstar(nowx,nowy,-,);
printf("Case %d: %d\n",cs++,limit);
for(int i = ; i < limit; i++)
putchar(d[ans[i]]);
putchar('\n');
}
return ;
}

HDU 3567 Eight II的更多相关文章

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

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

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

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

  3. HDU 3567 Eight II BFS预处理

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

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

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

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

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

  6. HDU 2236 无题II(二分图匹配+二分)

    HDU 2236 无题II 题目链接 思路:行列仅仅能一个,想到二分图,然后二分区间长度,枚举下限.就能求出哪些边是能用的,然后建图跑二分图,假设最大匹配等于n就是符合的 代码: #include & ...

  7. Eight II HDU - 3567

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

  8. HDU 5919 Sequence II(主席树+逆序思想)

    Sequence II Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) To ...

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

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

随机推荐

  1. 转:APP测试总结

  2. HDU 5412 CRB and Queries(区间第K大 树套树 按值建树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5412 Problem Description There are N boys in CodeLan ...

  3. windows XP下Python2.7包管理工具安装-setuptool,pip、distribute、nose、virtualenv

    在Python开发中为了对项目进行管理和调试.必须安装一些特定的软件包.据说业内这个叫做yak shaving-做一个非常酷非常绚丽的Python项目之前,必须做的一些枯燥无味的准备工作.本文介绍了s ...

  4. Xamarin Mono For Android、Monotouch 安装

    一.Windows下面的安装 1. 安装环境介绍:    Win8.1 企业版64位或Win7 64.VS2013 update4 2. 安装jdk    到oracle官方下载jdk-8u45-wi ...

  5. 用树莓派实现RGB LED的颜色控制——C语言版本号

    用树莓派实现RGB LED的颜色控制  RGB色彩模式是工业界的一种颜色标准.是通过对红(R).绿(G).蓝(B)三个颜色通道的变化以及它们相互之间的叠加来得到各式各样的颜色的,RGB即是代 表红.绿 ...

  6. Objective-c基础知识学习笔记

    Objective-c基础知识学习笔记(一) 一直有记录笔记的习惯.但非常久没分享一些东西了,正好上半年開始学习IOS了,如今有空写点.因开发须要,公司特意为我们配置了几台新MAC.还让我们自学了2周 ...

  7. 网络 - TCP/IP四层模型,面向生产

    TCP.IP四层模型 (网络接口层,网际互联层,传输层,应用层.) (左边是车模,右边是实际生产的车.) 物理层,负责0101比特流传递. 数据链路层,MAC地址负责局域网,内网通信.MAC地址,由4 ...

  8. Amazon宣布将MXNet作为AWS的深度学习框架——貌似性能比tf高啊

    Amazon公司的Werner Vogels于上周宣布Amazon深度学习框架将会正式选用MXNet,并且AWS将会通过增加源代码贡献.改进文档以及支持来自其它框架的可视化.开发以及迁移工具,为实现M ...

  9. HTML不熟悉方法总结

    1. onblur   属性在元素失去焦点时触发. 2. onfocus 属性在元素获得焦点时触发. 3.addEventlistener 事件监听 4.focus() 方法用于给予该元素焦点.这样用 ...

  10. git 本地项目推送至远程仓库

    1 在本地文件夹下创建一个 Git 仓库(如test目录下) git init 2 此时test文件夹即是你的maste主分支,你可以在改文件夹下写自己的项目 3 将test文件夹下的内容提交至暂存区 ...