結果

問題 No.217 魔方陣を作ろう
ユーザー snukesnuke
提出日時 2015-05-26 22:36:41
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,140 bytes
コンパイル時間 722 ms
コンパイル使用メモリ 83,432 KB
実行使用メモリ 7,504 KB
最終ジャッジ日時 2023-09-20 14:46:27
合計ジャッジ時間 36,296 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 1 ms
4,376 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <string.h>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
#include <iostream>
#include <sstream>
#include <numeric>
#include <cctype>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rrep(i,n) for(int i = 1; i <= n; ++i)
#define drep(i,n) for(int i = n-1; i >= 0; --i)
#define gep(i,g,j) for(int i = g.head[j]; i != -1; i = g.e[i].next)
#define each(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define rng(a) a.begin(),a.end()
#define maxs(x,y) x = max(x,y)
#define mins(x,y) x = min(x,y)
#define pb push_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcount
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
inline int in() { int x; scanf("%d",&x); return x;}
inline void priv(vi a) { rep(i,sz(a)) printf("%d%c",a[i],i==sz(a)-1?'\n':' ');}

const int MX = 10000005, INF = 1000010000;
const ll LINF = 1000000000000000000ll;
const double eps = 1e-10;

int n, s;
vvi a;

int calc() {
  int res = 0;
  rep(i,n) {
    int x = 0;
    rep(j,n) x += a[i][j];
    res += abs(x-s);
    x = 0;
    rep(j,n) x += a[j][i];
    res += abs(x-s);
  }
  {
    int x = 0;
    rep(i,n) x += a[i][i];
    res += abs(x-s);
  }
  {
    int x = 0;
    rep(i,n) x += a[n-1-i][i];
    res += abs(x-s);
  }
  return res;
}

int main(){
  snuke;
  scanf("%d",&n);
  a = vvi(n,vi(n));
  rep(i,n)rep(j,n) a[i][j] = i*n+j+1;
  int sum = 0;
  rep(i,n)rep(j,n) sum += a[i][j];
  s = sum/n;
  int score = calc();
  rep(ti,MX) {
    vvi p = a;
    int ai = rand()%n;
    int aj = rand()%n;
    int bi = rand()%n;
    int bj = rand()%n;
    swap(a[ai][aj], a[bi][bj]);
    int sc = calc();
    if (sc > score) {
      a = p;
    } else {
      score = sc;
      // cerr << score << endl;
      // rep(i,n) priv(a[i]);
      if (score == 0) break;
    }
  }
  rep(i,n) priv(a[i]);
  return 0;
}




0