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
 
public class Main{
    private static int N;
    private static int M;
    private static int[][] map;
    private static ArrayList<Pos> virusPos;
    private static int result;
    private static int[] dx = { 010-1 };
    private static int[] dy = { 10-10 };
    private static boolean check;
    private static Pos[] set;
 
 
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine(), " ");
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken()); // 바이러스 놓을 수 있는 수
        map = new int[N][N];
        virusPos = new ArrayList<Pos>();
        set = new Pos[M];
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine(), " ");
            for (int j = 0; j < N; j++) {
                map[i][j] = Integer.parseInt(st.nextToken());
                if (map[i][j] == 2) {
                    map[i][j] = 0;
                    virusPos.add(new Pos(i, j));
                }
                if(map[i][j] == 1) map[i][j] = -1;
            }
        }
        result = Integer.MAX_VALUE;
        check = false;
        combination(00);
        if(result == Integer.MAX_VALUE) System.out.println("-1");
        else System.out.println(result);
 
    }// end of main
 
    
 
    public static void combination(int k, int len) {
        if (len == M) {
            process();
            return;
        } else {
            for (int i = k; i < virusPos.size(); i++) {
                set[len] = new Pos(virusPos.get(i).x, virusPos.get(i).y);
                combination(i + 1, len + 1);
            }
        }
    }
 
    public static void process() {
        Queue<Pos> queue = new LinkedList<Pos>();
        int[][] temp = new int[N][N];
        boolean[][] visited = new boolean[N][N];
        for (int i = 0; i < temp.length; i++) {
            System.arraycopy(map[i], 0, temp[i], 0, N);
        }
 
        for (int i = 0; i < set.length; i++) {
            temp[set[i].x][set[i].y] = -2;
            visited[set[i].x][set[i].y] = true;
            queue.add(new Pos(set[i].x, set[i].y));
        }
//        print(temp);
        int value = 1;
        while (!queue.isEmpty()) {
            int size = queue.size();
 
            for (int i = 0; i < size; i++) {
                Pos p = queue.poll();
                for (int j = 0; j < dx.length; j++) {
                    int nx = p.x + dx[j];
                    int ny = p.y + dy[j];
                    
                    if(nx >=0 && ny >=0 && nx < N && ny < N && map[nx][ny] !=-1 && !visited[nx][ny]) {
                        visited[nx][ny] = true;
                        temp[nx][ny] = value;
                        queue.add(new Pos(nx, ny));
                    }
                }
            }
            value++;
            
        }
//        print(temp);
        int time = 0;
        label :for (int j = 0; j < temp.length; j++) {
            for (int i = 0; i < temp.length; i++) {
                if(temp[i][j] == 0) {
                    check = false;
                    break label;
                }else {
                    if(time  < temp[i][j]) time = temp[i][j];
                    check = true;
                }
                
            }
        }
        if(check) {
            result = result > time ? time :result;
        }
        else return;
        
    }
 
    private static void print(int[][] temp) {
        // TODO Auto-generated method stub
        for (int i = 0; i < temp.length; i++) {
            for (int j = 0; j < temp.length; j++) {
                System.out.print(temp[i][j]);
            }
            System.out.println();
        }
        
        System.out.println("========================");
    }
 
    static class Pos {
        int x;
        int y;
 
        public Pos(int x, int y) {
            super();
            this.x = x;
            this.y = y;
        }
 
        @Override
        public String toString() {
            return "Pos [x=" + x + ", y=" + y + "]";
        }
 
    }
}// end of class
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

Class에 상태변화 변수를 줘서 큐 돌릴 때 true만 돌림

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
 
