ac代码:
#include<iostream>
#include<map>
#include<queue>
using namespace std;
map<string,int>mp1,mp2;
map<string,string>mp3;
queue<string>q;
string str,res="12345678";
void pri(string str){if(res==str) return ;pri(mp3[str]);
// printf("%d",mp2[str]);printf("%c",mp2[str]+64);
}
void ispush(string s,string tmp,int flag){if(mp1[s]) return;mp1[s]=mp1[tmp]+1;mp2[s]=flag;mp3[s]=tmp;q.push(s);}
int main(){for(int i=0;i<8;i++){char c; scanf("%c",&c);getchar();// str[i]=c;str.push_back(c);}// cout<<"str[0]="<<str[0]<<endl;// cout<<"str="<<str<<endl;q.push(res),mp1[res]=1;// int k=10;while(1){string tmp=q.front(); q.pop();// cout<<tmp<<"**"<<str<<endl;if(tmp==str){ break;}string s=tmp;for(int j=0;j<=3;j++)swap(s[j],s[7-j]);ispush(s,tmp,1);for(int j=0;j<=2;j++) s[j+1]=tmp[j];for(int j=7;j>=5;j--) s[j-1]=tmp[j];s[0]=tmp[3], s[7]=tmp[4];ispush(s,tmp,2);s=tmp; s[1]=tmp[6],s[2]=tmp[1];s[5]=tmp[2],s[6]=tmp[5];ispush(s,tmp,3);}printf("%d\n",mp1[str]-1);pri(str);// while(res!=str){// printf("%d",mp2[str]);// str=mp3[str];// }return 0;
}
1:一个一个字符以 str[i]='~' 的形式赋值时,str[i]有值,str没有(无语了 debug好久才找出来)
得用 str.push_back(~)
2.不要用强制转换,直接加上偏移量即可: