#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m;
int a[101][101], b[101][101];
pair<int, int> pos[100100];
vector<array<int, 3>> Q;
void shiftR(int i, int c){
assert(0 <= c && c < m);
for (int j=0;j<m;j++) b[i][(j+c)%m] = a[i][j];
for (int j=0;j<m;j++){
a[i][j] = b[i][j];
pos[a[i][j]] = {i, j};
}
Q.push_back({1, i+1, c});
}
void shiftC(int j, int c){
assert(0 <= c && c < n);
for (int i=0;i<n;i++) b[(i+c)%n][j] = a[i][j];
for (int i=0;i<n;i++){
a[i][j] = b[i][j];
pos[a[i][j]] = {i, j};
}
Q.push_back({2, j+1, c});
}
void solveR(int p){
for (int j=0;j<m;j++){
auto [cx, cy] = pos[p*m + j];
if (cx==p && cy==j) continue;
if (cx==p){
shiftC(j, 1);
shiftC(cy, 1);
shiftR(p+1, (m+j-cy) % m);
shiftC(j, n-1);
shiftC(cy, n-1);
}
else if (cy==j){
shiftR(cx, 1);
shiftC(j, (n+cx-p) % n);
shiftR(cx, m-1);
shiftC(j, (n+p-cx) % n);
}
else{
shiftC(j, (n+cx-p) % n);
shiftR(cx, (m+j-cy) % m);
shiftC(j, (n+p-cx) % n);
}
}
// printf("ok\n");
// for (int i=0;i<n;i++){
// for (int j=0;j<m;j++) printf("%d ", a[i][j]);
// printf("\n");
// }
}
void swap2(int p, int q){
shiftC(p, 1);
shiftR(0, (m+q-p) % m);
shiftC(q, 1);
shiftR(0, (m+p-q) % m);
shiftC(p, 1);
}
void sort2(){
for (int j=0;j<m;j++){
auto [cx, cy] = pos[m+j];
if (cy==j) continue;
swap2(j, cy);
}
}
int main(){
scanf("%d %d", &n, &m);
for (int i=0;i<n;i++){
for (int j=0;j<m;j++){
scanf("%d", a[i]+j);
pos[a[i][j]] = {i, j};
}
}
for (int i=0;i<n-1;i++) solveR(i);
if (m==2){
if (a[n-1][0] > a[n-1][1]) shiftR(n-1, 1);
}
else if (n==2) sort2();
for (int i=0;i<n;i++){
for (int j=0;j<m;j++) assert(a[i][j] == i*m + j);
}
// printf("ok done\n");
// for (int i=0;i<n;i++){
// for (int j=0;j<m;j++) printf("%d ", a[i][j]);
// printf("\n");
// }
printf("%d\n", (int)Q.size());
for (auto &[x, y, z]:Q) printf("%d %d %d\n", x, y, z);
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:84:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:87:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | scanf("%d", a[i]+j);
| ~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
340 KB |
Output is correct |
6 |
Runtime error |
8 ms |
1228 KB |
Execution killed with signal 6 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Runtime error |
8 ms |
1228 KB |
Execution killed with signal 6 |
19 |
Halted |
0 ms |
0 KB |
- |