1 條題解

  • 1
    @ 2025-7-9 8:18:32

    用字符串存储状态,用三个map存储到达这个状态的操作数,如何操作和是否到达过。

    map <string,int> dis;
    map <string,int> vis;
    map <string,string> ans;
    

    手打三种(A,B,C)操作

    string A(string s){
    	string temp="";
    	for(int i=0;i<8;i++){
    		temp+=s[i];
    	}
    	for(int i=0;i<8;i++){
    		temp[i]=s[7-i];
    	}
    	return temp;
    }
    string B(string s){
    	string t="";
    	for(int i=0;i<8;i++){
    		t+=s[i];
    	}
    	t[0]=s[3],t[1]=s[0],t[2]=s[1],t[3]=s[2],t[4]=s[5],t[5]=s[6],t[6]=s[7],t[7]=s[4];
    	return t;
    }
    string C(string s){
    	string t="";
    	for(int i=0;i<8;i++){
    		t+=s[i];
    	}
    	t[1]=s[6],t[2]=s[1],t[3]=s[3],t[4]=s[4],t[5]=s[2],t[6]=s[5],t[7]=s[7];
    	return t;
    }
    

    然后用bfs寻找答案:

    void bfs(){
    	queue <string> q;
    	q.push(s);
    	while(q.size()){
    		string now=q.front();
    		if(now==s1){
    			cout<<dis[now]<<"\n"<<ans[now];
    			exit(0);
    		}
    		q.pop();
    		string a,b,c;
    		a=A(now);
    		b=B(now);
    		c=C(now);
    		if(!vis[a]){
    			q.push(a);
    			ans[a]=ans[now]+'A';
    		}
    		if(!vis[b]){
    			q.push(b);
    			ans[b]=ans[now]+'B';
    		}
    		if(!vis[c]){
    			q.push(c); 
    			ans[c]=ans[now]+'C';
    		}
    		dis[a]=dis[now]+1;
    		vis[a]=1;
    		dis[b]=dis[now]+1;
    		vis[b]=1;
    		dis[c]=dis[now]+1;
    		vis[c]=1;
    	}
    }
    
    • 1

    資訊

    ID
    17
    時間
    1000ms
    記憶體
    256MiB
    難度
    4
    標籤
    遞交數
    21
    已透過
    12
    上傳者