P2730 魔板 (写了巨久..有一些数字,字符,字符串之间的转换规则)

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.不要用强制转换,直接加上偏移量即可:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值