#include #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include using namespace std; using lint = long long int; using P = pair; using PL = pair; #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) #define ALL(a) (a).begin(),(a).end() constexpr int MOD = 1000000007; vector RH_B = {1532834020, 1388622299}; vector RH_M = {2147482409, 2147478017}; constexpr int INF = 2147483647; void yes(bool expr) {cout << (expr ? "Yes" : "No") << "\n";} templatevoid chmax(T &a, const T &b) { if (avoid chmin(T &a, const T &b) { if (b A; vector dx = {-1, 0, 1, 0}; vector dy = {0, 1, 0, -1}; vector dc = {'U', 'R', 'D', 'L'}; void set_B(int idx, vector &B, vector &light_id) { if(idx < N) { B[idx] = light_id[0*N*4 + idx*4 + 0]; } else if(idx < 2*N) { B[idx] = light_id[(idx-N)*N*4 + (N-1)*4 + 1]; } else if(idx < 3*N) { B[idx] = light_id[(N-1)*N*4 + (N-1 - (idx-2*N))*4 + 2]; } else if(idx < 4*N) { B[idx] = light_id[(N-1 - (idx-3*N))*N*4 + 0*4 + 3]; } } int calc_score(vector &B) { //転倒数を最小化 int score = M*(M-1) / 2; REP(i, M) FOR(j, i+1, M) if(B[i] > B[j]) score--; return score; } vector get_B(vector &light_id, vector &grid) { vector B(M); //全てのポートから発射される光を順番に追跡する(計算量N*N*4) REP(port, M) { int x, y, d; if(port < N) { x = 0; y = port; d = 2; } else if(port < 2*N) { x = port - N; y = N-1; d = 3; } else if(port < 3*N) { x = N-1; y = N-1 - (port - 2*N); d = 0; } else { x = N-1 - (port - 3*N); y = 0; d = 1; } while(x >= 0 && x < N && y >= 0 && y < N) { if(grid[x][y] == '1') { if(d == 0) d = 1; else if(d == 1) d = 0; else if(d == 2) d = 3; else if(d == 3) d = 2; } else if(grid[x][y] == '2') { if(d == 0) d = 3; else if(d == 1) d = 2; else if(d == 2) d = 1; else if(d == 3) d = 0; } int nx = x + dx[d]; int ny = y + dy[d]; x = nx; y = ny; } if(x == -1) B[y] = A[port]; else if(y == N) B[x + N] = A[port]; else if(x == N) B[3*N-1 - y] = A[port]; else if(y == -1) B[4*N-1 - x] = A[port]; } return B; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> N; M = 256; A.resize(M); REP(i, M) cin >> A[i]; REP(i, M) A[i]--; //そのマスでその方向に向かっている光のID vector light_id(N*N*4); //そのマスからその方向に向かったときに到達するポート vector next_port(N*N*4); REP(i, N) REP(j, N) { light_id[i*N*4 + j*4 + 0] = A[3*N-1 - j]; light_id[i*N*4 + j*4 + 1] = A[4*N-1 - i]; light_id[i*N*4 + j*4 + 2] = A[j]; light_id[i*N*4 + j*4 + 3] = A[N + i]; next_port[i*N*4 + j*4 + 0] = j; next_port[i*N*4 + j*4 + 1] = N + i; next_port[i*N*4 + j*4 + 2] = 3*N-1 - j; next_port[i*N*4 + j*4 + 3] = 4*N-1 - i; } vector grid(N, string(N, '0')); vector B = get_B(light_id, grid); int best_score = calc_score(B); double time = 0; int loop = 0; double start_temp = 1.0, end_temp = 0.0; double temp = start_temp; auto start_time = std::chrono::system_clock::now(); int best_score_2 = best_score; vector best_grid = grid; int TIME_LIMIT = 1900; while(true) { if(loop%500 == 0) { auto now_time = std::chrono::system_clock::now(); time = std::chrono::duration_cast(now_time-start_time).count(); temp = start_temp + (end_temp - start_temp) * time / TIME_LIMIT; cerr << "LOOP:" << loop << " SCORE:" << best_score << " TIME:" << time << endl; if(time > TIME_LIMIT) break; } loop++; int i = rand() % N; int j = rand() % N; char old_val = grid[i][j]; char new_val = '0' + (rand() % 3); if(new_val == old_val) continue; grid[i][j] = new_val; vector new_B = get_B(light_id, grid); int new_score = calc_score(new_B); double prob = exp(((double)new_score-best_score)/pow(10, temp)); if(new_score > best_score || prob > (rand()%INF)/(double)INF) { best_score = new_score; B = new_B; if(new_score > best_score_2) { best_score_2 = new_score; best_grid = grid; } } else { grid[i][j] = old_val; } } REP(i, N) cout << best_grid[i] << "\n"; cerr << best_score_2 << "\n"; }