您的位置:首页 > 其它

HDU 3567 - Eight II

2016-01-20 20:46 423 查看
B - Eight II
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice HDU
3567

Appoint description:
System Crawler (2016-01-18)

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


Eight的升级版,不仅要输出路径,还要保证字典序最小。一直写出来WA,最后参照别人的思路写终于A了。康托做Hash,4进制存储路径(方便比较字典序大小),BFS过程中遇到已访问的要更新。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <string>
#include <iostream>
using namespace std;

#define N 400000
#define ll __int64
#define INF 0x3f3f3f3f
int fac[10];
int vx[]={1,0,0,-1};
int vy[]={0,-1,1,0};
char d[]="dlru";
int dd[2][4]={0,1,2,3,3,2,1,0};
int vis[2]
;
ll path[2]
;
ll mi[30];

struct node{
int a[10];
int pos,step;
int flag,cant;
ll path;
};

int Cantor(int *s,int n){
int num=0;
for (int i=0;i<n;i++){
int cnt=0;
for (int j=i+1;j<n;j++)
cnt+=(s[j]<s[i]);
num+=fac[n-i-1]*cnt;
}
return num;
}

void init(){
fac[0]=mi[0]=1;
for (int i=1;i<10;i++)
fac[i]=fac[i-1]*i;

for (int i=1;i<30;i++)
mi[i]=mi[i-1]*4;
}

string getpath(ll c,int flag,int cant){
int str[100],pos=0;
for (int i=0;i<vis[flag][cant];i++){
str[pos++]=c%4;
c/=4;
}
string s="";
for (int i=pos-1;i>=0;i--)
s+=d[str[i]];
return s;
}

node start,endd;
void bfs(){
memset(vis,-1,sizeof(vis));
start.cant=Cantor(start.a,9);
start.step=0;
start.flag=0;
start.path=0;

endd.cant=Cantor(endd.a,9);
endd.step=0;
endd.flag=1;
endd.path=0;

vis[0][start.cant]=vis[1][endd.cant]=0;
if (start.cant==endd.cant){
printf("0\n\n");
return;
}

queue<node> q;
q.push(start);
q.push(endd);
int minans=INF;
ll str;
string res;

while (!q.empty()){
node cur=q.front();
q.pop();
int x=cur.pos/3;
int y=cur.pos%3;

for (int i=0;i<4;i++){
node com=cur;
int tx=x+vx[i];
int ty=y+vy[i];
if (tx<0 || ty<0 || tx>2 || ty>2)
continue;
com.pos=tx*3+ty;
swap(com.a[cur.pos],com.a[com.pos]);
int cant=Cantor(com.a,9);
com.cant=cant;
if (vis[com.flag][cant]!=-1){
if (cur.step+1>vis[com.flag][cant])
continue;
else{
if (com.flag)
str=dd[com.flag][i]*mi[cur.step]+cur.path;
else
str=cur.path*4+dd[com.flag][i];
if (path[com.flag][cant]>str)
path[com.flag][cant]=str;
}
}else{
vis[com.flag][cant]=cur.step+1;
if (com.flag)
path[com.flag][cant]=dd[com.flag][i]*mi[cur.step]+cur.path;
else
path[com.flag][cant]=cur.path*4+dd[com.flag][i];
}
com.step++;
com.path=path[com.flag][cant];
if (vis[com.flag^1][cant]!=-1){
string s=getpath(path[0][cant],0,cant)+getpath(path[1][cant],1,cant);
int len=s.length();
if (len>minans){
cout<<minans<<endl;
cout<<res<<endl;
return;
}
if (len<minans){
minans=len;
res=s;
}else{
if (res>s)
res=s;
}
}
q.push(com);
}
}
}

int main(){
init();
int t,kase=0;
scanf("%d",&t);
while (t--){
string s,t;
cin>>s>>t;
for (int i=0;i<9;i++){
if (s[i]=='X'){
start.a[i]=0;
start.pos=i;
}
else
start.a[i]=s[i]-'0';
}
for (int i=0;i<9;i++){
if (t[i]=='X'){
endd.a[i]=0;
endd.pos=i;
}
else
endd.a[i]=t[i]-'0';
}

printf("Case %d: ",++kase);
bfs();
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: