Eight II

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 130000/65536 K (Java/Others)
Total Submission(s): 4621    Accepted Submission(s): 1006

Problem Description
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
 
Author
zhymaoiing
 
Source
 
Recommend
zhouzeyong
题意:就是给起始八位码状态和结束八位码状态 求所需移动最少步数和操作步骤(以最小字典序)
 
这题卡了好久好久!!o(╥﹏╥)o ,看了大佬们的题解,才做出来的~
思路:这题结合康拓展开,映射,bfs打表就可以出来了
比如
起始状态12X453786  (120453786) 就可以替换为 120345678
 
映射关系
 
1 → 1
2 → 2
0 → 0
4 → 3
5 → 4
3 → 5
7 → 6
8 → 7
6 → 8
结束状态12345678X (12345678X) 就可以替换为 125348670
所以可以先将九种起始状态bfs打表
 
然后以结束状态八位码状态的康拓展开就可以得到我们想到的了~
 

康拓展开  %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; // 康托展开值
}
 

 
 
accode 
#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 ;
}
 

最新文章

  1. 【原】SDWebImage源码阅读(五)
  2. JS~~~ 前端开发一些常用技巧 模块化结构 &amp;&amp;&amp;&amp;&amp; 命名空间处理 奇技淫巧!!!!!!
  3. 1月11日,HTML学习笔记
  4. linux默认编辑器 sublime
  5. vbs下载文件
  6. PHPStrom 使用技巧以及基本设置教程【更新完结】
  7. (转)Objective-C中的instancetype和id区别
  8. zoj 3755
  9. 1.3. chromium源代码分析 - chromiumframe - 窗口系列
  10. Angular2.0的项目架构
  11. Flask Ansible自动化平台搭建(持续更新)
  12. php之array_column 的使用
  13. SQL Server数据库中表的查
  14. day75 form 组件(对form表单进行输入值校验的一种方式)
  15. 第K个幸运数字(4、7)
  16. Python开发——4.集合和字符串拼接
  17. README.md 编写
  18. A - Playground
  19. VC6.0在win 8.1中的安装使用
  20. bzoj1050[HAOI2006]旅行comf(枚举+贪心+并查集)

热门文章

  1. servlet打包成war
  2. day26-socket(server和client通信)
  3. [LC] 15. 3Sum
  4. Critical-Value|Critical-Value Approach to Hypothesis Testing
  5. 关于分频器的FPGA实现整理思路
  6. 91)PHP,cookie代码展示
  7. CSS性能优化探讨
  8. stack|session|fuss|anniversary
  9. Web服务器的配置与管理
  10. python3下scrapy爬虫(第二卷:初步抓取网页内容之直接抓取网页)