public class Main{
    private static int N;
    private static int M;
    private static int[][] map;
    private static ArrayList<Pos> list;
    private static int[] dx = { -1010 };
    private static int[] dy = { 010-1 };
    private static Pos[] set;
    private static int result;
    private static boolean virusCheck;
 
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine(), " ");
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        map = new int[N][N];
        set = new Pos[M];
        list = new ArrayList<Pos>();
        for (int i = 0; i < map.length; i++) {
            st = new StringTokenizer(br.readLine(), " ");
            for (int j = 0; j < map.length; j++) {
                map[i][j] = Integer.parseInt(st.nextToken());
                if (map[i][j] == 2) {
                    list.add(new Pos(i, j));
                    map[i][j] = -3// 바이러스
                }
                if (map[i][j] == 1)
                    map[i][j] = -1// 벽
            }
        }
        result = Integer.MAX_VALUE;
        combination(000, map);
        if (result == Integer.MAX_VALUE)
            System.out.println("-1");
        else {
            System.out.println(result);
        }
    }// end of main
 
    public static void combination(int k, int idx, int cnt, int[][] map) {
        if (idx == M) {
            process();
            return;
        } else {
            for (int i = k; i < list.size(); i++) {
                set[idx] = new Pos(list.get(i).x, list.get(i).y);
                combination(i + 1, idx + 1, cnt, map);
            }
        }
    }
 
    public static void process() {
        Queue<Pos> queue = new LinkedList<>();
        int[][] temp = new int[N][N];
        boolean[][] visited = new boolean[N][N];
        for (int i = 0; i < map.length; i++) {
            System.arraycopy(map[i], 0, temp[i], 0, N);
        }
 
        for (int i = 0; i < set.length; i++) {
            visited[set[i].x][set[i].y] = true;
            queue.add(new Pos(set[i].x, set[i].y, true));
        }
//        print(temp);
 
        int time = 1;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int k = 0; k < size; k++) {
                Pos p = queue.poll();
                if (p.condition) {
 
                    for (int i = 0; i < dx.length; i++) {
                        int nx = p.x + dx[i];
                        int ny = p.y + dy[i];
                        if (nx >= 0 && ny >= 0 && nx < N && ny < N && !visited[nx][ny] && temp[nx][ny] != -1) {
                            if(temp[nx][ny] == -3) {
                                temp[nx][ny] = -3;
                                visited[nx][ny] = true;
                                queue.add(new Pos(nx, ny, true));
                            }else if(temp[nx][ny] == 0) {
                                
                                temp[nx][ny] = time;
                                visited[nx][ny] = true;
                                queue.add(new Pos(nx, ny,true));
                            }
 
                        }
                    }
                }
            }
            time++;
        }
 
