結果

問題 No.283 スライドパズルと魔方陣
ユーザー fumofumofunifumofumofuni
提出日時 2022-09-01 23:44:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 4,661 bytes
コンパイル時間 2,710 ms
コンパイル使用メモリ 206,580 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-26 23:41:42
合計ジャッジ時間 7,563 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 RE -
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 3 ms
6,940 KB
testcase_20 RE -
testcase_21 AC 7 ms
6,944 KB
testcase_22 AC 6 ms
6,940 KB
testcase_23 AC 8 ms
6,944 KB
testcase_24 AC 6 ms
6,944 KB
testcase_25 AC 6 ms
6,940 KB
testcase_26 AC 8 ms
6,940 KB
testcase_27 AC 30 ms
6,944 KB
testcase_28 RE -
testcase_29 AC 41 ms
6,944 KB
testcase_30 AC 32 ms
6,940 KB
testcase_31 AC 41 ms
6,940 KB
testcase_32 AC 32 ms
6,940 KB
testcase_33 AC 42 ms
6,940 KB
testcase_34 AC 31 ms
6,944 KB
testcase_35 AC 41 ms
6,944 KB
testcase_36 AC 32 ms
6,940 KB
testcase_37 AC 42 ms
6,940 KB
testcase_38 AC 32 ms
6,940 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(){
    /*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)swap(ans[i][0],ans[i][1]);
        ll h=inversion(ans);
        if(f%2==g%2&&f%2==h%2)cout << n << endl;
    }
    return 0;*/
    ll n;cin >> n;
    if(n==2)cout << "impossible" << endl,exit(0);
    cout << "possible" << endl;
    if(n==1){
        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){
        rep(i,n){
            rep(j,n){
                if(j)cout <<" ";
                cout << ans[i][j];
            }
            cout << endl;
        }
        return 0;
    }
    rep(i,n)rev(all(ans[i]));
    f=inversion(ans);
    if(ret%2==f%2){
        rep(i,n){
            rep(j,n){
                if(j)cout <<" ";
                cout << ans[i][j];
            }
            cout << endl;
        }
        return 0;
    }
    rep(i,n)swap(ans[i][0],ans[i][1]);
    f=inversion(ans);
    if(ret%2==f%2){
        rep(i,n){
            rep(j,n){
                if(j)cout <<" ";
                cout << ans[i][j];
            }
            cout << endl;
        }
    }
    assert(n>=INF);
}
0