#include <iostream>
#include <vector>
#include <tuple>
using namespace std;
vector<tuple<int, int, int>> ans;
int N, M, arr[105][105];
int tmp[105];
void hor(int i, int p)
{
p = (p % M + M) % M;
ans.push_back({1, i+1, p});
//i번이 이전 i-p번
for (int j = 0; j < M; j++) {
tmp[j] = arr[i][(j - p + M) % M];
}
for (int j = 0; j < M; j++) arr[i][j] = tmp[j];
}
void ver(int i, int p)
{
p = (p % N + N) % N;
ans.push_back({2, i+1, p});
for (int j = 0; j < N; j++) {
tmp[j] = arr[(j - p + N) % N][i];
}
for (int j = 0; j < N; j++) arr[j][i] = tmp[j];
}
bool done()
{
for (int i = 0; i < N; i++) {
if (arr[i][M-1] != i * M + (M-1)) return false;
}
for (int j = 0; j < M; j++) {
if (arr[N-1][j] != (N-1) * M + j) return false;
}
return true;
}
void last_swap(int i, int j)
{
hor(N-1, M-1-j);
ver(M-1, 1);
hor(N-1, j-i);
ver(M-1, -1);
hor(N-1, i-j);
ver(M-1, 1);
hor(N-1, 1+j);
}
void print()
{
cout << "STATE" << endl;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) cout << arr[i][j] << ' ';
cout << endl;
}
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
cin >> N >> M;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cin >> arr[i][j];
}
}
for (int i = 0; i < N-1; i++) {
for (int j = 0; j < M-1; j++) {
int place = i * M + j; //place번을 (i, j) 위치에 놓아야 함
int curx = -1, cury = -1;
for (int k = i; k < N; k++) {
for (int l = 0; l < M; l++) {
if (arr[k][l] == place) {
curx = k;
cury = l;
break;
}
}
if (cury != -1) break;
}
//(curx, cury)를 (i, j)에 옮기기
hor(curx, N - 1 - cury);
ver(M-1, 1);
hor(curx, 1 + cury);
hor(i, N - 1 - j);
ver(M-1, i - (curx + 1));
hor(i, 1 + j);
}
}
//M-1행 맞춰야 함
for (int i = 0; i < N - 1; i++) {
int place = i * M + M - 1;
int curx = -1, cury = -1;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (arr[i][j] == place) {
curx = i, cury = j;
break;
}
}
}
if (cury == M-1) {
ver(M-1, N-1-curx);
hor(N-1, -1);
ver(M-1, curx-i);
hor(N-1, 1);
ver(M-1, 1+i);
}
else {
ver(M-1, N-1-i);
hor(N-1, M-1-cury);
ver(M-1, 1+i);
}
}
for (int i = 0; i < M - 1; i++) {
int place = (N - 1) * M + i;
int cury = -1;
for (int j = i; j < M; j++) {
if (arr[N-1][j] == place) cury = j;
}
if (cury != i) last_swap(i, cury);
}
while (!done()) {
last_swap(0, 1);
last_swap(0, 1);
}
cout << ans.size() << '\n';
for (auto p:ans) {
cout << get<0>(p) << ' ' << get<1>(p) << ' ' << get<2>(p) << '\n';
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:58:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:59:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Unexpected end of file - int32 expected |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Unexpected end of file - int32 expected |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
340 KB |
Unexpected end of file - int32 expected |
2 |
Halted |
0 ms |
0 KB |
- |