반응형
    
    
    
  11967번: 불켜기
(1, 1)방에 있는 스위치로 (1, 2)방과 (1, 3)방의 불을 켤 수 있다. 그리고 (1, 3)으로 걸어가서 (2, 1)방의 불을 켤 수 있다. (2, 1)방에서는 다시 (2, 2)방의 불을 켤 수 있다. (2, 3)방은 어두워서 갈 수 없으
www.acmicpc.net
import java.io.*;
import java.util.*;
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int[] dx = new int[]{1, 0, -1, 0};
        int[] dy = new int[]{0, 1, 0, -1};
        int count = 1;
        String[] in = br.readLine().split(" ");
        int N = Integer.parseInt(in[0]);
        int M = Integer.parseInt(in[1]);
        Queue<int[]> Q = new LinkedList<>();
        LinkedList<int[]>[][] switchs = new LinkedList[N][N];
        for (int i=0; i<N; i++)
            for (int j=0; j<N; j++)
                switchs[i][j] = new LinkedList<>();
        // 방문
        boolean[][] visited = new boolean[N][N];
        // 불 켜짐
        boolean[][] lightOn = new boolean[N][N];
        // 이동가능
        boolean[][] moveable = new boolean[N][N];
        String[] temp = new String[M];
        while (M-->0) temp[M] = br.readLine();
        // 정렬 후 Queue에 각 자리를 맞춰서(-1) int배열로 넣기
        Arrays.stream(temp).forEach(item-> {
            int[] itemArr = Arrays.stream(item.split(" "))
                                .mapToInt(Integer::parseInt)
                                .map(i -> i - 1)
                                .toArray();
            switchs[itemArr[1]][itemArr[0]].add(new int[]{itemArr[3], itemArr[2]});
        });
        visited[0][0] = true;
        lightOn[0][0] = true;
        Q.offer(new int[]{0, 0});
        while(!Q.isEmpty()){
            int[] cur = Q.poll();
            // 이동가능 방 체크
            for (int dir=0; dir<4; dir++) {
                int nx = cur[1] + dx[dir];
                int ny = cur[0] + dy[dir];
                if (nx<0 || nx>=N || ny<0 || ny>=N) continue;
                moveable[ny][nx] = true;
            }
            // 스위치 누르기
            for (int[] s : switchs[cur[0]][cur[1]]){
                // 불이 꺼져있으면
                if (!lightOn[s[0]][s[1]]){
                    lightOn[s[0]][s[1]] = true;
//                    System.out.printf("(%d, %d)에서 (%d, %d)의 불을 켬\n", cur[1], cur[0], s[1], s[0]);
                    count++;
                    if (moveable[s[0]][s[1]] && !visited[s[0]][s[1]]){
                        visited[s[0]][s[1]] = true;
                        Q.offer(new int[]{s[0], s[1]});
                    }
                }
            }
            // 현재 위치에서 이동할 수 있는 방으로 이동
            for (int dir=0; dir<4; dir++) {
                int nx = cur[1] + dx[dir];
                int ny = cur[0] + dy[dir];
                if (nx<0 || nx>=N || ny<0 || ny>=N) continue;
                if (visited[ny][nx] || !lightOn[ny][nx] || !moveable[ny][nx]) continue;
                visited[ny][nx] = true;
                Q.offer(new int[]{ny, nx});
            }
        }
        System.out.print(count);
    }
}

반응형
    
    
    
  'Algorithm' 카테고리의 다른 글
| 백준 7795번 먹을 것인가 먹힐 것인가 [ Java ] (0) | 2021.01.05 | 
|---|---|
| 백준 5427번 불 [ Java ] (0) | 2021.01.05 | 
| 백준 10757번 큰 수 A+B [ Java ] (0) | 2021.01.04 | 
| 백준 3273번 두 수의 합 [ Java ] (0) | 2021.01.03 | 
| 백준 1431번 시리얼 번호 [ Java ] (0) | 2021.01.03 |