【题目描述】
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:
八数码,也被称为“九宫格”,来自一个古老的游戏。
在这个游戏中,你会得到一个3乘3的棋盘和8块瓷砖。瓷砖编号从1到8,每个覆盖一个网格。正如您所看到的,有一个空白的网格,可以表示为‘X’。与空白网格具有共同边缘的网格中的块可以移动到该空白网格中。这个操作导致用一个瓷砖交换“X”。
我们用符号‘r’来表示‘X’和右边的瓷砖交换,‘l’代表左边,‘u’代表上面的,‘d’代表下面的。
九宫格状态可以使用字符串S表示用如下展示的规则。
问题是操作一个‘r’,‘u’,‘l’,‘d’的操作列表,将九宫格的状态从状态A转到B状态。你需要找到满足以下约束的结果:
1.在所有可能的解决方案中,它的长度最小。
2.在所有最小长度的解中,它是字典序最小的解。
【输入】
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.
第一行是T(T<=200),表示这个问题的测试样例数。
每个测试用例的输入由两行组成,状态A占据第一行,状态B位于第二行。保证从A状态到B状态有可用的解决方案。
【输出】
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.
对于每个测试样例两行。
第一行“case x:d”的格式,其中x是从1计数的大小写数,d是您需要将A转换为B的最小操作列表长度
S是满足约束的操作列表,显示在第二行上。
【样例输入】
2
12X453786
12345678X
564178X23
7568X4123
【样例输出】
Case 1: 2
dd
Case 2: 8
urrulldr
题目链接:https://cn.vjudge.net/problem/HDU-3567
前一题已经自闭两天了,没想到还有一道hard版本
偷看大佬题解看到映射成九种情况第一反应是竟然还有这种操作,emmm……是我太菜了
其他的和前一题差不多的思路
偷看的题解在这里 → https://blog.csdn.net/codeswarrior/article/details/80313587
分成9种情况映射,bfs存下所有可能,vis标记方向,pre还原路径
代码如下:
#include
#include
#include
#include
#include
using namespace std;
static const int MAXN=362880;
static const int fac[]={1,1,2,6,24,120,720,5040,40320};
static const int dx[]={1,0,0,-1},dy[]={0,-1,1,0};
static const string op="dlru";
static const string s[9]={"X12345678","1X2345678","12X345678","123X45678","1234X5678","12345X678","123456X78","1234567X8","12345678X"};
int vis[9][MAXN+10];
int pre[9][MAXN+10];
struct Node{
int num[9];
int hashs;
int pos;
};
int cantor(int *st)
{
int sum=0;
for(int i=0;i<=8;i++)
{
int cnt=0;
for(int j=i+1;j<=8;j++)
{
if(st[j]<st[i])
cnt++;
}
sum+=fac[8-i]*cnt;
}
return sum;
}
void print(int hashs,int num)
{
string st;
while(hashs!=-1)
{
st+=op[vis[num][hashs]];
hashs=pre[num][hashs];
}
cout<<st.size()-1<<endl;
for(int i=st.size()-2;i>=0;i--)
cout<<st[i];
cout<<endl;
}
void bfs(Node node,int num)
{
queue<Node> Q;
Q.push(node);
vis[num][node.hashs]=1;
while(!Q.empty())
{
Node now=Q.front();
Q.pop();
for(int i=0;i<4;i++)
{
if(0<=now.pos/3+dx[i] && now.pos/3+dx[i]<=2 && 0<=now.pos%3+dy[i] && now.pos%3+dy[i]<=2)
{
Node next=now;
swap(next.num[now.pos],next.num[(now.pos/3+dx[i])*3+now.pos%3+dy[i]]);
next.hashs=cantor(next.num);
if(vis[num][next.hashs]==-1)
{
next.pos=(now.pos/3+dx[i])*3+now.pos%3+dy[i];
vis[num][next.hashs]=i;
pre[num][next.hashs]=now.hashs;
Q.push(next);
}
}
}
}
}
void init(int num)
{
Node node;
for(int i=0;i<=8;i++)
{
if(s[num][i]=='X')
{
node.num[i]=0;
node.pos=i;
}
else
node.num[i]=s[num][i]-'0';
}
node.hashs=cantor(node.num);
bfs(node,num);
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0),cout.tie(0);
memset(vis,-1,sizeof(vis));
memset(pre,-1,sizeof(pre));
for(int i=0;i<=8;i++)
init(i);
int T;
cin>>T;
for(int kase=1;kase<=T;kase++)
{
string st;
cin>>st;
int c[10],tem[10];
int cnt=1,num;
for(int i=0;i<=8;i++)
if(st[i]=='X')
{
c[0]=0;
num=i;
}
else
c[st[i]-'0']=cnt++;
cin>>st;
for(int i=0;i<=8;i++)
if(st[i]=='X')
tem[i]=c[0];
else
tem[i]=c[st[i]-'0'];
int hashs=cantor(tem);
cout<<"Case "<<kase<<": ";
print(hashs,num);
}
return 0;
}