#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define dbg(x) cerr << #x << " " << x << "\n"
using ll = long long;
int n, m;
vector <int> c_moves, l_moves;
int nr_op, ans;
const int MAX_N = 5;
int a[MAX_N][MAX_N];
int b[MAX_N][MAX_N];
bool check () {
bool okc = true;
for (int col = 0; col < m; col++) {
int color = a[0][col];
for (int row = 1; row < n; row++)
if (color != a[row][col])
okc = false;
}
bool okr = true;
for (int row = 0; row < n; row++) {
int color = a[row][0];
for (int col = 1; col < m; col++)
if (color != a[row][col])
okr = false;
}
return (okc || okr);
}
void move_row (int row) {
for (int col = 0; col < m; col++)
b[row][(col - 1 + m) % m] = a[row][col];
for (int col = 0; col < m; col++)
a[row][col] = b[row][col];
}
void move_col (int col) {
for (int row = 0; row < n; row++)
b[(row - 1 + n) % n][col] = a[row][col];
for (int row = 0; row < n; row++)
a[row][col] = b[row][col];
}
void go_rows (int nr_row, int nr_op) {
if (nr_row == n) {
if (check ())
ans = min (ans, nr_op);
return;
}
for (int i = 0; i < m; i++) {
go_rows (nr_row + 1, nr_op + min (i, m - i));
move_row (nr_row);
}
}
void go_columns (int nr_col, int nr_op) {
if (nr_col == m) {
go_rows (0, nr_op);
return;
}
for (int i = 0; i < n; i++) {
go_columns (nr_col + 1, nr_op + min (i, n - i));
move_col (nr_col);
}
}
int main () {
ios::sync_with_stdio (false);
cin.tie (0); cout.tie (0);
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> a[i][j];
ans = 100500;
go_columns (0, 0);
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
5 ms |
364 KB |
Output is correct |
9 |
Correct |
44 ms |
364 KB |
Output is correct |
10 |
Correct |
41 ms |
364 KB |
Output is correct |
11 |
Correct |
45 ms |
364 KB |
Output is correct |
12 |
Correct |
41 ms |
364 KB |
Output is correct |
13 |
Correct |
743 ms |
364 KB |
Output is correct |
14 |
Correct |
736 ms |
392 KB |
Output is correct |
15 |
Correct |
741 ms |
492 KB |
Output is correct |
16 |
Correct |
746 ms |
392 KB |
Output is correct |
17 |
Correct |
755 ms |
492 KB |
Output is correct |
18 |
Correct |
744 ms |
364 KB |
Output is correct |
19 |
Correct |
743 ms |
492 KB |
Output is correct |
20 |
Correct |
746 ms |
392 KB |
Output is correct |