๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/Note

[C++, ํ…œํ”Œ๋ฆฟ] ๋ฐฐ์—ด ๋Œ๋ฆฌ๊ธฐ์™€ ๋‹ฌํŒฝ์ด ๋ฐฐ์—ด

by ์•ˆ์ฃผํ˜• 2022. 9. 22.

1. ๋ฐฐ์—ด ์ƒํ•˜ ๋ฐ˜์ „

1 6 2 9 8 4 → 4 2 9 3 1 8
7 2 6 9 8 2 → 9 2 3 6 1 5
1 8 3 4 2 9 → 7 4 6 2 3 1
7 4 6 2 3 1 → 1 8 3 4 2 9
9 2 3 6 1 5 → 7 2 6 9 8 2
4 2 9 3 1 8 → 1 6 2 9 8 4
   <๋ฐฐ์—ด>       <์—ฐ์‚ฐ ๊ฒฐ๊ณผ>
void cmd1(){   // ๋ฐฐ์—ด ์ƒํ•˜ ๋ฐ˜์ „
    int temp[101][101] = {0, };
    for(int i=0; i<N; i++){
        for(int k=0; k<M; k++){
            temp[N-i-1][k] = map[i][k];
        }
    }
    memcpy(map, temp, sizeof(map));
}

 

2. ๋ฐฐ์—ด ์ขŒ์šฐ ๋ฐ˜์ „

1 6 2 9 8 4 → 4 8 9 2 6 1
7 2 6 9 8 2 → 2 8 9 6 2 7
1 8 3 4 2 9 → 9 2 4 3 8 1
7 4 6 2 3 1 → 1 3 2 6 4 7
9 2 3 6 1 5 → 5 1 6 3 2 9
4 2 9 3 1 8 → 8 1 3 9 2 4
   <๋ฐฐ์—ด>       <์—ฐ์‚ฐ ๊ฒฐ๊ณผ>
void cmd2(){   // ๋ฐฐ์—ด ์ขŒ์šฐ ๋ฐ˜์ „
    int temp[101][101] = {0, };
    for(int i=0; i<M; i++){
        for(int k=0; k<N; k++){
            temp[k][M-i-1] = map[k][i];
        }
    }
    memcpy(map, temp, sizeof(map));
}

 

3. ์˜ค๋ฅธ์ชฝ์œผ๋กœ 90๋„ ํšŒ์ „

1 6 2 9 8 4 → 4 9 7 1 7 1
7 2 6 9 8 2 → 2 2 4 8 2 6
1 8 3 4 2 9 → 9 3 6 3 6 2
7 4 6 2 3 1 → 3 6 2 4 9 9
9 2 3 6 1 5 → 1 1 3 2 8 8
4 2 9 3 1 8 → 8 5 1 9 2 4
   <๋ฐฐ์—ด>       <์—ฐ์‚ฐ ๊ฒฐ๊ณผ>
void cmd3(){  // ์‹œ๊ณ„๋ฐฉํ–ฅ 90๋„ ํšŒ์ „
    int temp[101][101] = {0, };
    swap(N, M); // (6, 8) -> (8, 6)
    for(int i=0; i<N; i++){
        for(int k=0; k<M; k++){
            temp[i][k] = map[M - k -1][i];
        }
    }
    memcpy(map, temp, sizeof(map));
}

 

4. ์™ผ์ชฝ์œผ๋กœ 90๋„ ํšŒ์ „

1 6 2 9 8 4 → 4 2 9 1 5 8
7 2 6 9 8 2 → 8 8 2 3 1 1
1 8 3 4 2 9 → 9 9 4 2 6 3
7 4 6 2 3 1 → 2 6 3 6 3 9
9 2 3 6 1 5 → 6 2 8 4 2 2
4 2 9 3 1 8 → 1 7 1 7 9 4
   <๋ฐฐ์—ด>       <์—ฐ์‚ฐ ๊ฒฐ๊ณผ>
