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 100
#define M 100
#define K (N * M * 4 + 1)
int aa[N][M], ii[N * M], jj[N * M], n, m;
int tt[K], hh[K], dd[K], k;
void rotate(int i1, int j1, int i2, int j2, int cw) {
int di, dj, a, b, c;
di = (i2 - i1 + n) % n, dj = (j2 - j1 + m) % m;
if (cw) {
tt[k] = 2, hh[k] = j1, dd[k] = n - di, k++;
tt[k] = 1, hh[k] = i1, dd[k] = m - dj, k++;
tt[k] = 2, hh[k] = j1, dd[k] = di, k++;
tt[k] = 1, hh[k] = i1, dd[k] = dj, k++;
a = aa[i1][j1], b = aa[i1][j2], c = aa[i2][j1];
aa[i1][j1] = c, ii[c] = i1, jj[c] = j1;
aa[i1][j2] = a, ii[a] = i1, jj[a] = j2;
aa[i2][j1] = b, ii[b] = i2, jj[b] = j1;
} else {
tt[k] = 1, hh[k] = i1, dd[k] = m - dj, k++;
tt[k] = 2, hh[k] = j1, dd[k] = n - di, k++;
tt[k] = 1, hh[k] = i1, dd[k] = dj, k++;
tt[k] = 2, hh[k] = j1, dd[k] = di, k++;
a = aa[i1][j1], b = aa[i1][j2], c = aa[i2][j1];
aa[i1][j1] = b, ii[b] = i1, jj[b] = j1;
aa[i1][j2] = c, ii[c] = i1, jj[c] = j2;
aa[i2][j1] = a, ii[a] = i2, jj[a] = j1;
}
}
int main() {
static char visited[N * M];
int h, i, j, a, c, tmp;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++)
for (j = 0; j < m; j++) {
scanf("%d", &a);
aa[i][j] = a, ii[a] = i, jj[a] = j;
}
c = 0;
for (a = 0; a < n * m; a++)
if (!visited[a]) {
c++;
while (!visited[a])
visited[a] = 1, a = ii[a] * m + jj[a];
}
if (c % 2 != 0) {
tt[k] = 1, hh[k] = 0, dd[k] = 1, k++;
for (j = m - 1; j > 0; j--)
tmp = aa[0][j], aa[0][j] = aa[0][j - 1], aa[0][j - 1] = tmp;
for (j = 0; j < m; j++)
jj[aa[0][j]] = j;
}
for (i = 0; i + 2 < n; i++)
for (j = 0; j < m; j++) {
a = i * m + j;
if (ii[a] == i && jj[a] == j)
continue;
if (ii[a] == i)
rotate(i, j, i + 1, jj[a], 0);
else if (jj[a] != j)
rotate(ii[a], j, i, jj[a], 1);
else
rotate(ii[a], j, i, j ^ 1, 0);
}
for (j = 0; j + 1 < m; j++)
for (i = n - 2; i < n; i++) {
a = i * m + j;
if (ii[a] == i && jj[a] == j)
continue;
if (jj[a] == j)
rotate(i, j, ii[a], j + 1, 1);
else if (ii[a] != i)
rotate(i, jj[a], ii[a], j, 0);
else
rotate(i, jj[a], i ^ 1, j, 1);
}
printf("%d\n", k);
for (h = 0; h < k; h++)
printf("%d %d %d\n", tt[h], hh[h] + 1, dd[h]);
return 0;
}
Compilation message (stderr)
Main.c: In function 'main':
Main.c:40:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:43:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | scanf("%d", &a);
| ^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |