# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
742208 | rainboy | Riddick's Cube (IZhO13_riddicks) | C11 | 156 ms | 288 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |