Post

[JAVA]백준 17406번 배열 돌리기 4

[JAVA]백준 17406번 배열 돌리기 4

📌문제 링크


https://www.acmicpc.net/problem/17406

Image

📌문제 설명


깊은 복사를 주의하며 백트래킹에서 회전만 조심하면 되는 문제입니다. 배열 돌리기 1 문제에 백트래킹 개념만 추가되었습니다. https://gepetton.github.io/posts/No.16926/

📌코드


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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

// 회전 정보 클래스
class RotateInfo {
    int r, c, s;
    RotateInfo(int r, int c, int s) {
        this.r = r;
        this.c = c;
        this.s = s;
    }
}

public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static int N, M, K;
    static int[][] map = new int[51][51];
    static Boolean[] visited = new Boolean[7];
    static ArrayList<RotateInfo> rotateList = new ArrayList<>();
    static int answer = Integer.MAX_VALUE;

    public static void main(String[] args) throws Exception {
        input(); // 입력 받기
        solve(); // 문제 해결
    }

    // 입력을 받는 메서드
    public static void input() throws Exception {
        StringTokenizer st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        K = Integer.parseInt(st.nextToken());
        for (int i = 1; i <= N; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 1; j <= M; j++) {
                map[i][j] = Integer.parseInt(st.nextToken());
            }
        }
        for (int i = 0; i < K; i++) {
            st = new StringTokenizer(br.readLine());
            int r = Integer.parseInt(st.nextToken());
            int c = Integer.parseInt(st.nextToken());
            int s = Integer.parseInt(st.nextToken());
            RotateInfo info = new RotateInfo(r, c, s);
            rotateList.add(info);
        }
        br.close();
    }

    // 회전 연산을 수행하는 메서드
    public static void rotate(int idx) {
        if (idx == K) {
            answer = Math.min(answer, getMin());
            return;
        }
        for (int i = 0; i < K; i++) { // rotateList 대신 인덱스로 ��회
            if (visited[i]) continue;
            visited[i] = true;

            // 회전 전에 원래 맵을 복사
            int[][] copyMap = new int[51][51];
            for (int x = 1; x <= N; x++) {
                for (int y = 1; y <= M; y++) {
                    copyMap[x][y] = map[x][y];
                }
            }

            // 회전 연산 수행
            RotateInfo rotateInfo = rotateList.get(i);
            int r = rotateInfo.r;
            int c = rotateInfo.c;
            int s = rotateInfo.s;
            for (int j = 1; j <= s; j++) {
                int sr = r - j;
                int sc = c - j;
                int er = r + j;
                int ec = c + j;
                int temp = map[sr][sc];
                for (int x = sr; x < er; x++) {
                    map[x][sc] = map[x + 1][sc];
                }
                for (int y = sc; y < ec; y++) {
                    map[er][y] = map[er][y + 1];
                }
                for (int x = er; x > sr; x--) {
                    map[x][ec] = map[x - 1][ec];
                }
                for (int y = ec; y > sc + 1; y--) {
                    map[sr][y] = map[sr][y - 1];
                }
                map[sr][sc + 1] = temp;
            }

            // 다음 회전 연산 수행
            rotate(idx + 1);

            // 백트래킹: 원래 맵 복원
            for (int x = 1; x <= N; x++) {
                for (int y = 1; y <= M; y++) {
                    map[x][y] = copyMap[x][y];
                }
            }

            visited[i] = false;
        }
    }

    // 최소 값을 구하는 메서드
    public static int getMin() {
        int mn = Integer.MAX_VALUE;
        for (int i = 1; i <= N; i++) {
            int sum = 0;
            for (int j = 1; j <= M; j++) {
                sum += map[i][j];
            }
            mn = Math.min(mn, sum);
        }
        return mn;
    }

    // 문제를 해결하는 메서드
    public static void solve() {
        for (int i = 0; i < 7; i++) {
            visited[i] = false;
        }
        rotate(0);
        System.out.println(answer);
    }
}
This post is licensed under CC BY 4.0 by the author.