魔板(洛谷-P2730)
题目描述
在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板。这是一张有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”:魔板中央四格作顺时针旋转。
下面是对基本状态进行操作的示范:
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个整数,用空格分开(这些整数在范围 1——8 之间)不换行,表示目标状态。
输出格式:
Line 1: 包括一个整数,表示最短操作序列的长度。
Line 2: 在字典序中最早出现的操作序列,用字符串表示,除最后一行外,每行输出60个字符。
输入输出样例
输入样例#1:
2 6 8 4 5 7 3 1
输出样例#1:
7
BCABCCB
思路:
很容易想到用 BFS 来做,即找到 A、B、C 三种操作的规律,然后进行搜索,将搜索到的状态加入 BFS 的队列中,这样 8 个位置上的数就要转换成一个 8 位数,判重时要长度 10^8 的的数组,开不了这么大的数组
因此本题的难度就在于判重,网上的题解多有使用康拓展开来进行状态判定从而去从去重的,但其实可以使用 map 来作为 vis 数组,即将 8 个位置上的数转成一个 string 的形式,然后当做 map 的键,其对应的值即为达到该状态的操作符
因此只要写三个函数将操作序列 A、B、C 模拟一下,然后 BFS 即可
源代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL multMod(LL a,LL b,LL mod){ a%=mod; b%=mod; LL res=0; while(b){if(b&1)res=(res+a)%mod; a=(a<<=1)%mod; b>>=1; } return res%mod;}
LL quickMultPowMod(LL a, LL b,LL mod){ LL res=1,k=a; while(b){if((b&1))res=multMod(res,k,mod)%mod; k=multMod(k,k,mod)%mod; b>>=1;} return res%mod;}
LL quickPowMod(LL a,LL b,LL mod){ LL res=1; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1; } return res; }
LL getInv(LL a,LL mod){ return quickPowMod(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-6;
const int MOD = 1000000000+7;
const int N = 1000+5;
const int dnewStr[] = {0,0,-1,1,1,-1,1,1};
const int dy[] = {1,-1,0,0,-1,1,-1,1};
using namespace std;
queue<string> Q;
map<string, string> mp;
void opA(string oldStr) {
string newStr = oldStr;
swap(newStr[0], newStr[7]);
swap(newStr[1], newStr[6]);
swap(newStr[2], newStr[5]);
swap(newStr[3], newStr[4]);
if (mp.count(newStr) == 0) {
Q.push(newStr);
mp[newStr] = mp[oldStr] + "A";
}
}
void opB(string oldStr) {
string newStr = oldStr;
swap(newStr[0], newStr[3]);
swap(newStr[4], newStr[5]);
swap(newStr[1], newStr[3]);
swap(newStr[5], newStr[6]);
swap(newStr[2], newStr[3]);
swap(newStr[6], newStr[7]);
if (mp.count(newStr) == 0) {
Q.push(newStr);
mp[newStr] = mp[oldStr] + "B";
}
}
void opC(string oldStr) {
string newStr = oldStr;
swap(newStr[1], newStr[2]);
swap(newStr[5], newStr[6]);
swap(newStr[1], newStr[5]);
if (mp.count(newStr) == 0) {
Q.push(newStr);
mp[newStr] = mp[oldStr] + "C";
}
}
void BFS(string target) {
Q.push("12345678");
mp["12345678"] = "";
while (!Q.empty()) {
string str = Q.front();
Q.pop();
opA(str);
opB(str);
opC(str);
if (mp.count(target) == 1) {
cout << mp[target].length() << endl;
cout << mp[target] << endl;
break;
}
}
}
int main(){
string str("12345678");
cin >> str[0] >> str[1] >> str[2] >> str[3];
cin >> str[4] >> str[5] >> str[6] >> str[7];
BFS(str);
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】