一些存档。。。

<pre name="code" class="cpp">#include <iostream>  
#include <stdio.h>  
#include <string.h>  
#include <algorithm>  
#include <queue>  
#include <map>  
#include <vector>  
#include <stack>
#include <set>  
#include <string>  
#include <math.h>  
using namespace std;  


const int NUM=7;
//0~7的阶乘 
int factory[NUM]={0,1,2,6,24,120,720};

//康托展开 
int Cantor(int perm[]){
	int i,j,counted;
	int result=0;
	for(i=0;i<NUM;++i){
		counted=0;
		for(j=i+1;j<NUM;++j)
			if(perm[i]>perm[j])++counted;
		result=result+counted*factory[NUM-i-1];
	}
	return result;
}

//3进制转换为10进制 
int _32dec(int ori[]){
	int res=0;
	for(int i=0;i<NUM;i++){
		res*=3;
		res+=ori[i];
	}
	return res;
}

/*
 *存储除了DBL块以外的7个块的位置和方向信息 
 * 1:UBR 
 * 2:UFR
 * 3:UFL
 * 4:UBL
 * 5:DBR
 * 6:DFR
 * 7:DFL
 * 
 */
struct Node{
	int permutation[7];
	int orientation[7];
	
	void U(){
		int t=permutation[3];
		permutation[3]=permutation[2];
		permutation[2]=permutation[1];
		permutation[1]=permutation[0];
		permutation[0]=t;
		//
		t=orientation[3];
		orientation[3]=orientation[2];
		orientation[2]=orientation[1];
		orientation[1]=orientation[0];
		orientation[0]=t;
	}
	
	void R(){
		int t=permutation[0];
		permutation[0]=permutation[1];
		permutation[1]=permutation[5];
		permutation[5]=permutation[4];
		permutation[4]=t;
		//o
		t=orientation[0];
		orientation[0]=orientation[1];
		orientation[1]=orientation[5];
		orientation[5]=orientation[4];
		orientation[4]=t;
		orientation[0]+=1; orientation[0]%=3;
		orientation[1]+=2; orientation[1]%=3;
		orientation[5]+=1; orientation[5]%=3;
		orientation[4]+=2; orientation[4]%=3;
	}
	
	void F(){
		int t=permutation[1];
		permutation[1]=permutation[2];
		permutation[2]=permutation[6];
		permutation[6]=permutation[5];
		permutation[5]=t;
		//o
		t=orientation[1];
		orientation[1]=orientation[2];
		orientation[2]=orientation[6];
		orientation[6]=orientation[5];
		orientation[5]=t;
		orientation[1]+=1; orientation[1]%=3;
		orientation[2]+=2; orientation[2]%=3;
		orientation[6]+=1; orientation[6]%=3;
		orientation[5]+=2; orientation[5]%=3;
	}
	
	void D(){
		U(); U(); U();
	}
	
	void L(){
		R(); R(); R();
	}
	
	void B(){
		F(); F(); F();
	}
};

//哈希函数
//返回的最小值是0
//返回的最大值小于1.2e7
// 
int _hash(Node& nd){
	int hash_p=Cantor(nd.permutation);
	int hash_o=_32dec(nd.orientation);
	//2187是三进制2222222 
	return hash_p*2187+hash_o;
}

bool vis[12000000];
char premove[12000000];
int prestate[12000000];

queue<Node> que;
void BFS(Node s){
	while(!que.empty())que.pop();
	
	memset(vis,0,sizeof(vis));
	que.push(s);
	vis[_hash(s)]=1;
	while(!que.empty()){
		Node cur=que.front(); que.pop();
		int hashcur=_hash(cur);
		
		Node t;
		int h;
		//cout<<hashcur<<endl;
		for(int k=0;k<3;k++){
			t=cur;
			
			if(k==0)t.U();
			if(k==1)t.D();
			if(k==2)t.R();
			if(k==3)t.L();
			if(k==4)t.F();
			if(k==5)t.B();

			h=_hash(t);
			if(!vis[h]){
				que.push(t);
				vis[h]=1;
				if(k==0)premove[h]='U';
				if(k==1)premove[h]='D';
				if(k==2)premove[h]='R';
				if(k==3)premove[h]='L';
				if(k==4)premove[h]='F';
				if(k==5)premove[h]='B';
				prestate[h]=hashcur;
			}
			if(h==0)break;
			
		}
		if(h==0)break;
	}
}


int main(){
	int n;
	while(cin>>n){
		Node solved_state;
		for(int i=0;i<7;i++){
			solved_state.orientation[i]=0;
			solved_state.permutation[i]=i;
		}
	
		char str[20];
		scanf("%s",str);
		
		int sz=strlen(str);
		
		for(int i=0;i<sz;i++){
			if(str[i]=='U'){
				solved_state.U();
			}
			if(str[i]=='F'){
				solved_state.F();
			}
			if(str[i]=='R'){
				solved_state.R();
			}
		}
		
		
		BFS(solved_state);
		cout<<"finished"<<endl;
		stack<char> sta;
		int tmp=0;
		sta.push(premove[tmp]);
		tmp=prestate[tmp];
		while(tmp){
			sta.push(premove[tmp]);
			tmp=prestate[tmp];
		}
		while(!sta.empty()){
			cout<<sta.top()<<" ";
			sta.pop();
		}cout<<endl;
	}
	
	return 0;
} 



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值