#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
#define rep(i , n) for(int i = 0 ; i < (int)n ; i++)
#define rep1(i , x, y) for(int i = (int)x; i<=(int)y; i++)
const int inf = 0x3f3f3f3f;
int encode(int* a){
int num = 0 ;
rep(i, 6)
num = num * 10 + a[i];
return num;
}
void decode(int x,int * a){
for(int i = 5 ; i>= 0 ; i--)
{
a[i] = x % 10;
x/=10;
}
}
const int N = 1e6 + 100;
int vis[N][6][6], VIS[N][6];
struct node
{
int s, p, max_;
node(int s = 0,int p = 0, int max_ = 0) :s(s),p(p), max_(max_) {}
};
queue<node> q;
void Push(int s, int p, int max_,int val){
if(vis[s][p][max_] == -1)
{
vis[s][p][max_] = val + 1;
VIS[s][max_] = min(vis[s][p][max_], VIS[s][max_]);
q.push(node(s, p ,max_));
}
}
void init_bfs(){
memset(vis,-1,sizeof(vis));
memset(VIS,inf,sizeof(VIS));
VIS[123456][0] = 0;
int a[7];
rep(i, 6) a[i] = i + 1;
q.push(node(encode(a), 0, 0));
vis[encode(a)][0][0] = 0;
while(!q.empty())
{
node u = q.front();
q.pop();
int val = vis[u.s][u.p][u.max_];
if(u.p > 0)
{
Push(u.s, u.p - 1, u.max_, val);
int b[7];
decode(u.s, b);
swap(b[u.p], b[u.p - 1]);
int nts = encode(b);
Push(nts, u.p, u.max_, val);
}
if(u.p < 5)
{
Push(u.s, u.p + 1, max(u.max_, u.p + 1), val);
int b[7];
decode(u.s, b);
swap(b[u.p], b[u.p + 1]);
int nts = encode(b);
Push(nts, u.p, max(u.max_, u.p + 1), val); // 这里的max_很神啊
}
}
}
int a[7], b[7];
void solve(){
int c[6] = {1, 2, 3, 4, 5, 6};
int ans = inf;
do
{
int all = 0, max_ = 0 ;
rep(i, 6)
{
int fro = a[c[i] - 1];
int to_ = b[i];
if(fro != to_) max_ = max(max_, c[i] - 1 );
all += abs(fro - to_);
}
int exc = inf;
for(int i = max_ ; i< 6 ; i++)
exc = min(exc, VIS[encode(c)][i]);
all += exc;
// if(encode(c) == 612345 ){
// cout<<all <<" "<< exc<<" "<< max_ << endl;
// }
ans = min(ans,all);
}
while(next_permutation(c, c + 6));
printf("%d\n",ans);
}
char s[100];
void read(int* a){
scanf("%s",s);
rep(i, 6){
a[i] = s[i] - '0';
}
}
int main()
{
init_bfs();
int T;
scanf("%d",&T);
while(T--){
read(a);
read(b);
solve();
}
return 0;
}
山东省第七届省赛L题
最新推荐文章于 2023-04-13 22:13:18 发布