Submission #676577

# Submission time Handle Problem Language Result Execution time Memory
676577 2022-12-31T10:36:33 Z QwertyPi Riddick's Cube (IZhO13_riddicks) C++14
100 / 100
2 ms 304 KB
#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 time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 300 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 2 ms 212 KB Output is correct
14 Correct 2 ms 212 KB Output is correct
15 Correct 2 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 2 ms 304 KB Output is correct
18 Correct 2 ms 212 KB Output is correct
19 Correct 2 ms 304 KB Output is correct
20 Correct 2 ms 296 KB Output is correct