Submission #676577

#TimeUsernameProblemLanguageResultExecution timeMemory
676577QwertyPiRiddick's Cube (IZhO13_riddicks)C++14
100 / 100
2 ms304 KiB
#include <bits/stdc++.h> using namespace std; int a[5][5]; int R, C; void shift_r(int r, int v){ int b[5]; for(int i = 0; i < C; i++){ b[i] = a[r][(i + v) % C]; } for(int i = 0; i < C; i++){ a[r][i] = b[i]; } } void shift_c(int c, int v){ int b[5]; for(int i = 0; i < R; i++){ b[i] = a[(i + v) % R][c]; } for(int i = 0; i < R; i++){ a[i][c] = b[i]; } } int ans = 100500; int cx = 0; int d(int x, int y, int C){ return min(C - abs(x - y), abs(x - y)); } void test(){ bool all_eq = true; for(int i = 0; i < R; i++){ for(int j = 1; j < C; j++){ all_eq &= a[i][j] == a[i][0]; } } if(all_eq) ans = min(ans, cx); int b[5]; for(int i = 0; i < C; i++){ b[i] = a[0][i]; } map<int, int> M[5]; for(int i = 0; i < R; i++){ bool r_eq = false; for(int tr = 0; tr < C; tr++){ bool eq = true; for(int j = 0; j < C; j++){ eq &= b[j] == a[i][j]; } r_eq |= eq; if(eq) M[i][tr]++; shift_r(i, 1); } if(!r_eq) return; } for(int i = 0; i < C; i++){ int c = i, cost = 0; for(int j = 0; j < R; j++){ int min_cost = 10; for(auto k : M[j]){ min_cost = min(min_cost, d(c, k.first, C)); } cost += min_cost; } ans = min(ans, cost + cx); } } void rec(int idx){ if(idx == C){ test(); return; } for(int i = 0; i < R; i++){ cx += min(i, R - i); rec(idx + 1); shift_c(idx, 1); cx -= min(i, R - i); } } int main(){ cin >> R >> C; for(int i = 0; i < R; i++){ for(int j = 0; j < C; j++){ cin >> a[i][j]; } } rec(0); cout << ans << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...