思路:把每一个状态当成一个结点,每次看能变到什么状态,边权值为一求其最小步数
一.魔板
Rubik 先生在发明了风靡全球的魔方之后,又发明了它的二维版本——魔板。
这是一张有 8 个大小相同的格子的魔板:
1 2 3 4
8 7 6 5
我们知道魔板的每一个方格都有一种颜色。
这 8 种颜色用前 8 个正整数来表示。
可以用颜色的序列来表示一种魔板状态,规定从魔板的左上角开始,沿顺时针方向依次取出整数,构成一个颜色序列。
对于上图的魔板状态,我们用序列 (1,2,3,4,5,6,7,8)来表示,这是基本状态。
这里提供三种基本操作,分别用大写字母 A,B,C 来表示(可以通过这些操作改变魔板的状态):
A:交换上下两行;
B:将最右边的一列插入到最左边;
C:魔板中央对的4个数作顺时针旋转。
下面是对基本状态进行操作的示范:
A:
8 7 6 5
1 2 3 4
B:
4 1 2 3
5 8 7 6
C:
1 7 2 4
8 6 3 5
对于每种可能的状态,这三种基本操作都可以使用。
你要编程计算用最少的基本操作完成基本状态到特殊状态的转换,输出基本操作序列。
注意:数据保证一定有解。
输入格式
输入仅一行,包括 8个整数,用空格分开,表示目标状态。
输出格式
输出文件的第一行包括一个整数,表示最短操作序列的长度。
如果操作序列的长度大于0,则在第二行输出字典序最小的操作序列。
数据范围
输入数据中的所有数字均为 1 到 8 之间的整数。
输入样例:
2 6 8 4 5 7 3 1
输出样例:
7
BCABCCB
#include<iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;
map<string,pair<char,string>> pre;
string move0(string s)
{
for(int i = 0 ; i < 4 ; i++) swap(s[i],s[7-i]);
return s;
}
string move1(string s)
{
for (int i = 0; i < 3; ++ i) swap(s[2 - i], s[3 - i]), swap(s[4 + i], s[5 + i]);
return s;
}
string move2(string s)
{
swap(s[1],s[2]),swap(s[5],s[6]),swap(s[1],s[5]);
return s;
}
void bfs(string st,string ed)
{
if(st == ed) return ;
queue<string> q;
q.push(st);
while(q.size())
{
string s = q.front();
q.pop();
if(s == ed) return;
string m[3];
m[0] = move0(s),m[1] = move1(s),m[2] = move2(s);
for(int i = 0 ; i < 3 ; i++)
{
if(!pre.count(m[i]))
{
q.push(m[i]);
pre[m[i]] = {'A'+i,s};
}
}
}
}
int main()
{
string st,ed,res;
st = "12345678";
for(int i = 0 ; i < 8 ; i++)
{
char c;
cin >> c;
ed = ed + c;
}
bfs(st,ed);
while(st!=ed)
{
res = pre[ed].first + res;
ed = pre[ed].second;
}
if(res.size()==0) cout << "0";
else
{
cout << res.size() << endl;
cout << res;
}
return 0;
}
二.八数码
在一个 3×3 的网格中,1∼8 这 8 个数字和一个 X
恰好不重不漏地分布在这 3×3的网格中。
例如:
1 2 3
X 4 6
7 5 8
在游戏过程中,可以把 X
与其上、下、左、右四个方向之一的数字交换(如果存在)。
我们的目的是通过交换,使得网格变为如下排列(称为正确排列):
1 2 3
4 5 6
7 8 X
例如,示例中图形就可以通过让 X
先后与右、下、右三个方向的数字交换成功得到正确排列。
交换过程如下:
1 2 3 1 2 3 1 2 3 1 2 3
X 4 6 4 X 6 4 5 6 4 5 6
7 5 8 7 5 8 7 X 8 7 8 X
把 X
与上下左右方向数字交换的行动记录为 u
、d
、l
、r
。
现在,给你一个初始网格,请你通过最少的移动次数,得到正确排列。
输入格式
输入占一行,将 3×3 的初始网格描绘出来。
例如,如果初始网格如下所示:
1 2 3
x 4 6
7 5 8
则输入为:1 2 3 x 4 6 7 5 8
输出格式
输出占一行,包含一个字符串,表示得到正确排列的完整行动记录。
如果答案不唯一,输出任意一种合法方案即可。
如果不存在解决方案,则输出 unsolvable
。
输入样例:
2 3 4 1 5 x 7 6 8
输出样例
ullddrurdllurdruldr
AC代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include<queue>
using namespace std;
unordered_map<string,string> pre;
unordered_map<string,int> p;
string ed = "12345678x";
char ch[5] = "drul"; //根据dx,dy得到移动方式
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
bool bfs(string s)
{
queue<string> q; //创建状态队列
q.push(s);
p[s] = 0;
while(q.size())
{
string t = q.front();
q.pop();
if(t == ed) return true;
int k = t.find('x');
int x = k / 3 , y = k % 3; //一维变成二维
string cop = t;
for(int i = 0 ; i < 4 ; i++)
{
int a = x + dx[i];
int b = y + dy[i];
if(a >= 0 && a < 3 && b >= 0 && b < 3)
{
int num = a * 3 + b; //新x的位置
swap(t[k],t[num]);
if(!p.count(t)) //map.count(key)!!
{
pre[t] = cop;
p[t] = i;
q.push(t);
}
swap(t[num],t[k]);
}
}
}
return false;
}
int main()
{
string st,res;
for(int i = 0 ; i < 9 ; i++)
{
char c;
cin >> c;
st += c;
}
if(bfs(st))
{
string t = ed;
while(st!= t)
{
res = ch[p[t]] + res;
t = pre[t];
}
cout << res << endl;
}
else
{
puts("unsolvable");
}
return 0;
}