原题链接:魔板
解题思路:
注意事项:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<set>
using namespace std;
typedef struct node {
int data;
int d[10];
int len;
int road[100];
}node;
const int maxn = 10000;
int A[10] ,B[10];
int hase(int *d){
int sum=0;
for(int i=1;i<=8;i++)
sum=sum*10+d[i];
return sum ;
}
int fa(int *t){
return hase(t)/10000+hase(t)%10000*10000;
}
int fb(int *t){
int a[10];
a[1]=t[2];a[2]=t[3];a[3]=t[4];a[4]=t[1];
a[5]=t[6];a[6]=t[7];a[7]=t[8];a[8]=t[5];
return hase(a);
}
int fc(int *t){
int a[10];
a[1]=t[1];a[4]=t[4];a[5]=t[5];a[8]=t[8];
a[2]=t[6];a[3]=t[2];a[6]=t[7];a[7]=t[3];
return hase(a);
}
int main(void){
int T;
cin >> T;
while(T--){
for(int i=1;i<=4;i++) scanf("%1d",&A[i]);
for(int i=1;i<=4;i++) scanf("%1d",&A[9-i]);
for(int i=1;i<=4;i++) scanf("%1d",&B[i]);
for(int i=1;i<=4;i++) scanf("%1d",&B[9-i]);
queue<node>q;
set<int>s;
node a ;
for(int i=1;i<=8;i++) a.d[i]=A[i];
a.data = hase(A);
a.len = 0 ;
q.push(a);
s.insert(a.data);
int b_hash=hase(B);
while(!q.empty()){
node x = q.front(); q.pop();
if(x.data == b_hash){
for(int i=0;i<x.len;i++) printf("%c",'A'+x.road[i]);
printf("\n");
break;
}
int t[10],base=10000000;
for(int i=1;i<=8;i++){ t[i]=x.data/base%10; base /=10;}
int a=fa(t),b =fb(t),c=fc(t);
if(s.count(a)==0) {
s.insert(a);
node y;
y.data=a;
y.len=x.len+1;
memcpy(y.road,x.road,sizeof(y.road));
y.road[x.len]=0;
q.push(y);
}
if(s.count(b)==0) {
s.insert(b);
node y;
y.data=b;
y.len=x.len+1;
memcpy(y.road,x.road,sizeof(y.road));
y.road[x.len]=1;
q.push(y);
}
if(s.count(c)==0) {
s.insert(c);
node y;
y.data=c;
y.len=x.len+1;
memcpy(y.road,x.road,sizeof(y.road));
y.road[x.len]=2;
q.push(y);
}
}
}
return 0;
}
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
typedef struct node {
int data;
int len;
int road[100];
}node;
int s[87654321] ;
node q[1000000];
const int maxn = 10000;
int A[10] ,B[10];
int hase(int *d){
int sum=0;
for(int i=1;i<=8;i++)
sum=sum*10+d[i];
return sum ;
}
int fa(int *t){
return hase(t)/10000+hase(t)%10000*10000;
}
int fb(int *t){
int a[10];
a[1]=t[2];a[2]=t[3];a[3]=t[4];a[4]=t[1];
a[5]=t[6];a[6]=t[7];a[7]=t[8];a[8]=t[5];
return hase(a);
}
int fc(int *t){
int a[10];
a[1]=t[1];a[4]=t[4];a[5]=t[5];a[8]=t[8];
a[2]=t[6];a[3]=t[2];a[6]=t[7];a[7]=t[3];
return hase(a);
}
int main(void){
int T;
cin >> T;
while(T--){
memset(s,0,sizeof(s));
for(int i=1;i<=4;i++) scanf("%1d",&A[i]);
for(int i=1;i<=4;i++) scanf("%1d",&A[9-i]);
for(int i=1;i<=4;i++) scanf("%1d",&B[i]);
for(int i=1;i<=4;i++) scanf("%1d",&B[9-i]);
node a ;
a.data = hase(A);
a.len = 0 ;
q[1]=a;
s[a.data]=1;
int b_hash=hase(B);
int rear=1,bottom=2;
while(rear<bottom){
node x = q[rear]; rear++;
if(x.data == b_hash){
for(int i=0;i<x.len;i++) printf("%c",'A'+x.road[i]);
printf("\n");
break;
}
int t[10],base=10000000;
for(int i=1;i<=8;i++){ t[i]=x.data/base%10; base /=10;}
int a=fa(t),b =fb(t),c=fc(t);
if(s[a]==0) {
s[a]=1;
node y=x;
y.data=a;
y.len=x.len+1;
y.road[x.len]=0;
q[bottom++]=y;
}
if(s[b]==0) {
s[b]=1;
node y=x;
y.data=b;
y.len=x.len+1;
y.road[x.len]=1;
q[bottom++]=y;
}
if(s[c]==0) {
s[c]=1;
node y=x;
y.data=c;
y.len=x.len+1;
y.road[x.len]=2;
q[bottom++]=y;
}
}
}
return 0;
}
参考代码:
0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复