您的位置:首页 > 其它

HDU3567:Eight II(康拓展开+预处理) (B)

2017-07-08 15:52 295 查看
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

 

题意:由字符串1变成字符串2所需要在最小步数的情况下输出其字典序最小的方案
思路:八数码问题,处理这种问题首先想到的方法自然是康拓,我们可以先枚举X不同位置的9中方案,用bfs搜出这种方案到其他所有方案的移动方法,那么在后面我们就可以直接得出答案,使用逆推的方法
用A到B的话,可以将A看成是9种状态的一种,同样对B进行相应转化,最后相对于从一个状态转化到9中初始状态中的一种。 例如A为12X453786我们可以把它看成12X345678  ,其中我们把4看成了3,5看成了4,3看成了5等等,按照这个转化,即可将B12345678X转化为12534867X,这样我们相当于求12534867X变道初始状态之一的12X345678

本题还有个WA点是要求输出字典序较小的,那么往周围四个方向交换试探的顺序为dlru。

还有发现在c++下提交可能会超时  在G++下提交可以ac

#include<stdio.h>
#include<string.h>
#include<cstring>
#include<math.h>
#include<queue>
#include<stack>
#include<algorithm>

using namespace std;

struct node
{
int x,y; //x的坐标位置
char maps[5][5];
node() {}
node(char *s) //构造函数 将一个字符串直接处理赋值给maps数组
{
int i,j;
int xx = 0,yy = 0;
for(i = 0; i<strlen(s); i++)
{
maps[xx][yy] = s[i];
if(s[i] == 'X')
{
x = xx;
y = yy;
}
yy++;
if(yy == 3)
{
xx++;
yy = 0;
}
}
}
}s;
char str[50];
int num[20];
int sum[15];//阶乘
bool vis[500000];//用于判重
char ans[10][500000];//记录变化
int pre[10][500000];//记录康拓值
int to[4][2] = {1,0,0,-1,0,1,-1,0};
char way[10] = "dlru";

int solve(node a)//康托展开求值
{
int ans=0;
int s[20];//存每个位置的值
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
s[i*3+j]=a.maps[i][j];
int cnt=0;
for(int k=3*i+j-1; k>=0; k--)
{
if(s[k]>s[3*i+j])
cnt++;
}
ans = ans+sum[i*3+j]*cnt;
}
}
return ans;
}

void bfs(int goal)
{
memset(pre[goal],-1,sizeof(pre[goal]));
memset(vis,false,sizeof vis);
node a,next;
queue<node>q;
q.push(s);
vis[solve(s)] = true;
while(!q.empty())
{
a=q.front();
q.pop();
int k_s=solve(a);//求a的康脱展开式的值
for(int i=0;i<4;i++)
{
next=a;
next.x+=to[i][0];
next.y+=to[i][1];//x点的转移
if(next.x < 0 || next.y < 0 || next.x>2 || next.y > 2)
continue;
next.maps[a.x][a.y] = a.maps[next.x][next.y];//x点与其旁边的点的交换
next.maps[next.x][next.y] = 'X';
int k_n = solve(next);
if(vis[k_n])//对康拓展开式的值 进行判重 每个康拓展开式的值对应唯一的状态
continue;
vis[k_n] = true;
q.push(next);
pre[goal][k_n] = k_s;//记录康拓值 用于查找上一个状态
ans[goal][k_n] = way[i];//记录如何变化的 用于结果的输出
}
}
}

int main()
{

int t;
int x,y,len,i,j;
sum[0]=1;
for(int i=1;i<=9;i++)//阶乘
sum[i]=sum[i-1]*i;
s = node("X12345678");//对9个初始状态进行预处理
bfs(0);
s = node("1X2345678");
bfs(1);
s = node("12X345678");
bfs(2);
s = node("123X45678");
bfs(3);
s = node("1234X5678");
bfs(4);
s = node("12345X678");
bfs(5);
s = node("123456X78");
bfs(6);
s = node("1234567X8");
bfs(7);
s = node("12345678X");
bfs(8);
scanf("%d",&t);
int cas=1;
while(t--)
{
scanf("%s",str);
int p;//x的位置 表示将第几个预处理作为他的初始状态
for(i = 0,j = 0; i<9; i++)//保存位置,因为前面预处理的都是位置
{
if(str[i]=='X') p = i;
else
num[str[i]-'0'] = j++;
}
scanf("%s",str);
for(i = 0; i<9; i++)//求出目标状态每个数在原状态的位置
{
if(str[i]=='X')
continue;
str[i] = num[str[i]-'0']+'1';
}
s = node(str);//由目标态逆推到初始态
int sum = solve(s);
string ss="";
while(sum!=-1)
{
ss+=ans[p][sum];
sum = pre[p][sum];
}
printf("Case %d: %d\n",cas++,ss.size()-1);
for(i = ss.size()-2; i>=0; i--)//由于方案是逆推,输出也要逆推
printf("%c",ss[i]);
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: