답안 #804021

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
804021 2023-08-03T06:53:17 Z 반딧불(#10100) Shifty Grid (CCO17_shifty) C++17
25 / 25
28 ms 1328 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int n, m;
int arr[102][102];
int idxX[10002], idxY[10002];

struct dat{
    int a, b, c;
    dat(){}
    dat(int a, int b, int c): a(a), b(b), c(c){}
};
vector<dat> ans;

int tmp[102];

void rotateRow(int i, int c){
    for(int j=0; j<m; j++) tmp[j] = arr[i][j];
    for(int j=0; j<m; j++){
        arr[i][j] = tmp[(j+m-c)%m];
        idxX[arr[i][j]] = i, idxY[arr[i][j]] = j;
    }
    ans.push_back(dat(1, i, c));
}

void rotateColumn(int j, int c){
    for(int i=0; i<n; i++) tmp[i] = arr[i][j];
    for(int i=0; i<n; i++){
        arr[i][j] = tmp[(i+n-c)%n];
        idxX[arr[i][j]] = i, idxY[arr[i][j]] = j;
    }
    ans.push_back(dat(2, j, c));
}

void sendU(int sx, int sy, int ex, int ey){
    if(sx==ex && sy==ey) return;
    if(sx != ex && sy != ey){
        rotateColumn(ey, (sx-ex+n)%n);
        rotateRow(sx, (ey-sy+m)%m);
        rotateColumn(ey, (ex-sx+n)%n);
        rotateRow(sx, (sy-ey+m)%m);
    }
    else if(sy == ey){
        rotateRow(sx, m-1);
        rotateColumn(ey, (sx-ex+n)%n);
        rotateRow(sx, 1);
        rotateColumn(ey, (ex-sx+n)%n);
    }
    else{
        rotateColumn(sy, n-1);
        rotateRow(ex, (sy-ey+m)%m);
        rotateColumn(sy, 1);
        rotateRow(ex, (ey-sy+m)%m);
    }
}

void sendL(int sx, int sy, int ex, int ey){
    if(sx==ex && sy==ey) return;
    if(sx != ex && sy != ey){
        rotateRow(ex, (sy-ey+m)%m);
        rotateColumn(sy, (ex-sx+n)%n);
        rotateRow(ex, (ey-sy+m)%m);
        rotateColumn(sy, (sx-ex+n)%n);
    }
    else if(sx == ex){
        int a = 1, b = n-1;
        if(sx == n-2) a = n-1, b = 1;
        rotateColumn(sy, a);
        rotateRow(ex, (sy-ey+m)%m);
        rotateColumn(sy, b);
        rotateRow(ex, (ey-sy+m)%m);
    }
    else{
        rotateRow(sx, m-1);
        rotateColumn(ey, (sx-ex+n)%n);
        rotateRow(sx, 1);
        rotateColumn(ey, (ex-sx+n)%n);
    }
}

int main(){
    scanf("%d %d", &n, &m);
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            scanf("%d", &arr[i][j]);
//            arr[i][j] = i*m+j;
//            if(arr[i][j] <= 1) arr[i][j] = !arr[i][j];
            idxX[arr[i][j]] = i, idxY[arr[i][j]] = j;
        }
    }

    again:
    for(int i=0; i<n-2; i++){
        for(int j=0; j<m; j++){
            sendU(idxX[i*m+j], idxY[i*m+j], i, j);
        }
    }

    for(int j=0; j<m-2; j++){
        for(int i=n-2; i<n; i++){
            sendL(idxX[i*m+j], idxY[i*m+j], i, j);
        }
    }

    sendU(idxX[(n-2)*m+(m-2)], idxY[(n-2)*m+(m-2)], n-2, m-2);
    sendU(idxX[(n-2)*m+(m-1)], idxY[(n-2)*m+(m-1)], n-2, m-1);

    if(arr[n-1][m-1] != n*m-1){
        int s = rand()%3;
        for(int i=0; i<s; i++){
            if(rand()%2) rotateRow(rand()%n, rand()%m);
            else rotateColumn(rand()%m, rand()%n);
        }
        goto again;
    }

    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
//            printf("%2d ", arr[i][j]);
            assert(arr[i][j] == i*m+j);
        }
//        puts("");
    }

    printf("%d\n", (int)ans.size());
    for(dat p: ans) printf("%d %d %d\n", p.a, p.b+1, p.c);
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:85:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:88:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |             scanf("%d", &arr[i][j]);
      |             ~~~~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 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 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 0 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 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 3 ms 468 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 1 ms 340 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 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 0 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 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 3 ms 468 KB Output is correct
19 Correct 2 ms 468 KB Output is correct
20 Correct 2 ms 468 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 4 ms 592 KB Output is correct
23 Correct 5 ms 596 KB Output is correct
24 Correct 3 ms 468 KB Output is correct
25 Correct 4 ms 468 KB Output is correct
26 Correct 2 ms 384 KB Output is correct
27 Correct 2 ms 468 KB Output is correct
28 Correct 26 ms 1308 KB Output is correct
29 Correct 28 ms 1300 KB Output is correct
30 Correct 28 ms 1328 KB Output is correct
31 Correct 27 ms 1268 KB Output is correct
32 Correct 26 ms 1256 KB Output is correct
33 Correct 4 ms 660 KB Output is correct
34 Correct 3 ms 468 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 18 ms 1104 KB Output is correct
37 Correct 3 ms 468 KB Output is correct
38 Correct 5 ms 660 KB Output is correct
39 Correct 4 ms 536 KB Output is correct
40 Correct 9 ms 848 KB Output is correct
41 Correct 4 ms 468 KB Output is correct
42 Correct 3 ms 468 KB Output is correct
43 Correct 16 ms 976 KB Output is correct
44 Correct 3 ms 468 KB Output is correct
45 Correct 11 ms 904 KB Output is correct
46 Correct 6 ms 660 KB Output is correct