//        print(temp);
        int value = 0;
        for (int i = 0; i < temp.length; i++) {
            for (int j = 0; j < temp.length; j++) {
                if (temp[i][j] == 0) {
                    return;
                } else {
                    if (temp[i][j] > value)
                        value = temp[i][j];
                }
            }
        }
 
        result = result > value ? value : result;
    }
 
    public static void print(int[][] temp) {
        for (int i = 0; i < temp.length; i++) {
            for (int j = 0; j < temp.length; j++) {
                System.out.print(temp[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println("==========================================");
    }
 
    public static int VirusCount() {
        return 0;
    }
 
    static class Pos {
        int x;
        int y;
        boolean condition;
 
        public Pos(int x, int y) {
            super();
            this.x = x;
            this.y = y;
        }
 
        public Pos(int x, int y, boolean condition) {
            super();
            this.x = x;
            this.y = y;
            this.condition = condition;
        }
 
        @Override
        public String toString() {
            return "Pos [x=" + x + ", y=" + y + "]";
        }
 
    }
 
}// end of class
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

배열 이동시키는게 중요

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
 
public class Main{
    private static int R;
    private static int C;
    private static int T;
    private static int[][] map;
    private static int downCleanerX;
    private static int upCleanerX;
    private static int result;
    private static int[] dx = { -1010 };
    private static int[] dy = { 010-1 };
    private static ArrayList<Dust> list;
 
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine(), " ");
        R = Integer.parseInt(st.nextToken());
        C = Integer.parseInt(st.nextToken());
        T = Integer.parseInt(st.nextToken());
        map = new int[R][C];
        downCleanerX = 0;
        upCleanerX = 0;
        list = new ArrayList<>();
        for (int i = 0; i < R; i++) {
            st = new StringTokenizer(br.readLine(), " ");
            for (int j = 0; j < C; j++) {
                map[i][j] = Integer.parseInt(st.nextToken());
                if (map[i][j] == -1) downCleanerX = i;
            }
        }
        upCleanerX = downCleanerX - 1;
        result = 0;
        process();
        System.out.println(result);
    }// end of main
 
    public static void process() {
        while (true) {
            T--;
            //확산가능한곳 찾기
            findDust();
            // 미세먼지확산
            spread();
            // 공기 청정기 작동
            cleaner();
 
            if (T == 0) {
                check();
                break;
            }
        }
    }
    private static void check() {
        for (int i = 0; i < R; i++) {
            for (int j = 0; j < C; j++) {
                if(map[i][j] > 0) result += map[i][j];
            }
        }
    }
 
    public static void findDust() {
        list = new ArrayList<Dust>();
        for (int i = 0; i < R; i++) {
            for (int j = 0; j < C; j++) {
                if(map[i][j] > 0) list.add(new Dust(i,j ,map[i][j]));
            }
        }
    }
 
    public static void cleaner() {
        for (int i = upCleanerX ; i >0 ; i--) {
            map[i][0= map[i-1][0];
        }
        map[upCleanerX][0= 0;
//        print();
        for (int i = 0; i < C - 1; i++) {
            map[0][i] = map[0][i + 1];
        }
//        print();
        for (int i = 0; i < upCleanerX; i++) {
            map[i][C-1= map[i + 1][C-1];
        }
//        print();
        for (int i = C - 1; i >= 1; i--) {
            map[upCleanerX][i] = map[upCleanerX][i - 1];
        }
//        print();
        map[upCleanerX][0= -1;
//        print();
        //아래 시계방향
        for (int i = downCleanerX; i < R-1 ; i++) {
            map[i][0= map[i+1][0];
        }
        map[downCleanerX][0=0;
//        print();
        for (int i = 0; i < C - 1; i++) {
            map[R-1][i] = map[R-1][i + 1];
        }
//        print();
        for (int i = R-1; i > downCleanerX; i--) {
            map[i][C-1= map[i-1][C-1];
        }
//        print();
        for (int i = C - 1; i >= 1; i--) {
            map[downCleanerX][i] = map[downCleanerX][i - 1];
        }
//        print();
        map[downCleanerX][0= -1;
//        print();
    }
 
    public static void spread() {
        for (Dust d : list) {
            int count = 0;
            for (int i = 0; i < dx.length; i++) {
                int nx = d.x + dx[i];
                int ny = d.y + dy[i];
                if(nx>=0 && ny>=0 && nx <&& ny <&&map[nx][ny]>=0) {
                    count++;
                    map[nx][ny] += d.size /5
                }
            }
            map[d.x][d.y]= map[d.x][d.y]- (d.size/5* count;  
//            print();
        }
    }
    public static void print() {
        for (int i = 0; i < R; i++) {
            for (int j = 0; j < C; j++) {
                System.out.print(map[i][j] + " ");
            }
            System.out.println();
        }
        
        System.out.println("==========================");
    }
 
    static class Dust {
        int x;
        int y;
        int size;
 
        public Dust(int x, int y, int size) {
            super();
            this.x = x;
            this.y = y;
            this.size = size;
        }
 
        @Override
        public String toString() {
            return "dust [x=" + x + ", y=" + y + ", size=" + size + "]";
        }
 
    }
}// end of class
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

정렬이 중요

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
 
public class Main{
    private static int M;
    private static int[][] map;
    private static int C;
    private static int R;
    private static int result;
    private static ArrayList<Info> list;
 
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine(), " ");
        R = Integer.parseInt(st.nextToken());
        C = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
 
        list = new ArrayList<Info>();
 
        for (int k = 0; k < M; k++) {
            st = new StringTokenizer(br.readLine(), " ");
            int r = Integer.parseInt(st.nextToken());
            int c = Integer.parseInt(st.nextToken());
            int s = Integer.parseInt(st.nextToken()); // 속력
            int d = Integer.parseInt(st.nextToken()); // 방향
            int z = Integer.parseInt(st.nextToken()); // 크기
 
            list.add(new Info(r, c, s, d, z));
 
        }
        result = 0;
        Collections.sort(list);
        process();
        System.out.println(result);
 
    }// end of main
 
    public static void process() {
        int fisherIndex = 0;
        
        while (fisherIndex != C) {
            fisherIndex++;
            for (Info info : list) {
                if (fisherIndex == info.c) {
                    result += info.z;
                    list.remove(info);
                    break;
                }
            }
 
            map = new int[R+1][C+1];
            move();
            Collections.sort(list);
            for (int i = list.size()-1; i>=0; i--) {
                if(map[list.get(i).r][list.get(i).c] != list.get(i).z) list.remove(i);
            }
 
        }
    }
 
    public static void move() {
        // 1~4 상하 우좌
 
        for (Info info : list) {
            int count = 0;
//            System.out.println(info.toString());
            while (count != info.s) {
                count++;
                switch (info.d) {
                case 1:
                    if (info.r > 1) {
                        info.r = info.r - 1;
                        continue;
 
                    } else {
                        count--;
                        info.d = 2;
                    }
                    break;
                case 2:
                    if (info.r < R) {
                        info.r = info.r + 1;
                        continue;
                    } else {
                        count--;
                        info.d = 1;
                    }
                    break;
                case 3:
                    if (info.c < C) {
                        info.c = info.c + 1;
                        continue;
                    } else {
                        count--;
                        info.d = 4;
                    }
                    break;
 
                case 4:
                    if (info.c > 1) {
                        info.c = info.c - 1;
                        continue;
                    } else {
                        count--;
                        info.d = 3;
                    }
                    break;
 
                default:
                    break;
                }
            }
            if(map[info.r][info.c] < info.z) map[info.r][info.c] = info.z;
 
 
        }
    }
 
    public static class Info implements Comparable<Info> {
        int r;
        int c;
        int s;// 속력
        int d;// 방향
        int z;// 크기
 
        public Info(int r, int c, int s, int d, int z) {
            super();
            this.r = r;
            this.c = c;
            this.s = s;
            this.d = d;
            this.z = z;
        }
 
        @Override
        public int compareTo(Info o) {
            if (o.c != this.c) {
                return this.c - o.c;
            } else if (o.c == this.c && o.r != this.r) {
                return this.r - o.r;
            }
            return o.z- this.z;
        }
 
        @Override
        public String toString() {
            // TODO Auto-generated method stub
            return "r:" + r +" c : " + c + " 속력 " + s +" 방향" + d + " 크기" + z ;
        }
        
    }
}// end of class
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

1. 먹을 수 있는곳 탐색
2. 1개 일 때 : 바로 거리 계산
3. 2개 이상 : 정렬을 통해 처음꺼를 거리차이 계산함

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
 
public class Main16236_아기상어 {
    private static int N;
    private static int[][] map;
    private static Queue<Shark> queue;
    private static int[] dx = { -1010 };
    private static int[] dy = { 010-1 };
    private static boolean[][] visited;
    private static ArrayList<Shark> list;
    private static int ateFish;
    private static int result;
    
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(br.readLine().trim());
        map = new int[N][N];
        visited = new boolean[N][N];
        queue = new LinkedList<>();
        for (int i = 0; i < N; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine(), " ");
            for (int j = 0; j < N; j++) {
                map[i][j] = Integer.parseInt(st.nextToken());
                if (map[i][j] == 9) {
                    visited[i][j] = true;
                    map[i][j] = 0;
                    queue.add(new Shark(i, j, 20));
                }
            }
        }
        ateFish = 0;
        result = 0;
        process();
        System.out.println(result);
 
    }// end of main
    public static void process() {
        while (true) {
 
            int numFish = findEatFish(); //먹을수있는곳 찾음 
            if (numFish == 0)
                return;
 
            else if (numFish == 1) { //하나일때는 바로먹음
                result += list.get(0).diffLen;
                map[list.get(0).x][list.get(0).y] = 0;
                ateFish++;
                if (ateFish == list.get(0).volum) {
                    queue.add(new Shark(list.get(0).x, list.get(0).y, list.get(0).volum+10));
                    ateFish= 0;
                }
                else
                    queue.add(new Shark(list.get(0).x, list.get(0).y, list.get(0).volum, 0));
            } else { //먹을수있는게 2개이상일때는 정렬해서 처음꺼 먹음
                Collections.sort(list);
                result += list.get(0).diffLen;
                map[list.get(0).x][list.get(0).y] = 0;
                ateFish++;
                if (ateFish == list.get(0).volum) {
                    ateFish= 0;
                    queue.add(new Shark(list.get(0).x, list.get(0).y, list.get(0).volum+10));
                    
                }
                else
                    queue.add(new Shark(list.get(0).x, list.get(0).y, list.get(0).volum, 0));
            }
        }
    }
 
    public static int findEatFish() {
        int numFish = 0;
        list = new ArrayList<Shark>();
        visited = new boolean[N][N];
        while (!queue.isEmpty()) {
            Shark s = queue.poll();
            visited[s.x][s.y] = true;
            for (int i = 0; i < dx.length; i++) {
                int nx = s.x + dx[i];
                int ny = s.y + dy[i];
                if (inRange(nx, ny) && !visited[nx][ny] && (s.volum >= map[nx][ny] || map[nx][ny] == 0)) {
 
                    if (s.volum == map[nx][ny] || map[nx][ny] == 0) {
                        visited[nx][ny] = true;
                        queue.add(new Shark(nx, ny, s.volum, s.diffLen + 1));
                    } else {
                        visited[nx][ny] = true;
                        numFish++;
 
                        list.add(new Shark(nx, ny, s.volum, s.diffLen + 1));
                        queue.add(new Shark(nx, ny, s.volum, s.diffLen + 1));
                    }
                }
            }
        }
        return numFish;
    }
    public static boolean inRange(int x, int y) {
        if (x >= 0 && y >= 0 && x < N && y < N)
            return true;
        return false;
    }
 
    static class Shark implements Comparable<Shark> {
        int x;
        int y;
        int volum;
        int diffLen;
 
        public Shark(int x, int y, int volum, int diffLen) {
            super();
            this.x = x;
            this.y = y;
            this.volum = volum;
            this.diffLen = diffLen;
        }
 
        @Override
        public String toString() {
            return "Shark [x=" + x + ", y=" + y + ", volum=" + volum + ", diffLen=" + diffLen + "]";
        }
 
        @Override
        public int compareTo(Shark o) {
            if (this.diffLen == o.diffLen && this.x == o.x) {
                return this.y - o.y;
            } else if (this.diffLen == o.diffLen) {
                return this.x - o.x;
            }
 
            return this.diffLen - o.diffLen;
 
        }
 
    }
}// end of class
 
 
 
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs
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
 
public class Main{
    private static int N;
    private static int[][] arr;
    private static int result;
 
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(br.readLine());
        arr = new int[N+1][2];
        
        for (int i = 1; i < arr.length; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine()," ");
            for (int j = 0; j < 2; j++) {
                arr[i][j] = Integer.parseInt(st.nextToken());
            }
        }
        
        
        result = Integer.MIN_VALUE;
        process(1,0);
        System.out.println(result);
    }//end of main
    
    public static void process(int index, int value) {
        if(index > N) {
            if(result < value) result = value;
            return;
        }
        
        for (int i = index; i < arr.length; i++) {
            int k = i + arr[i][0];
            int num = arr[i][1];
            if(k-1 <= N) {
                process(i + arr[i][0] , value + arr[i][1]);
            }else {
                process(i+arr[i][0], value);
            }
        }
    }
    
}//end of class
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

조합 2번 사용 : 1. 팀을 N/2 만큼 나누고 그 팀을 2명씩 또 조합해서 나누어줌

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
 
public class Main{
    private static int[][] ability;
    private static int N;
    private static int result;
    private static int[] linkTeam;
    private static int[] arr;
    private static int[] startTeam;
    private static int[] startPair;
    private static int[] linkPair;
    private static int startSumValue;
    private static int linkSumValue;
 
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(br.readLine());
        ability = new int[N][N];
 
        for (int i = 0; i < N; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            for (int j = 0; j < N; j++) {
                ability[i][j] = Integer.parseInt(st.nextToken());
            }
        }
        result = Integer.MAX_VALUE;
        startSumValue = 0;
        linkSumValue = 0;
        arr = new int[N];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = i;
        }
 
        linkTeam = new int[N / 2];
        splitTeam(00);
        System.out.println(result);
    }// end of main
 
    public static void splitTeam(int k, int len) {
        if (len == N / 2) {
 
            process();
            if(result > Math.abs(linkSumValue - startSumValue)) {
                result = Math.abs(linkSumValue - startSumValue);
 
            }
            startSumValue =0;
            linkSumValue =0;
            
            return;
        }
 
        for (int i = k; i < arr.length; i++) {
            linkTeam[len] = arr[i];
            splitTeam(i + 1, len + 1);
        }
 
    }
 
    public static void process() {
        boolean[] visited = new boolean[N];
        for (int i = 0; i < linkTeam.length; i++) {
            visited[linkTeam[i]] = true;
        }
        int index = 0;
        startTeam = new int[N/2];
        for (int i = 0; i < visited.length; i++) {
            if(!visited[i]) {
                startTeam[index++= i;
            }
        }
        
        
        startPair = new int[2];
        linkPair = new int[2];
        splitPair(0,0);    
        
        
    }
    
    public static void splitPair(int k, int len) {
        if(len == 2) {
            result(startPair, linkPair);
            return;
        }
        for (int i = k; i < startTeam.length; i++) {
            startPair[len] =startTeam[i];
            linkPair[len] = linkTeam[i];
            splitPair(i+1, len+1);
        }
    }
 
    private static void result(int[] start, int[] link) {
        for (int i = 0; i < link.length-1; i++) {
            startSumValue += ability[start[i]][start[i+1]] + ability[start[i+1]][start[i]];
            linkSumValue += ability[link[i]][link[i+1]] + ability[link[i+1]][link[i]];
        }
        
        
 
    }
 
}// end of class
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

재귀 끝나는 조건 설정해주는데 생각이 잘안나서 오래걸렸다.

CCTV 1~5 --> 1. 북동남서 다 검사

                   2. 북남 == 남북 검사가 똑같으니깐 한번만 해주면 된다. 이런식으로 3 , 4, 5 번에 대해서도 처리해줌

 

 

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
202
203
204
 
 
public class Main15683_{
    private static int M;
    private static int N;
    private static int[][] map;
    private static ArrayList<Info> list;
    private static int size;
    private static int result;
 
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine(), " ");
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        map = new int[N][M];
        list = new ArrayList<Info>();
        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());
                if (map[i][j] != 0 && map[i][j] != 6)
                    list.add(new Info(i, j, map[i][j]));
            }
        }
        size = list.size();
        result = Integer.MAX_VALUE;
        dfs(0);
        System.out.println(result);
 
    }// end of main
 
    public static void dfs(int k) {
        if (k == size) {
            int num = check();
            if (result > num)
                result = num;
            return;
        }
 
        Info info = list.get(k);
        int nx = info.x;
        int ny = info.y;
        int nType = info.type;
 
        int[][] temp = new int[N][M];
        switch (nType) {
        case 1:
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < temp.length; j++) {
                    System.arraycopy(map[j], 0, temp[j], 0, M);
                }
                spread(i, nx, ny);
                dfs(k + 1);
 
                for (int j = 0; j < temp.length; j++) {
                    System.arraycopy(temp[j], 0, map[j], 0, M);
                }
            }
            break;
        case 2:
            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < temp.length; j++) {
                    System.arraycopy(map[j], 0, temp[j], 0, M);
                }
                spread(i, nx, ny);
                spread(i + 2, nx, ny);
                dfs(k + 1);
                for (int j = 0; j < temp.length; j++) {
                    System.arraycopy(temp[j], 0, map[j], 0, M);
                }
            }
            break;
        case 3:
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < temp.length; j++) {
                    System.arraycopy(map[j], 0, temp[j], 0, M);
                }
                spread(i, nx, ny);
                spread((i + 1) % 4, nx, ny); // 동북 경우 회전큐 마냥 해결
                dfs(k + 1);
                for (int j = 0; j < temp.length; j++) {
                    System.arraycopy(temp[j], 0, map[j], 0, M);
                }
 
            }
            break;
        case 4:
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < temp.length; j++) {
                    System.arraycopy(map[j], 0, temp[j], 0,M);
                }
 
                spread(i, nx, ny);
                spread((i + 1) % 4, nx, ny);
                spread((i + 2) % 4, nx, ny);
                dfs(k + 1);
 
                for (int j = 0; j < temp.length; j++) {
                    System.arraycopy(temp[j], 0, map[j], 0, M);
                }
 
            }
            break;
        case 5:
            for (int j = 0; j < temp.length; j++) {
                System.arraycopy(map[j], 0, temp[j], 0, M);
            }
            spread(0, nx, ny);
            spread(1, nx, ny);
            spread(2, nx, ny);
            spread(3, nx, ny);
            dfs(k + 1);
 
            for (int j = 0; j < temp.length; j++) {
                System.arraycopy(temp[j], 0, map[j], 0, M);
            }
            break;
 
        default:
            break;
        }
    }
 
    public static void spread(int dir, int x, int y) {
//        dir 0~3 : 북 서 남 동
        if (dir == 0) {
            int nx = x - 1;
            if (nx < 0)
                return;
            if (map[nx][y] == 6)
                return;
            else {
                map[nx][y] = -1;
                spread(dir, nx, y);
 
            }
 
        } else if (dir == 1) {
            int ny = y - 1;
            if (ny < 0)
                return;
            if (map[x][ny] == 6)
                return;
            else {
                map[x][ny] = -1;
                spread(dir, x, ny);
 
            }
        } else if (dir == 2) {
            int nx = x + 1;
            if (nx >= N)
                return;
            if (map[nx][y] == 6)
                return;
            else {
                map[nx][y] = -1;
                spread(dir, nx, y);
 
            }
        } else {
            int ny = y + 1;
            if (ny >= M)
                return;
            if (map[x][ny] == 6)
                return;
            else {
                map[x][ny] = -1;
                spread(dir, x, ny);
 
            }
        }
    }
 
    public static int check() {
        int checkNum = 0;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if (map[i][j] == 0) {
                    checkNum++;
                }
            }
        }
        return checkNum;
    }
 
    public static class Info {
        int x;
        int y;
        int type;
 
        public Info(int x, int y, int type) {
            super();
            this.x = x;
            this.y = y;
            this.type = type;
        }
 
    }
}// end of class
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

+ Recent posts