16926번: 배열 돌리기 1 (acmicpc.net)

 

 

16926번: 배열 돌리기 1

크기가 N×M인 배열이 있을 때, 배열을 돌려보려고 한다. 배열은 다음과 같이 반시계 방향으로 돌려야 한다. A[1][1] ← A[1][2] ← A[1][3] ← A[1][4] ← A[1][5] ↓ ↑ A[2][1] A[2][2] ← A[2][3] ← A[2][4] A[2][5]

www.acmicpc.net

소요시간 약 2시간 정도?

디버깅하는데 시간 많이 소요함.

#include<iostream>

// 버그 찾기 /  6,4, 1
using namespace std;


int map[300][1000];
struct node { // 회전 시작 지점  초기화 -1, -1 로 spin 접근 시 +1 로 하여 0,0부터 시작
    int y = -1; // 외부 내부 꼭지점 지정할 때 사용
    int x = -1;
}now;
int n, m, s;
node vertex[3]{ 0, }; // 0,0  제외한 꼭지점
void spin(int rr, int cc) { //rr , cc 가 끝단들
    now.y += 1; // 시작지점들
    now.x += 1;

    vertex[0] = { n - now.y-1,now.x }; // n,0 
    vertex[1] = { n - now.y-1,m - now.x-1 }; // n,m 
    vertex[2] = { now.y,m - now.x-1 }; // 0,m 시작. 점점 줄어듦.
    int tmp1 = map[vertex[0].y][vertex[0].x];
    int tmp2 = map[vertex[1].y][vertex[1].x];
    int tmp3 = map[vertex[2].y][vertex[2].x];
    //4개 일일이 cover할 예정

    //버그. 안쪽 커버 못함.
    for (int y = rr - 1; y >= now.y; --y) { //좌측, 행
        map[y + 1][now.x] = map[y][now.x];
    }
    for (int x = cc - 1; x >= now.x; --x)  //하측, 열
    {
        map[rr][x + 1] = map[rr][x];
    }
    map[rr][now.x + 1] = tmp1;
    for (int y = now.y + 1; y < rr; ++y)  //우측, 행
    {
        map[y-1][cc] = map[y][cc];
    }
    map[rr - 1][cc] = tmp2;
    for (int x = now.x + 1; x < cc; ++x) {//상측 열
        map[now.y][x - 1] = map[now.y][x];
    }
    map[now.y][cc - 1] = tmp3;


}
int main(void) {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  //  freopen("텍스트.txt", "r", stdin);
    cin >> n >> m >> s;
    for (int y = 0; y < n; ++y) {
        for (int x = 0; x < m; ++x) {
            cin >> map[y][x];
        }
    }
  
    int r = n-1;
    int c = m-1;
    int Min = n;
    if (n > m)Min = m;
    int tmp = Min;
    for (int t = 0; t < s; ++t) { // s회전
        //Min 은 테두리에서 안쪽으로 회전하는 용도. 근데 2번째 이상 회전시 문제 발생.
        while (Min > 0) { // 안쪽 까지 하기 위함.
            spin(r, c);
            r -= 1;
            c -= 1;
            Min -= 2;
        }
        //회전 후 초기화(중요)
        Min = tmp; 
        r = n - 1;
        c = m - 1;
        now={ -1,-1 };
    }

    for (int y = 0; y < n; ++y) {
        for (int x = 0; x < m; ++x) {
            cout << map[y][x] << " ";
        }
        cout << endl;
    }
}

 

 

하기 문제집 중 1번문제

문제집: 배열 돌리기 문제집 (ljh1012kr) (acmicpc.net)

 

문제집: 배열 돌리기 문제집 (ljh1012kr)

 

www.acmicpc.net

 

+ Recent posts