結果

問題 No.283 スライドパズルと魔方陣
ユーザー fumofumofunifumofumofuni
提出日時 2022-09-02 00:28:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,790 bytes
コンパイル時間 2,831 ms
コンパイル使用メモリ 216,180 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-14 16:56:41
合計ジャッジ時間 7,187 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,820 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 2 ms
6,816 KB
testcase_04 AC 2 ms
6,820 KB
testcase_05 AC 1 ms
6,816 KB
testcase_06 AC 2 ms
6,816 KB
testcase_07 AC 2 ms
6,816 KB
testcase_08 WA -
testcase_09 AC 2 ms
6,820 KB
testcase_10 AC 2 ms
6,816 KB
testcase_11 AC 2 ms
6,820 KB
testcase_12 AC 2 ms
6,816 KB
testcase_13 AC 2 ms
6,816 KB
testcase_14 AC 2 ms
6,820 KB
testcase_15 AC 3 ms
6,816 KB
testcase_16 AC 2 ms
6,820 KB
testcase_17 AC 2 ms
6,820 KB
testcase_18 AC 2 ms
6,816 KB
testcase_19 AC 2 ms
6,816 KB
testcase_20 WA -
testcase_21 AC 8 ms
6,816 KB
testcase_22 AC 6 ms
6,820 KB
testcase_23 AC 9 ms
6,820 KB
testcase_24 AC 7 ms
6,816 KB
testcase_25 AC 7 ms
6,816 KB
testcase_26 AC 9 ms
6,816 KB
testcase_27 AC 33 ms
6,816 KB
testcase_28 WA -
testcase_29 AC 48 ms
6,820 KB
testcase_30 AC 36 ms
6,816 KB
testcase_31 AC 48 ms
6,820 KB
testcase_32 AC 36 ms
6,816 KB
testcase_33 AC 47 ms
6,820 KB
testcase_34 AC 36 ms
6,816 KB
testcase_35 AC 47 ms
6,816 KB
testcase_36 AC 35 ms
6,820 KB
testcase_37 AC 48 ms
6,820 KB
testcase_38 AC 35 ms
6,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
 ll MOD=1000000007;
 ll MOD9=998244353;
 int inf=1e9+10;
 ll INF=1e18;
 ll dy[8]={1,0,-1,0,1,1,-1,-1};
 ll dx[8]={0,1,0,-1,1,-1,1,-1};
