본문 바로가기

자료구조+알고리즘/BOJ

[16935][java][백준]배열 돌리기 3

알고리즘 분류

  • 구현

 

 

문제

 

풀이

 

이번 문제는 실버2임에도 불구하고 쉬운편! 하지만 계산과정이 많아서 귀찮고 머리가 복잡하다..

 

1번 연산 : 배열 상하 반전
2번 연산 : 좌우 반전
3번 연산 : 오른쪽 90도 회전
4번 연산 : 왼쪽 90도 회전
5번 연산 : 4그룹 1->2, 2->3, 3->4, 4->1
6번 연산 : 4그룹 1->4, 4->3, 3->2, 2->1

 

시간이 많이 걸린건 3번 부터..

3번은 전체적으로 위치를 바꿔주면 되니까 한행이 바뀌는 위치를 보고 위치조정 해주면 된다

5번부터 한 배열을 4부분으로 나눠서 위치를 바꿔주는데 한행을 두개로 나누면 0열~m/2열 , m/2열~m열로 시작점과 끝점을 달리해서 위치를 바꿔준다 ! 하지만 여기서 주의할 점은 행과 열이 다르니 마지막 결과값이 행과 열이 바뀌므로 이것도 처리해줘야 오류가 안난다 ! 

 

5번, 6번 이중for문을 4개씩 다 따로 위치 조정을 했는데 뒤늦게 생각해보니 이중for문 하나로 전체적인 칸에서 그냥 if문으로 범위 조건 달아서 돌려도 될듯...

너무 길어진 코드가 되었지만 제출이 성공해서 뿌듯하다 

 

java코드

switch~case문으로 함수 호출 해서 구현하였습니다.

 

* 주석을 참고해서 보면 이해가 됩니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import java.io.BufferedReader;
        import java.io.IOException;
        import java.io.InputStreamReader;
        import java.util.StringTokenizer;
 
public class Main {
    static int n,m,r,num;
    static int[][] map;
    static int[][] tmpMap;
 
 
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
 
        n = Integer.parseInt(st.nextToken());
        m = Integer.parseInt(st.nextToken());
        r = Integer.parseInt(st.nextToken());
 
        map = new int[n][m];
 
        for(int i=0; i<n; i++){
            st = new StringTokenizer(br.readLine());
            for(int j=0; j<m; j++){
                map[i][j] = Integer.parseInt(st.nextToken());
            }
        }
 
 
        st = new StringTokenizer(br.readLine());
        // 케이스 별로 연산의 수만큼 반복 계산
        for(int i=0; i<r; i++){
            //케이스 번호
            num = Integer.parseInt(st.nextToken());
 
            switch(num){
//                연산 : 배열 상하 반전
                case 1:
                    one();
                    break;
//                2번 연산 : 좌우 반전
                case 2:
                    two();
                    break;
//                3번 연산 : 오른쪽 90도 회전
                case 3:
                    three();
                    break;
//                4번 연산 : 왼쪽 90도 회전
                case 4:
                    four();
                    break;
//                5번 연산 : 4그룹 1->2, 2->3, 3->4, 4->1
                case 5:
                    five();
                    break;
//                6번 연산 : 4그룹 1->4, 4->3, 3->2, 2->1
                case 6:
                    six();
                    break;
 
            }
 
        }
        //결과 배열 출력
        for(int i=0; i<n; i++){
            for(int j=0; j<m; j++){
                System.out.print(map[i][j]+ " " );
            }
            System.out.println();
        }
    }
 
    /*----------------------------------------------------------*/
    public static void one(){
        //1번 연산 : 배열 상하 반전
        tmpMap = new int[n][m];
        for(int i=0; i<n; i++){
            for(int j=0; j<m; j++){
                tmpMap[n-i-1][j] = map[i][j];  //행만 서로 반대
            }
        }
        map = tmpMap;
    }
    public static void two(){
        //2번 연산 : 좌우 반전
        tmpMap = new int[n][m];
        for(int i=0; i<n; i++){
            for(int j=0; j<m; j++){
               tmpMap[i][m-j-1= map[i][j]; //열만 반대
            }
        }
        map = tmpMap;
    }
    public static void three(){
        //3번 연산 : 오른쪽 90도 회전
        tmpMap = new int[m][n];
 
        for(int i=0; i<n; i++){
 
            for(int j=0; j<m; j++){
                //System.out.println("i, j : " + i + ", " + j);
                //System.out.println(j + ", " + (n-i-1));
                tmpMap[j][n-i-1=  map[i][j];
            }
        }
        //배열 바꾸기 => 결과 배열 크기 다르므로
        int tmp = n;
        n = m;
        m= tmp;
 
        map = tmpMap;
 
    }
    public static void four(){
        //4번 연산 : 왼쪽 90도 회전
        tmpMap = new int[m][n];
        for(int i=0; i<n; i++){
            for(int j=0; j<m; j++){
                tmpMap[m-j-1][i] =  map[i][j];
            }
        }
        //배열 바꾸기
        int tmp = n;
        n = m;
        m= tmp;
 
        map = tmpMap;
 
 
    }
    public static void five(){
        //5번 연산 : 4그룹 1->2, 2->3, 3->4, 4->1
        tmpMap = new int[n][m];
        for(int i=0; i<n/2; i++){
            for(int j=0; j<m/2; j++){
                tmpMap[i][m/2+j] = map[i][j];
 
            }
        }
 
        // 0,4 -> 3,4
        for(int i=0; i<n/2; i++){
            for(int j=m/2; j<m; j++){
                tmpMap[n/2+i][j] = map[i][j];
            }
        }
        // 3,4 -> 3,0
        for(int i=n/2; i<n; i++){
            for(int j=m/2; j<m; j++){
                tmpMap[i][j-m/2= map[i][j];
            }
        }
        // 3,0 -> 0,0
        for(int i=n/2; i<n; i++){
            for(int j=0; j<m/2; j++){
                tmpMap[i-n/2][j] = map[i][j];
            }
        }
        map = tmpMap;
 
    }
    public static void six(){
        //          4그룹 1->2, 2->3, 3->4, 4->1
        //6번 연산 : 4그룹 1->4, 4->3, 3->2, 2->1
        tmpMap = new int[n][m];
        // 0,0 -> 3,0
        for(int i=0; i<n/2; i++){
            for(int j=0; j<m/2; j++){
                tmpMap[n/2+i][j] = map[i][j];
 
            }
        }
        
 
        // 3,0 -> 3,4
        for(int i=n/2; i<n; i++){
            for(int j=0; j<m/2; j++){
                tmpMap[i][j+m/2= map[i][j];
            }
        }
        // 3,4 -> 0,4
        for(int i=n/2; i<n; i++){
            for(int j=m/2; j<m; j++){
                tmpMap[i-n/2][j] = map[i][j];
            }
        }
       
 
        // 0,4 -> 0,0
        for(int i=0; i<n/2; i++){
            for(int j=m/2; j<m; j++){
                tmpMap[i][j-m/2= map[i][j];
            }
        }
        
        map = tmpMap;
 
    }
}
 
cs

 

 

728x90
반응형