B16 BFS 魔板

视频链接:B16 BFS 魔板_哔哩哔哩_bilibili

 

Luogu P2730 [USACO3.2]魔板 Magic Squares

 

 

 

#include<bits/stdc++.h>
using namespace std;

string es; //目标串
queue<string> q; //<状态串>
map<string,string> mp; //<状态串,操作串>

void A(string s){
  string t=s;
  s[0]=t[7],s[7]=t[0],s[1]=t[6],s[6]=t[1];
  s[2]=t[5],s[5]=t[2],s[3]=t[4],s[4]=t[3];
  if(!mp.count(s)) q.push(s),mp[s]=mp[t]+'A';
}
void B(string s){
  string t=s;
  s[0]=t[3],s[1]=t[0],s[2]=t[1],s[3]=t[2];
  s[7]=t[4],s[6]=t[7],s[5]=t[6],s[4]=t[5];
  if(!mp.count(s)) q.push(s),mp[s]=mp[t]+'B';
}
void C(string s){
  string t=s;
  s[1]=t[6],s[2]=t[1],s[5]=t[2],s[6]=t[5];
  if(!mp.count(s)) q.push(s),mp[s]=mp[t]+'C';
}
void bfs(string str){
  q.push(str);
  while(q.size()){
    auto s=q.front(); q.pop();
    A(s);B(s);C(s);
    if(mp.count(es)){
      cout<<mp[es].size()<<endl<<mp[es];
      return;
    }
  }
}
int main(){
  char c;
  for(int i=0;i<8;i++) cin>>c,es+=c;
  bfs("12345678");
}

 

posted @ 2023-06-04 17:29  董晓  阅读(747)  评论(0)    收藏  举报