Algorithm

[Algo] 백준 15729 방탈출 JAVA

조핑구 2024. 5. 8. 13:48

문제 바로가기 : https://www.acmicpc.net/problem/15729

메모리 : 74000KB

시간 : 428ms

언어 : Java 11


코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

    public static <T> void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        int[] button = new int[N + 2];
        StringTokenizer st = new StringTokenizer(br.readLine());
        for (int i = 0; i < N; i++) {
            button[i] = Integer.parseInt(st.nextToken());
        }
        int ans = 0;
        for (int i = 0; i < N; i++) {
            if (button[i] == 1) {
                ans++;
                button[i + 1] = button[i + 1] == 1 ? 0 : 1;
                button[i + 2] = button[i + 2] == 1 ? 0 : 1;
            }
        }
        System.out.println(ans);
    }
}

 

풀이

버튼을 누르면 오른쪽 두 개의 상태도 바뀐다는 것이 포인트이다. 일정한 방향성을 가진 것이기 때문에 왼쪽으로 오른쪽으로 버튼을 눌러가면 된다.

1이 나올 때=이자리에 불을 켜야한다=버튼을 눌렀다! 오른쪽의 버튼을 반대로 바꿔가면 된다.