말하는 컴공감자의 텃밭

백준 15721번 번데기 S5 - Brute force 본문

알고리즘/Backjoon - Java

백준 15721번 번데기 S5 - Brute force

현콩 2024. 1. 15. 16:38
728x90

백준 15721번 번데기 S5

 

단순 구현문제이다.

다만 어이없게 실수해서 1시간 반을 헤맸다...  열받넹 ^<^

ㅋㅋㅋ

i ++ 어디감 ㅋㅋ

 

뻔 -> 0, 데기 -> 1

로 사람의 위치를 구현해줬다. 물론 % 연산으로도 가능하다.

 

 

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
import java.util.*;
import java.io.*;
 
public class Main {// Boj_15721_번데기
    // 큐로 구현, 완전 탐색 input 10000이므로
    static int answer, T, B, time_bb, time_dg;
    static Queue<Integer> que;
    static int[] action;
 
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int A = Integer.parseInt(br.readLine());// 사람 수
        T = Integer.parseInt(br.readLine());// T번째 뻔, 데기
        B = Integer.parseInt(br.readLine());// 0 -> 뻔, 1-> 데기
        action = new int[A];
        que = new LinkedList<>();
 
        answer = 0; time_bb = 0; time_dg = 0;
        for (int i = 0; i < A; i++) {
            que.add(i);
        }
 
        game();
 
        System.out.println(answer);
 
    }
 
    public static void game() {
        int idx = 2;
        loop1: while (true) { // 회차
            int temp = 0;
            
            for (int i = 0; i < 2; i++) { // 뻔 데기 * 2
                temp = cycle();
//                System.out.println("뻔 ");
                time_bb++;
                if(chk()) {
                    answer = temp;
                    break loop1;
                }
                temp = cycle();
//                System.out.println("데기 ");
                time_dg++;
                if(chk()) {
                    answer = temp;
                    break loop1;
                }
            }
 
            for (int i = 0; i < idx; i++) { // 뻔 * n
                temp = cycle();
//                System.out.println("뻔 반복 ");
                time_bb++;
                if(chk()) {
                    answer = temp;
                    break loop1;
                }
            }
            for (int i = 0; i < idx; i++) { // 데기 * n
                temp = cycle();
//                System.out.println("데기 반복");
                time_dg++;
                if(chk()) {
                    answer = temp;
                    break loop1;
                }
            }
            idx ++;
//            System.out.println("회차 :" + (idx-1));
        }
        return;
    }
 
    public static int cycle() { // 순환하도록
        int front = que.poll();
        que.add(front);
//        System.out.printf(front +"번째 사람 차례 ");
        return front;
    }
    
    public static boolean chk() { // 뻔, 데기 구분
        if(B == 0 && T == time_bb) {
//            System.out.println("---뻔 " + time_bb+"번째");
            return true;
        }
        if(B == 1 && T == time_dg) {
//            System.out.println("---데기 " + time_dg+"번째");
            return true;
        }
        return false;
    }
}
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
import java.util.*;
import java.io.*;
 
public class Main {// Boj_15721_번데기
    // 숫자 %연산으로 , 완전 탐색 input 10000이므로
    static int answer, A, T, B, time_bb, time_dg;
 
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        A = Integer.parseInt(br.readLine());// 사람 수
        T = Integer.parseInt(br.readLine());// T번째 뻔, 데기
        B = Integer.parseInt(br.readLine());// 0 -> 뻔, 1-> 데기
 
        answer = 0;
        time_bb = 0;
        time_dg = 0;
        game();
 
        if (answer == 0) {
            answer = A - 1;
        } else {
            answer -= 1;
        }
        System.out.println(answer);
 
    }
 
    public static void game() {
        int idx = 2;
        loop1: while (true) { // 회차
            for (int i = 0; i < 2; i++) { // 뻔 데기 * 2
//                System.out.println("뻔");
                time_bb++;
                if (chk()) {
                    break loop1;
                }
//                System.out.println("데기");
                time_dg++;
                if (chk()) {
                    break loop1;
                }
            }
            for (int i = 0; i < idx; i++) { // 뻔 * n
//                System.out.println("뻔");
                time_bb++;
                if (chk()) {
                    break loop1;
                }
            }
            for (int i = 0; i < idx; i++) { // 데기 * n
//                System.out.println("데기");
                time_dg++;
                if (chk()) {
                    break loop1;
                }
            }
            idx++;
        }
        answer = (time_bb + time_dg) % A;
        return;
    }
 
    public static boolean chk() { // 뻔, 데기 구분
        if (B == 0 && T == time_bb) {
//            System.out.println("---뻔 " + time_bb+"번째");
            return true;
        }
        if (B == 1 && T == time_dg) {
//            System.out.println("---데기 " + time_dg+"번째");
            return true;
        }
        return false;
    }
}
cs

 

728x90
Comments