社区讨论

求助10分,必关,码粪良好

P1379八数码难题参与者 2已保存回复 14

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
13 条
当前快照
1 份
快照标识符
@mm7gt8y9
此快照首次捕获于
2026/03/01 16:05
上周
此快照最后确认于
2026/03/04 10:10
6 天前
查看原帖
样例输出0
帮我改正确必关
CPP
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5,t=123804765,ch[4]={-3,-1,1,3};
int s;
queue<int>q;
map<int,int>mp;
signed main(){
    cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);
    cin>>s;
    mp[s]=0;q.push(s);
    while(!q.empty()){
        int nw=q.front();
        q.pop();
        int op=nw,x;
        for(int i=9;i>=1;i--){
            if(op%10==0){
                x=i;
                break;
            }
            op/=10;
        }
        for(int i=0;i<4;i++){
            int xx=x+ch[i];
            if(xx<1||xx>9)continue;
            int val=nw/(int)(pow(10,xx-1))%10;
            int v=nw+pow(10,x-1)*val-pow(10,xx-1)*val;
            if(mp.count(v)==0){
                mp[v]=mp[nw]+1;
                q.push(v);
            }
        }
    }
    cout<<mp[t];
    return 0;
}

回复

14 条回复,欢迎继续交流。

正在加载回复...