Eight II HDU - 3567
Eight II
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 130000/65536 K (Java/Others)
Total Submission(s): 4621 Accepted Submission(s): 1006
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.
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.
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.
12X453786
12345678X
564178X23
7568X4123
dd
Case 2: 8
urrulldr
康拓展开 %orz
康托展开是一个全排列到一个自然数的双射,常用于构建哈希表时的空间压缩。 康托展开的实质是计算当前排列在所有由小到大全排列中的顺序,因此是可逆的。
以下称第x个全排列是都是指由小到大的顺序。
康拓展开式
\[X=a_{n}\left ( n-1 \right )!+a_{n-1}\left ( n-2 \right )!+\cdots a_{1}\cdot 0!\]
例如,3 5 7 4 1 2 9 6 8 展开为 98884。因为X=2*8!+3*7!+4*6!+2*5!+0*4!+0*3!+2*2!+0*1!+0*0!=98884.
解释:
排列的第一位是3,比3小的数有两个,以这样的数开始的排列有8!个,因此第一项为2*8!
排列的第二位是5,比5小的数有1、2、3、4,由于3已经出现,因此共有3个比5小的数,这样的排列有7!个,因此第二项为3*7!
以此类推,直至0*0!
用途
显然,n位(0~n-1)全排列后,其康托展开唯一且最大约为n!,因此可以由更小的空间来储存这些排列。由公式可将X逆推出唯一的一个排列。
code
static const int FAC[] = {, , , , , , , , , }; // 阶乘
int cantor(int *a, int n)
{
int x = ;
for (int i = ; i < n; ++i) {
int smaller = ; // 在当前位之后小于其的个数
for (int j = i + ; j < n; ++j) {
if (a[j] < a[i])
smaller++;
}
x += FAC[n - i - ] * smaller; // 康托展开累加
}
return x; // 康托展开值
}
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<string>
#include<vector>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<cmath>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
#define MAX_N 362882 + 10
#define gcd(a,b) __gcd(a,b)
#define mem(a,x) memset(a,x,sizeof(a))
#define mid(a,b) a+b/2
#define stol(a) atoi(a.c_str())//string to long
int fac[];
int beg[][] ={{, , , , , , , , },{, , , , , , , , },{, , , , , , , , },{, , , , , , , , },{, , , , , , , , },{, , , , , , , , },{, , , , , , , , },{, , , , , , , , },{, , , , , , , , }};
int dir[][] = {{,},{,-},{,},{-,}};
char operate[] = "dlru";
int c;
int cal_cantor(int a[]){
int ans = ;
for (int i = ; i < ; i++){
int temp = ;
for (int j = i + ; j < ; j++){
if (a[j] < a[i]){
temp++;
}
}
ans += temp * fac[ - i];
}
return ans;
}
int temp[];
int mark[];
int start_cantor[];
struct Node{
int a[];
int x;
};
struct Vis{
int pre;
char p;
int step;
}vis[][MAX_N]; void bfs(int t,Node node){
queue<Node> que;
que.push(node);
while(que.size()){
Node n = que.front();
que.pop();
int n_contor = cal_cantor(n.a);
int pos = n.x;
for(int i = ; i < ; i++){
int x = n.x/;
int y = n.x%;
int nx = x + dir[i][];
int ny = y + dir[i][];
if(nx >= && nx < && ny >= && ny < ){
int cnt = nx * + ny;
swap(n.a[cnt],n.a[pos]);
n.x = cnt;
int v = cal_cantor(n.a);
if(vis[t][v].pre == -&&v!=start_cantor[t]){
vis[t][v].pre = n_contor;
vis[t][v].p = operate[i];
vis[t][v].step = vis[t][n_contor].step + ;
que.push(n);
}
n.x = pos;//
swap(n.a[cnt],n.a[pos]);
} } }
} void init(){
fac[] = fac[] = ;
for (int i = ; i < ; i++){
fac[i] = fac[i - ] * i;
}
for(int i = ; i < ; i++){
for(int j = ; j < MAX_N;j++)
vis[i][j].pre = -;
}
Node node;
for(int i = ; i < ; i++){
swap(node.a,beg[i]);
node.x = i;
start_cantor[i] = cal_cantor(node.a);
bfs(i,node);
swap(node.a,beg[i]);
}
}
int main(){
//std::ios::sync_with_stdio(false);
//std::cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
#else
#endif
init();
int T;
scanf("%d",&T);
string str;
int t = ;
while(T--){
cin >> str;
for(int i = ; i < ; ++i){
temp[i] = (str[i] == 'X'? : str[i]-'');
if(str[i] == 'X')
c = i;
}
for(int i = ; i < ; ++i){
mark[temp[i]] = beg[c][i];
}
cin >> str;
for(int i = ; i < ; ++i){
temp[i] = (str[i] == 'X'? : str[i]-'');
temp[i] = mark[temp[i]];
}
Node n;
swap(n.a,temp);
int end_ = cal_cantor(n.a);
printf("Case %d: %d\n",++t,vis[c][end_].step);
string ans = "";
while(vis[c][end_].step!=){
ans = vis[c][end_].p + ans;
end_ = vis[c][end_].pre;
}
cout<<ans<<endl; } return ;
}
Eight II HDU - 3567的更多相关文章
- HDU 3567 Eight II(八数码 II)
HDU 3567 Eight II(八数码 II) /65536 K (Java/Others) Problem Description - 题目描述 Eight-puzzle, which is ...
- POJ-1077 HDU 1043 HDU 3567 Eight (BFS预处理+康拓展开)
思路: 这三个题是一个比一个令人纠结呀. POJ-1077 爆搜可以过,94ms,注意不能用map就是了. #include<iostream> #include<stack> ...
- HDU 3567 Eight II
Eight II Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 3 ...
- 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 BFS预处理
题意:就是八数码问题,给你开始的串和结束的串,问你从开始到结束的最短且最小的变换序列是什么 分析:我们可以预处理打表,这里的这个题可以和HDU1430魔板那个题采取一样的做法 预处理打表,因为八数码问 ...
- HDU - 3567 Eight II (bfs预处理 + 康托) [kuangbin带你飞]专题二
类似HDU1430,不过本题需要枚举X的九个位置,分别保存状态,因为要保证最少步数.要保证字典序最小的话,在扩展节点时,方向顺序为:down, left, right, up. 我用c++提交1500 ...
- hdu 1430+hdu 3567(预处理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1430 思路:由于只是8种颜色,所以标号就无所谓了,对起始状态重新修改标号为 12345678,对目标状 ...
- (回文串 Manacher)吉哥系列故事——完美队形II -- hdu -- 4513
http://acm.hdu.edu.cn/showproblem.php?pid=4513 吉哥系列故事——完美队形II Time Limit: 3000/1000 MS (Java/Others) ...
- (全排列)Ignatius and the Princess II -- HDU -- 1027
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1027 Ignatius and the Princess II Time Limit: 2000/100 ...
随机推荐
- D. Salary Changing(找中位数)
题:https://codeforces.com/contest/1251/problem/D 题意:给你n个单位需要满足达到的区间,再给个s,s是要分配给n的单位的量,当然∑l<=s,问经过分 ...
- day08-内置函数和匿名函数
1. 1)网络编程只能是二进制.2)照片和视频也是以二进制储存. 3)html网页爬取到的也是二进制编码. 2. 非常重要的4个内置函数:zip ,filter,map,sorted 1)zip: 例 ...
- orthologs|paralogs
从这里我们可以举例说明,例如人的S100A8基因与猪的S100A8基因即为orthologs.人的a球蛋白和b球蛋白基因即为paralogs.需要补充的是,人的a球蛋白和鼠的b球蛋白基因也为paral ...
- 项目引入nacos 日志不显示问题
禁用nacos的日志即可解决 idea当中 添加vm options参数即可 -Dnacos.logging.default.config.enabled=false 打包后的启动命令 java - ...
- java中==和equals
/** * @author zhaojiatao * @date 2018/7/19 */ public class equalsLearn { public static void main(Str ...
- 初始化方法,init,构造器
1.继承于NSObject class student: NSObject { var name : String? var age : Int = var friend : Int = init(n ...
- ROC曲线、KS曲线
一.ROC曲线 ROC曲线由混淆矩阵为基础数据生成. 纵坐标:真阳性比率TPR,预测为正占真正为正的比例. 横坐标:假阳性比率FPR,预测为正占真正为负的比例. 除了roc曲线的纵横坐标外,还有一个准 ...
- JS代码,从一个数组中得到连号的数并显示
JavaScript code function m() { var k = [1, 2, 7, 8, 9, 11, 22, 35, 36]; ) return; ; ; ; ; i < k.l ...
- HTML的img标签:alt属性和title属性
当浏览器卖主扭曲了标准并且自顾自的不按规则去做一些事,他们可能会造成一些问题,或者至少产生了混淆.例子之一就是一些浏览器处理alt属性(一般会被错误的称作alt标签)的方式,比如拥有大量用户的Wind ...
- Longest Increasing Subsequence (Medium)
第一次做题思路201511092250 1.采用map存储,key为nums[i],value为以nums[i]为结尾的最大递增子序列的长度 2.采用map里面的lower_bounder函数直接找出 ...