#include <stdio.h>
#define N 5
#define M 5
#define INF 100500
int min(int a, int b) { return a < b ? a : b; }
int aa[N][M], n, m;
int check() {
int i, j, good;
good = 1;
for (i = 0; i < n; i++)
for (j = 1; j < m; j++)
if (aa[i][j] != aa[i][j - 1]) {
good = 0;
goto out1;
}
out1:
if (good)
return 1;
good = 1;
for (j = 0; j < m; j++)
for (i = 1; i < n; i++)
if (aa[i][j] != aa[i - 1][j]) {
good = 0;
goto out2;
}
out2:
return good;
}
int solve2(int i, int d) {
int j, k, tmp, ans;
if (i == n)
return check() ? d : INF;
ans = INF;
for (k = 0; k < m; k++) {
ans = min(ans, solve2(i + 1, d + min(k, m - k)));
tmp = aa[i][0];
for (j = 0; j + 1 < m; j++)
aa[i][j] = aa[i][j + 1];
aa[i][m - 1] = tmp;
}
return ans;
}
int solve1(int j, int d) {
int i, k, tmp, ans;
if (j == m)
return solve2(0, d);
ans = INF;
for (k = 0; k < n; k++) {
ans = min(ans, solve1(j + 1, d + min(k, n - k)));
tmp = aa[0][j];
for (i = 0; i + 1 < n; i++)
aa[i][j] = aa[i + 1][j];
aa[n - 1][j] = tmp;
}
return ans;
}
int main() {
int i, j;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
scanf("%d", &aa[i][j]);
printf("%d\n", solve1(0, 0));
return 0;
}
Compilation message
riddicks.c: In function 'main':
riddicks.c:70:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
riddicks.c:73:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | scanf("%d", &aa[i][j]);
| ^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
288 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 |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
11 ms |
212 KB |
Output is correct |
10 |
Correct |
11 ms |
280 KB |
Output is correct |
11 |
Correct |
11 ms |
280 KB |
Output is correct |
12 |
Correct |
11 ms |
212 KB |
Output is correct |
13 |
Correct |
138 ms |
212 KB |
Output is correct |
14 |
Correct |
136 ms |
260 KB |
Output is correct |
15 |
Correct |
156 ms |
212 KB |
Output is correct |
16 |
Correct |
148 ms |
212 KB |
Output is correct |
17 |
Correct |
136 ms |
260 KB |
Output is correct |
18 |
Correct |
137 ms |
260 KB |
Output is correct |
19 |
Correct |
137 ms |
212 KB |
Output is correct |
20 |
Correct |
136 ms |
260 KB |
Output is correct |