import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; public class Main { static boolean batch = false; static boolean readFile = false; static boolean outFile = false; static int caseNum = 0; static double startTemp = 3; static double endTemp = 0; static long limitTime = 1900; static int[] dx = {1, 0, -1, 0}; static int[] dy = {0, 1, 0, -1}; static int N, M, NN; static int[] a; static PrintWriter pw; static int okCnt, ngCnt; public static void main(String[] args) throws Exception { if (batch) { readFile = true; } if (readFile) { if (batch) { long tl = 2000; long tw = tl - 200; long total = 0; int bidx = 0; long best = 0; int widx = 0; long worst = 1000000000000000000L; int re = 0; int tle = 0; for (int z = 0; z < 100; z++) { try { long st = System.currentTimeMillis(); long score = solve(z); long time = System.currentTimeMillis() - st; if (time > tw) { System.out.println(score + "\t" + time + "ms"); if (time > tl) { tle++; } } else { System.out.println(score); } total += score; if (score > best) { best = score; bidx = z; } if (score < worst) { worst = score; widx = z; } } catch (Exception e) { System.out.println(z + ":\t" + e.getMessage()); re++; } } System.out.println("total: " + total); System.out.println("best: " + bidx + ": " + best); System.out.println("worst: " + widx + ": " + worst); System.out.println("RE: " + re); System.out.println("TLE: " + tle); } else { long score = solve(caseNum); System.out.println(okCnt + "\t" + ngCnt); System.out.println(score); } } else { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); init(br); br.close(); solve(); } } static long solve(int z) throws Exception { StringBuilder sb = new StringBuilder(); sb.append(z); while (sb.length() < 4) { sb.insert(0, '0'); } File file = new File("G:\\yuki\\5026\\in\\" + sb.toString() + ".txt"); BufferedReader br = new BufferedReader(new FileReader(file)); init(br); br.close(); if (outFile) { File file2 = new File("G:\\yuki\\5026\\out\\" + sb.toString() + ".txt"); pw = new PrintWriter(file2); } long res = solve(); if (outFile) { pw.close(); } return res; } static void init(BufferedReader br) throws Exception { N = Integer.parseInt(br.readLine()); M = N * 4; NN = N * N; String[] sa = br.readLine().split(" "); a = new int[M]; for (int i = 0; i < M; i++) { a[i] = Integer.parseInt(sa[i]); } } static long solve() { char[][] s = new char[N][N]; for (int i = 0; i < N; i++) { Arrays.fill(s[i], '0'); } long startTime = System.currentTimeMillis(); long preScore = simulate(s); while (true) { long time = System.currentTimeMillis() - startTime; if (time > limitTime) { break; } double ratio = (double) time / limitTime; double temp = startTemp * (1 - ratio) + endTemp * ratio; int p = (int) (Math.random() * NN); int x = p / N; int y = p % N; char pc = s[x][y]; int nc = pc - '0'; nc += Math.random() * 2 + 1; if (nc >= 3) { nc -= 3; } s[x][y] = (char) ('0' + nc); long newScore = simulate(s); double prob = Math.exp((preScore - newScore) / temp); if (Math.random() < prob) { // 更新 preScore = newScore; okCnt++; } else { // 戻す s[x][y] = pc; ngCnt++; } } if (outFile) { output(pw, s); } else if (!batch) { output(new PrintWriter(System.out), s); } long score = M * (M - 1) / 2 - preScore; return score; } static long simulate(char[][] s) { int[] b = new int[M]; for (int i = 0; i < M; i++) { int x = 0; int y = 0; int d = 0; if (i < N) { x = 0; y = i; d = 0; } else if (i < N * 2) { x = i - N; y = N - 1; d = 3; } else if (i < N * 3) { x = N - 1; y = N * 3 - 1 - i; d = 2; } else { x = N * 4 - 1 - i; y = 0; d = 1; } while (0 <= x && x < N && 0 <= y && y < N) { if (s[x][y] == '1') { d = 3 - d; } else if (s[x][y] == '2') { d = d ^ 1; } x += dx[d]; y += dy[d]; } if (x < 0) { b[y] = a[i]; } else if (y >= N) { b[N + x] = a[i]; } else if (x >= N) { b[N * 3 - 1 - y] = a[i]; } else { b[N * 4 - 1 - x] = a[i]; } } return tentousuu(b); } static long tentousuu(int[] b) { BIT bit = new BIT(M); long ret = 0; for (int i = 0; i < M; i++) { ret += i - bit.sum(b[i]); bit.add(b[i], 1); } return ret; } static class BIT { int n; long[] arr; public BIT(int n) { this.n = n; arr = new long[n + 1]; } void add(int idx, long val) { for (int i = idx; i <= n; i += i & -i) { arr[i] += val; } } long sum(int idx) { long sum = 0; for (int i = idx; i > 0; i -= i & -i) { sum += arr[i]; } return sum; } } static void output(PrintWriter pw, char[][] s) { for (int i = 0; i < N; i++) { pw.println(s[i]); } pw.flush(); } }