sicily 1515. 魔版C[Special judge]

来源:互联网 发布:外文期刊数据库 编辑:程序博客网 时间:2024/05/16 17:43
Description

魔板由8个大小相同方块组成,分别用涂上不同颜色,用1到8的数字表示。
其初始状态是
1 2 3 4
5 6 7 8
对魔板可进行三种基本操作:
A操作(左右两列互换):
3 4 1 2
7 8 5 6
B操作(每次以行循环左移一个):
2 3 4 1
6 7 8 5
C操作(中间四小块逆时针转一格):
1 3 7 4
5 2 6 8
用上述三种基本操作,可将任一种状态转换成另一种状态。

Input

输入包括多个要求解的魔板,每个魔板用三行描述。
第一行步数N(N<=100),表示最多容许的步数。
第二、第三行表示目标状态,按照魔板的形状,颜色用1到8的表示。
当N等于-1的时候,表示输入结束。

Output

对于每一个要求解的魔板,输出一行。
首先是一个整数M,表示你找到解答所需要的步数。接着若干个空格之后,从第一步开始按顺序给出M步操作(每一步是A、B或C),相邻两个操作之间没有任何空格。
注意:如果不能达到,则M输出-1即可。

Sample Input
 Copy sample input to clipboard
44 1 2 38 5 6 721 2 3 45 6 7 801 3 7 45 2 6 8-1
Sample Output
2 AB0-1

我的解法:

//类似sicily1150&1151,只要把操作A、B和C的操作改过来就OK了

#include <iostream>
#include <vector>
#include <string>


using namespace std;


int fact[8]={1,1,2,6,24,120,720,5040};  //定义1到7的阶乘


struct board
{
int num;         //记录步数
string id;       //记录顺序
bool visit;      //记录是否已存在
}step[40321];     //把每种排序用康托展开编码并存在board数组中        


int encode(int n)      //康托展开式编码
{
int temp[8];
for(int k=7;k>=0;k--)
{
temp[k]=n%10;
n=n/10;
}
int output=0;
for(int i=0;i<8;i++)
{
int tempcount = 0;
for(int j=i+1;j<8;j++)
if(temp[i]>temp[j])
tempcount++;
output+=tempcount*fact[7-i];
}
return output;
}


int operatingA(int n)          
{
int temp=0;
temp += n/10000000*100000;
temp += n/1000000%10*10000;
temp += n/100000%10*10000000;
temp += n/10000%10*1000000;
temp += n%10000/1000*10;
    temp += n%1000/100;
temp += n%100/10*1000;
temp += n%10*100;
return temp;
}


int operatingB(int n)
{
int temp=0;
temp += n/10000000*10000;
temp += n/1000000%10*10000000;
temp += n/100000%10*1000000;
temp += n/10000%10*100000;
temp += n%10000/1000;
    temp += n%1000/100*1000;
temp += n%100/10*100;
temp += n%10*10;
return temp;
}


int operatingC(int n)
{
int temp=0;
temp += n/10000000*10000000;
temp += n/1000000%10*100;
temp += n/100000%10*1000000;
temp += n/10000%10*10000;
temp += n%10000/1000*1000;
    temp += n%1000/100*10;
temp += n%100/10*100000;
temp += n%10;
    return temp;  
}


void clear(board step[])    //将board型数组清零
{
for(int i=0;i<40321;i++)
{
step[i].id ="";
step[i].num =0;
step[i].visit =false;
}
}




int main()
{
int source = 12345678;
int gen,a,aen,b,ben,c,cen,maxstep,ss;
while(cin>>maxstep)
{
if(maxstep==-1)
return 0;
int ini=source;
int g=0;
int goal[8];
clear(step);
for(int i=0;i<8;i++)    //读取目标board数据     
{
cin>>goal[i];
g=g*10+goal[i];
}
gen=encode(g);          
vector<int> q;          //用深度遍历算法BFS,向量q用来记录遍历状态
q.push_back(ini);      
ss=encode(ini);
step[ss].visit=true;
bool ans=true;
while(q[0] !=g)
{
if(step[encode(q[0])].num>maxstep)  
{
cout<<"-1"<<endl;
ans = false;
break;
}
a=operatingA(q[0]);
aen=encode(a);
b=operatingB(q[0]);
ben=encode(b);
c=operatingC(q[0]);
cen=encode(c);
ss=encode(q[0]);


if(!step[aen].visit)
{
q.push_back(a);
step[aen].num=step[ss].num+1;
step[aen].id=step[ss].id+"A";
step[aen].visit=true;
}
if(!step[ben].visit)
{
q.push_back(b);
step[ben].num=step[ss].num+1;
step[ben].id=step[ss].id+"B";
step[ben].visit=true;
}
if(!step[cen].visit)
{
q.push_back(c);
step[cen].num=step[ss].num+1;
step[cen].id=step[ss].id+"C";
step[cen].visit=true;
}
q.erase(q.begin());
}
if(ans)
cout<<step[gen].num<<"   "<<step[gen].id<<endl;
}
return 0;
}




原创粉丝点击