wa 0pts 求调
查看原帖
wa 0pts 求调
1339022
123456Zhe楼主2025/6/26 19:56

#include <bits/stdc++.h>
#include <bits/extc++.h>
#define Yes cout<<"yes"<<endl
#define No cout<<"no"<<endl
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
#define ll long long
#define ull unsigned long long
#define inf 0x3f3f3f3f
#define for_range(a,b) for(int i=a;i<=b;i++)
#define io_opt ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pii pair<int,int>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
ll n,goal=123804765,s[4][4];
int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
map<ll,ll>dis,vis;
int main() {
    io_opt;
    cin>>n;
    queue<ll>q;
    if(n==goal){
        cout<<0<<endl;
        return 0;
    }
    vis[n]=1;
    vis[goal]=2;
    dis[n]=0;
    dis[goal]=0;
    q.push(n);
    q.push(goal);
    while(!q.empty()){ 
        ll cur=q.front();
        ll nw=cur;
        q.pop();
        int xx,yy;
        for(int i=3;i>=1;i--){
            for(int j=3;j>=1;j--){
                s[i][j]=nw%10;
                nw/=10;
                if(!s[i][j]){
                    xx=i;
                    yy=j;
                }
            }
        }
        for_range(0,3){
            int nx=xx+dx[i],ny=yy+dy[i];
            if(nx<1||nx>3||ny<1||ny>3){
                continue;
            }
            if(vis[nw]==vis[cur]){
                swap(s[xx][yy],s[nx][ny]);
                continue;
            }
            if(vis[nw]+vis[cur]==3){
                cout<<dis[cur]+dis[nw]+1<<endl;
                return 0;
            }
            dis[nw]=dis[cur]+1;
            vis[nw]=vis[cur];
            q.push(nw);
            swap(s[xx][yy],s[nx][ny]);
        }
    }
    return 0;
}

2025/6/26 19:56
加载中...