template <typename T> inline bool chmax(T &a, T b) {
  return ((a < b) ? (a = b, true) : (false));
}
template <typename T> inline bool chmin(T &a, T b) {
  return ((a > b) ? (a = b, true) : (false));
}
vvl solve1(ll n){
  vvl mat(n,vl(n));
  ll now=1,r=0,c=n/2;
  while(now<=n*n){
    mat[r][c]=now;
    now++;
    r=(r-1+n)%n,c=(c+1+n)%n;
    if(mat[r][c]){
      r=(r+2+n)%n,c=(c-1+n)%n;
    }
  }
  return mat;
}
vvl solve2(ll n){
  vvl mat(n,vl(n));
  ll now=1;
  rep(i,n){
    rep(j,n){
      if(abs(i-j)%4==0||(i+j)%4==3)mat[i][j]=now;
      now++;
    }
  }
  now=1;
  per(i,n){
    per(j,n){
      if(!(abs(i-j)%4==0||(i+j)%4==3))mat[i][j]=now;
      now++;
    }
  }
  return mat;
}
vvl solve3(ll n){
  vvl pl=solve1(n/2);
  rep(i,n/2)rep(j,n/2)pl[i][j]=(pl[i][j]-1)*4;
  vvl lux(n/2,vl(n/2));
  rep(i,n/2)lux[n/2/2+1][i]=1;//u
  rep(j,n/2){
    for(int i=n/2/2+2;i<n/2;i++)lux[i][j]=2;//x;
  }
  swap(lux[n/2/2][n/2/2],lux[n/2/2+1][n/2/2]);
  /*rep(i,n/2){
    rep(j,n/2)cout << lux[i][j] <<" ";cout << endl;
  }*/
  vvl mat(n,vl(n));
  rep(i,n/2){
    rep(j,n/2){
      if(lux[i][j]==0){
        mat[i*2][j*2]=4;
        mat[i*2][j*2+1]=1;
        mat[i*2+1][j*2]=2;
        mat[i*2+1][j*2+1]=3;
      }
      if(lux[i][j]==1){
        mat[i*2][j*2]=1;
        mat[i*2][j*2+1]=4;
        mat[i*2+1][j*2]=2;
        mat[i*2+1][j*2+1]=3;
      }
      if(lux[i][j]==2){
        mat[i*2][j*2]=1;
        mat[i*2][j*2+1]=4;
        mat[i*2+1][j*2]=3;
        mat[i*2+1][j*2+1]=2;
      }
    }
  }
  rep(i,n/2){
    rep(j,n/2){
      mat[i*2][j*2]+=pl[i][j];
      mat[i*2][j*2+1]+=pl[i][j];
      mat[i*2+1][j*2]+=pl[i][j];
      mat[i*2+1][j*2+1]+=pl[i][j];
    }
  }
  return mat;
}
vvl solve(ll n){
    vvl mat;
    if(n%2)mat=solve1(n);
    else if(n%4==0)mat=solve2(n);
    else mat=solve3(n);
    return mat;
}
ll inversion(vvl mat){
    ll n=mat.size();
    rep(i,n){
        rep(j,n){
            if(mat[i][j]==n*n){
                if(i+1<n)swap(mat[i][j],mat[i+1][j]);
                else if(j+1<n)swap(mat[i][j],mat[i][j+1]);
            }
        }
    }
    /*rep(i,n){
        rep(j,n)cout << mat[i][j] <<" ";
        cout << endl;
    }*/
    ll ret=0;
    rep(i,n){
        rep(j,n){
            rep(k,n){
                rep(l,n){
                    if(i*n+j<k*n+l&&mat[i][j]>mat[k][l])ret++;
                }
            }
        }
    }
    return ret;
}
int main(){
    /*vl ep={{11,24,7,20,3},{4,12,25,8,16},{17,5,13,21,9},{10,18,1,14,22},{23,6,19,2,15}};
    cout << inversion(ep) << endl;
    for(ll n=3;n<=50;n++){
        vvl ans=solve(n);
        ll f=inversion(ans);
        rep(i,n)rev(all(ans[i]));
        ll g=inversion(ans);
        /*rep(i,n){
            ll left=0,right=n-1;
            while(left<right){
                swap(ans[left][i],ans[right][i]);
                left++;right--;
            }
        }
        ll h=inversion(ans);
        rep(i,n)rev(all(ans[i]));
        ll k=inversion(ans);
        rep(i,n)swap(ans[i][0],ans[i][n-1]);
        rep(i,n)swap(ans[i][1],ans[i][n-2]);
        rep(i,n){
            swap(ans[0][i],ans[n-1][i]);
        }
        rep(i,n){
            swap(ans[1][i],ans[n-2][i]);
        }
        ll h=inversion(ans);
        if(f%2==g%2&&f%2==h%2){
            cout << n << endl;
            cout << f%2 <<" " << g%2 <<" " << h%2 << endl;
        }
    }
    */
    ll n;cin >> n;
    if(n==2)cout << "impossible" << endl,exit(0);
    if(n==1){
        cout << "possible" << endl;
        cout << 1 << endl;return 0;
    }
    vvl mat(n,vl(n));
    rep(i,n){
        rep(j,n){
            cin >> mat[i][j];
            if(mat[i][j]==0)mat[i][j]=n*n;
        }
    }
    ll ret=inversion(mat);
    vvl ans=solve(n);
    /*rep(i,n){
        rep(j,n)cout << mat[i][j] <<" ";cout << endl;
    }
    rep(i,n){
        rep(j,n)cout << ans[i][j] <<" ";cout << endl;
    }*/
    ll f=inversion(ans);
    if(ret%2==f%2){
        cout << "possible" << endl;
        rep(i,n){
            rep(j,n){
                if(j)cout <<" ";
                cout << ans[i][j];
            }
            cout << endl;
        }
        return 0;
    }
    rep(i,n)rev(all(ans[i]));
    ll g=inversion(ans);
    if(ret%2==g%2){
        cout << "possible" << endl;
        rep(i,n){
            rep(j,n){
                if(j)cout <<" ";
                cout << ans[i][j];
            }
            cout << endl;
        }
        return 0;
    }
    cout << "possible" << endl;
    random_device rnd;mt19937 mt(rnd()); 
    ll csum=accumulate(all(ans[0]),0LL);
    while(1){
        shuffle(all(ans),mt);
        ll ret=0;
        rep(i,n){
            ret+=ans[i][i];
        }
        if(ret!=csum)continue;
        ret=0;
        rep(i,n){
            ret+=ans[n-1-i][i];
        }
        if(ret!=csum)continue;
        ll h=inversion(ans);
        cout << h << endl;
        if(ret%2==h%2){
        rep(i,n){
            rep(j,n){
                if(j)cout <<" ";
                cout << ans[i][j];
            }
            cout << endl;
            }
            return 0;
        }
    }
}
0