sicily 1150&1151 魔板[Special judge]
2012-10-22 16:13
465 查看
Description
魔板由8个大小相同方块组成,分别用涂上不同颜色,用1到8的数字表示。
其初始状态是
1 2 3 4
8 7 6 5
对魔板可进行三种基本操作:
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
用上述三种基本操作,可将任一种状态装换成另一种状态。
Input
输入包括多个要求解的魔板,每个魔板用三行描述。
第一行步数N,表示最多容许的步数。
第二、第三行表示目标状态,按照魔板的形状,颜色用1到8的表示。
当N等于-1的时候,表示输入结束。
Output
对于每一个要求解的魔板,输出一行。
首先是一个整数M,表示你找到解答所需要的步数。接着若干个空格之后,从第一步开始按顺序给出M步操作(每一步是A、B或C),相邻两个操作之间没有任何空格。
注意:如果不能达到,则M输出-1即可。
Sample Input
Copy sample input to clipboard
Sample Output
#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)
{
return n/10000+n%10000*10000;
}
int operatingB(int n)
{
int temp = 0;
temp += n%1000/10;
temp += n%10000/1000*100;
temp += n%10*1000;
temp += n%1000000/100000*10000;
temp += n/1000000*100000;
temp += n%100000/10000*10000000;
return temp;
}
int operatingC(int n)
{
int temp = 0;
temp += n%10;
temp += n%1000000/100000*10;
temp += n%100/10*100;
temp += n%100000/1000*1000;
temp += n%10000000/1000000*100000;
temp += n%1000/100*1000000;
temp += n/10000000*10000000;
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 = 12348765;
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;
}
魔板由8个大小相同方块组成,分别用涂上不同颜色,用1到8的数字表示。
其初始状态是
1 2 3 4
8 7 6 5
对魔板可进行三种基本操作:
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
用上述三种基本操作,可将任一种状态装换成另一种状态。
Input
输入包括多个要求解的魔板,每个魔板用三行描述。
第一行步数N,表示最多容许的步数。
第二、第三行表示目标状态,按照魔板的形状,颜色用1到8的表示。
当N等于-1的时候,表示输入结束。
Output
对于每一个要求解的魔板,输出一行。
首先是一个整数M,表示你找到解答所需要的步数。接着若干个空格之后,从第一步开始按顺序给出M步操作(每一步是A、B或C),相邻两个操作之间没有任何空格。
注意:如果不能达到,则M输出-1即可。
Sample Input
Copy sample input to clipboard
4 5 8 7 6 4 1 2 3 3 8 7 6 5 1 2 3 4 -1
Sample Output
2 AB 1 A 评分:M超过N或者给出的操作不正确均不能得分。
我的解法:
#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)
{
return n/10000+n%10000*10000;
}
int operatingB(int n)
{
int temp = 0;
temp += n%1000/10;
temp += n%10000/1000*100;
temp += n%10*1000;
temp += n%1000000/100000*10000;
temp += n/1000000*100000;
temp += n%100000/10000*10000000;
return temp;
}
int operatingC(int n)
{
int temp = 0;
temp += n%10;
temp += n%1000000/100000*10;
temp += n%100/10*100;
temp += n%100000/1000*1000;
temp += n%10000000/1000000*100000;
temp += n%1000/100*1000000;
temp += n/10000000*10000000;
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 = 12348765;
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;
}
相关文章推荐
- sicily 1151. 魔板[Special judge]
- sicily 1150 简单魔板 & 1151 魔板 bfs
- Sicily 1150 简单魔板 && 1151 魔板 (BFS深度优先搜索+康托展开状态压缩)
- [sicily]1151. 魔板[Special judge]
- [sicily]1151. 魔板[Special judge]
- sicily 1150. 简单魔板 & 1151. 魔板
- Sicily 1151. 魔板
- Sicily 1151. 魔板
- Sicily 1151. 魔板解题报告
- Sicily 1153 马的周游问题[Special judge] && sicily 1152
- Sicily 1151. 魔板
- Sicily 1151. 魔板
- Sicily 1151. 魔板 解题报告
- Sicily 1151. 魔板[Speical judge]
- sicily 1150 简单魔板 sicily 1151 魔板 sicily 1515 魔板C
- [Sicily 1150 1151 1515 魔板] BFS+判重(康托展开)
- sicily_1150 & sicily_1151
- Sicily1151:魔板搜索及优化
- sicily1150 & 魔板
- sicily 1151 魔板