void cmd4(){    //์‹œ๊ณ„ ๋ฐ˜๋Œ€๋ฐฉํ–ฅ 90๋„ ํšŒ์ „
    int temp[101][101] = {0, };
    swap(N, M);  // (6, 8) -> (8, 6)
    for(int i=0; i<N; i++){
        for(int k=0; k<M; k++){
            temp[i][k] = map[k][N-1-i];
        }
    }
    memcpy(map, temp, sizeof(map));
}

 

๋„ค ๊ทธ๋ฃน์œผ๋กœ ๋‚˜๋ˆˆ ๋’ค 5-1๊ณผ 5-2์ฒ˜๋Ÿผ ์œ„์น˜ ์ด๋™

1 1 1 1 2 2 2 2
1 1 1 1 2 2 2 2
1 1 1 1 2 2 2 2
4 4 4 4 3 3 3 3
4 4 4 4 3 3 3 3
4 4 4 4 3 3 3 3

 

5-1 (1->2,  2->3,  3->4,  4->1)

3 2 6 3 1 2 9 7 → 2 1 3 8 3 2 6 3
9 7 8 2 1 4 5 3 → 1 3 2 8 9 7 8 2
5 9 2 1 9 6 1 8 → 4 5 1 9 5 9 2 1
2 1 3 8 6 3 9 2 → 6 3 9 2 1 2 9 7
1 3 2 8 7 9 2 1 → 7 9 2 1 1 4 5 3
4 5 1 9 8 2 1 3 → 8 2 1 3 9 6 1 8
     <๋ฐฐ์—ด>            <์—ฐ์‚ฐ ๊ฒฐ๊ณผ>
void cmd5_1(){
    int temp[101][101] = {0, };
    for(int i=0; i<N/2; i++){   // 1 -> 2
        for(int k=0; k<M/2; k++){
            temp[i][M/2+k] = map[i][k];
        }
    }
    for(int i=0; i<N/2; i++){   // 2 -> 3
        int idx = M/2;
        for(int k=M/2; k<M; k++){
            temp[N/2+i][idx++] = map[i][k];
        }
    }
    for(int i=N/2; i<N; i++){   // 3 -> 4
        int idx1 = i;
        int idx2 = 0;
        for(int k=M/2; k<M; k++){
            temp[idx1][idx2++] = map[i][k];
        }
        idx1++;
    }
    
    int idx = 0;
    for(int i=N/2; i<N; i++){   // 4 -> 1
        for(int k=0; k<M/2; k++){
            temp[idx][k] = map[i][k];
        }
        idx++;
    }
    memcpy(map, temp, sizeof(map));
}

 

5-2 (1->4,  4->3,  3->2,  2->1)

3 2 6 3 1 2 9 7 → 1 2 9 7 6 3 9 2
9 7 8 2 1 4 5 3 → 1 4 5 3 7 9 2 1
5 9 2 1 9 6 1 8 → 9 6 1 8 8 2 1 3
2 1 3 8 6 3 9 2 → 3 2 6 3 2 1 3 8
1 3 2 8 7 9 2 1 → 9 7 8 2 1 3 2 8
4 5 1 9 8 2 1 3 → 5 9 2 1 4 5 1 9
     <๋ฐฐ์—ด>            <์—ฐ์‚ฐ ๊ฒฐ๊ณผ>
void cmd5_2(){
    int temp[101][101] = {0, };
    for(int i=0; i<N/2; i++){   // 1 -> 2
        for(int k=0; k<M/2; k++){
            temp[i][k] = map[i][M/2+k];
        }
    }
    for(int i=0; i<N/2; i++){   // 2 -> 3
        int idx = M/2;
        for(int k=M/2; k<M; k++){
            temp[i][k] = map[N/2+i][idx++];
        }
    }
    for(int i=N/2; i<N; i++){   // 3 -> 4
        for(int k=0; k<M/2; k++){
            temp[i][M/2+k] = map[i][k];
        }
    }
    
    for(int i=0; i<N/2; i++){   // 4 -> 1
        for(int k=0; k<M/2; k++){
            temp[N/2+i][k] = map[i][k];
        }
    }
    memcpy(map, temp, sizeof(map));
}

 

