hdu1430 magic board (Kang expanded to open bfs preprocessing)

Magic board

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4050    Accepted Submission(s): 951


Problem Description
Shortly after the Rubik's Cube took the world by storm, Mr. Rubik invented its simplified version, the Magic Board. The magic board consists of 8 squares of the same size, each of which has a different color, which can be represented by numbers 1-8. The state of the magic board at any time can be represented by the color sequence of the blocks: starting from the upper left corner of the magic board, write down the color code of each block in a clockwise direction, and the obtained number sequence can represent the state of the magic board at this time. For example, the sequence (1,2,3,4,5,6,7,8) indicates that the state of the magic board is:

1 2 3 4
8 7 6 5

For the magic board, three different operations can be applied, and the specific operation methods are as follows :

A: The upper and lower lines are interchanged, as shown in the figure above, it can be transformed into the state 87654321
B: Each row is shifted to the right by one grid at the same time, and it can be transformed into 41236785
as shown in the above figure. 17245368 will

give you the initial state and target state of the magic board. Please give the transformation steps with the least number of transformations from the initial state to the target state. If there are multiple transformation schemes, take the one with the smallest lexicographical order.
 

Input
Each set of test data includes two lines, which represent the initial state and the target state of the magic board respectively.
 

Output
For each set of test data, output the transformation steps that satisfy the meaning of the question.
 

Sample Input
 
  
12345678 17245368 12345678 82754631
 

Sample Output
 
  
C AC


topic portal

Idea: I understand the Cantor expansion first and then do this question. I think I can use the Cantor expansion to get an independent label for each state, so that a certain state can be found quickly, so I use 12345678 as the initial state first. Then record the contor value of each state. Here I use the method of writing the function in the structure, which is convenient and concise (learned from a big man on the Internet), and then it is difficult to think that the initial state and the final state of each input are The states are not all 12345678. How to match the input to the state in bfs?

We need to replace it.

Replacement: The original state is "12345678", for example, the initial state is "45781236" and the target state is "78451326"  

The first bit '7' of the target state is the third bit in the initial state, and the third bit of the original state is '3', so the first bit of the target state should be converted to '3', (that is, the input value One-to-one correspondence with 12345678) Just like this, the final target state is converted to 34125768, and finally the Canto value corresponding to 34125768 is found, and the output is good. 

In terms of lexicographical order, each bfs is in the order of ABC, so the steps of each state must be in the smallest lexicographical order.

See the code for details.

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
typedef long long ll;
const int N=40320+10;
int fact[9]= {1,1,2,6,24,120,720,5040,40320};
struct info{
	int arr[8];
	int val;//The serial number of the calculated Cantor expansion
	string step;
	void contor(){//Write Cantor expansion in the structure
		val=0;
		for(int i=0;i<8;i++){
			int t=0;
			for(int j=i+1;j<8;j++){
				if(arr[j]<arr[i])t++;
			}
			val+=t*fact[8-i-1];
		}
		val++;
	}
	void change_a(){
		reverse(arr,arr+8);
		step+='A';
	}
	void change_b(){
        int temp=arr[3];
        for (int i=3; i>0; --i)
            arr[i]=arr[i-1];
        arr[0]=temp;
        temp=arr[4];
        for (int i=4; i<7; ++i)
            arr[i]=arr[i+1];
        arr[7]=temp;
        step+="B";
    }
	void change_c(){
		int temp=arr[1];
		arr[1]=arr[6];
		arr[6]=arr[5];
		arr[5]=arr[2];
		arr[2]=temp;
		step+='C';
	}
};
int force[N];
string ans[N];//Record the operation of each state
int re[8];
info s,t;
void bfs(){
	queue<info>q;
	vis [s.val] = 1;
	q.push(s);
	info now,v;
	while(!q.empty()){
		now=q.front();
		q.pop();
		v=now;
		int len=v.step.length();
		if(len<1||v.step[len-1]!='A'){//Remove the case of AA
			v.change_a();
			v.contor ();
			if(!vis[v.val]){
				vis [v.val] = 1;
				ans[v.val]=v.step;
				q.push(v);
			}
		}
		v=now;
		if(len<3||v.step[len-1]!='B'||v.step[len-2]!='B'||v.step[len-3]!='B' ){//Remove the case of BBBB
			v.change_b();
			v.contor ();
			if(!vis[v.val]){
				vis [v.val] = 1;
				ans[v.val]=v.step;
				q.push(v);
			}
		}
		v=now;
		if(len<3||v.step[len-1]!='C'||v.step[len-2]!='C'||v.step[len-3]!='C' ){//Remove the case of CCCC
			v.change_c();
			v.contor ();
			if(!vis[v.val]){
				vis [v.val] = 1;
				ans[v.val]=v.step;
				q.push(v);
			}
		}
	}
}
int main(){
	for(int i=0;i<8;i++){
		s.arr[i]=i+1;
	}//Initialize 12345678
	s.contor ();
	bfs();
	char ss[10];
	while(scanf("%s",ss)!=EOF){
		for(int i=0;i<8;i++){
		re[ss[i]-'0']=i+1;
		}//One-to-one correspondence between the characters of the input initial state and 12345678 (the initial state in bfs)  
		scanf("%s",ss);
		for(int i=0;i<8;i++){
			t.arr[i]=re[ss[i]-'0'];//Find the corresponding value of the current target state in bfs
		}
		t.contor ();
		printf("%s\n",ans[t.val].c_str());
	}
}


 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325908333&siteId=291194637