[SSL] 1692 Magic Board (BFS-HASH)

[SSL] 1692 Magic Board (BFS-HASH)

Time Limit:1000MS
Memory Limit:65536K

Description

After successfully inventing the Rubik's Cube, Mr. Rubick invented its two-dimensional version, called the Magic Board. This is a magic board with 8 grids of the same size:
1 2 3 4
8 7 6 5
  We know that every square on the magic board has a color. These 8 colors are represented by the first 8 positive integers. A sequence of colors can be used to represent a state of the magic board. It is stipulated that starting from the upper left corner of the magic board, integers are taken out in a clockwise direction to form a color sequence. For the state of the magic board in the figure above, we use the sequence (1, 2, 3, 4, 5, 6, 7, 8) to represent. This is the basic state.
  Here are three basic operations, which are represented by capital letters "A", "B", and "C" (the state of the magic board can be changed through these operations):
"A": swap the upper and lower lines;
"B": change Insert the rightmost column into the leftmost column;
"C": The central four grids of the magic board rotate clockwise.
  The following is a demonstration of the operation of the basic states:
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
  For each possible state, these three Basic operations can be used.
  You need to program and calculate the conversion from the basic state to the target state with the least basic operations, and output the basic operation sequence.

Input

There is only one line, including 8 integers, separated by spaces (the integers are in the range of 1-8), indicating the target state.

Output

Line 1: Contains an integer that represents the length of the shortest operation sequence.
Line 2: The earliest sequence of operations in the lexicographic order, represented by a character string, each line outputs 60 characters except the last line.

Sample Input

2 6 8 4 5 7 3 1 

Sample Output

7 
BCABCCB

Ideas

Width first search, use hash to judge heavy, save state with string.

Code

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
string a[100010],b,hash[100010];
int an,sum=0,bn,qz[100010],gc[100010],bs[100010],m=100010,p=100007;
int rule[3][8]={
    
    {
    
    7,6,5,4,3,2,1,0},{
    
    3,0,1,2,5,6,7,4},{
    
    0,6,1,3,4,2,5,7}};//三种基本操作 
char str[3][5]={
    
    "A","B","C"};
int HASH(string x)//hash函数 
{
    
    
	int i,ans=0;
	for(i=0;i<8;i++)
		ans=ans*10+x[i]-'0';
	return ans%p;
}
int locate(string x)//x定位 
{
    
    
	int i,w=HASH(x);
	for(i=0;i<m&&hash[(w+i)%m]!=""&&hash[(w+i)%m]!=x;i++);
	return (w+i)%m;
}
bool member(string x)//x是否在hash中 
{
    
    
	int w=locate(x);
	if(hash[w]==x)
		return 1;
	hash[w]=x;
	return 0;
}
void Print(int x)//递归输出方案 
{
    
    
	if(qz[x]!=1)
		Print(qz[x]);
//	printf("%d",gc[x]);
	printf("%s",str[gc[x]]);
	sum++;
//	cout<<str[gc[x]]<<endl<<a[x][0]<<a[x][1]<<a[x][2]<<a[x][3]<<endl<<a[x][7]<<a[x][6]<<a[x][5]<<a[x][4]<<endl<<endl;
	if(sum%60==0) 
		printf("\n");
	return;
}
void BFS()//宽度优先搜索 
{
    
    
	int l,r,i,j;
	for(l=r=1;l<=r;l++)
	{
    
    
		for(i=0;i<3;i++)//枚举每种方案 
		{
    
    
			r++;
			a[r]="";
			qz[r]=l;//前缀 
			gc[r]=i;//方案 
			bs[r]=bs[l]+1;//步数 
			for(j=0;j<8;j++)a[r]+=a[l][rule[i][j]];//变化 
			if(a[r]==b)//到目标 
			{
    
    
				printf("%d\n",bs[r]);
				Print(r);
				exit(0);
			}
			if(member(a[r]))//判重 
				r--;
		}
	}
	return;
}
void input()
{
    
    
	int i,x;
	memset(gc,0,sizeof(gc));
	for(i=1;i<=8;i++) 
	{
    
    
		scanf("%d",&x);
		b+=char(x+'0');
	}
	a[1]="12345678";
	if(b=="12345678")//特判 
	{
    
    
		printf("0");
		exit(0);
	}
	return;
}
int main()
{
    
    
	input();
	BFS();
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_46975572/article/details/112944591