6. ์‹œ๊ณ„ ๋ฐ˜๋Œ€๋ฐฉํ–ฅ์œผ๋กœ ํ•œ ์นธ์”ฉ ํšŒ์ „

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]
   ↓         ↓                   ↑         ↑
A[3][1]   A[3][2] → A[3][3] → A[3][4]   A[3][5]
   ↓                                       ↑
A[4][1] → A[4][2] → A[4][3] → A[4][4] → A[4][5]
void cmd6(){    //์‹œ๊ณ„ ๋ฐ˜๋Œ€๋ฐฉํ–ฅ
    int x1 = 0, y1 = 0;
    int x2 = 0, y2 = M-1;
    int x3 = N-1, y3 = M-1;
    int x4 = N-1, y4 = 0;
    while(x1<x4 && y1<y2){
        int temp = map[x1][y1];
        for(int i=y1; i<y2; i++) map[x1][i] = map[x1][i+1];
        for(int i=x2; i<x3; i++) map[i][y2] = map[i+1][y2];
        for(int i=y3; i>y4; i--) map[x4][i] = map[x4][i-1];
        for(int i=x4; i>x1; i--) map[i][y1] = map[i-1][y1];
        map[x1+1][y1] = temp;
        x1++; y1++;
        x2++; y2--;
        x3--; y3--;
        x4--; y4++;
    }
}

 

7. ์‹œ๊ณ„๋ฐฉํ–ฅ์œผ๋กœ ํ•œ์นธ์”ฉ ํšŒ์ „

void cmd7(){  // ์‹œ๊ณ„๋ฐฉํ–ฅ
    int x1 = 0, y1 = 0;
    int x2 = 0, y2 = M-1;
    int x3 = N-1, y3 = M-1;
    int x4 = N-1, y4 = 0;
    while(x1<x4 && y1<y2){
        int temp = map[x1][y1];
        for(int i=x1; i<x4; i++) map[i][y1] = map[i+1][y1];
        for(int i=y4; i<y3; i++) map[x3][i] = map[x3][i+1];
        for(int i=x3; i>x2; i--) map[i][y2] = map[i-1][y2];
        for(int i=y2; i>y1; i--) map[x1][i] = map[x1][i-1];
        map[x1][y1+1] = temp;
        x1++; y1++;
        x2++; y2--;
        x3--; y3--;
        x4--; y4++;
    }
}

 

8. ๋‹ฌํŒฝ์ด ๋ฐฐ์—ด

25 10 11 12 13
24 9 2 3 14
23 8 1 4 15
22 7 6 5 16
21 20 19 18 17
 int arr[1000][1000] = {0, };
    int x=0, y=0;
    int num = N*N;
    int down_up_cnt = N, right_left_cnt=N-1;
    
    for(int i=0; i<N*N; i++){
        switch(i%4){
            case 0: //์•„๋ž˜๋ฐฉํ–ฅ
                if(arr[x][y]!=0) break;
                for(int k=0; k<down_up_cnt; k++){
                    arr[x][y]=num;
                    num--;
                    x++;
                }
                x--;
                y++;
                down_up_cnt--;
                break;
                
            case 1: //์˜ค๋ฅธ์ชฝ ๋ฐฉํ–ฅ
                if(arr[x][y]!=0) break;
                for(int k=0; k<right_left_cnt; k++){
                    arr[x][y]=num;
                    num--;
                    y++;
                }
                y--;
                x--;
                right_left_cnt--;
                break;
                
            case 2: //์œ„์ชฝ ๋ฐฉํ–ฅ
                if(arr[x][y]!=0) break;
                for(int k=0; k<down_up_cnt; k++){
                    arr[x][y]=num;
                    num--;
                    x--;
                }
                x++;
                y--;
                down_up_cnt--;
                break;
                
            case 3: //์™ผ์ชฝ ๋ฐฉํ–ฅ
                if(arr[x][y]!=0) break;
                for(int k=0; k<right_left_cnt; k++){
                    arr[x][y]=num;
                    num--;
                    y--;
                }
                y++;
                x++;
                right_left_cnt--;
                break;
        }
    }

๋Œ“๊ธ€