본문 바로가기
Problem Solving/Baekjoon

[백준] 26169 세 번 이내에 사과를 먹자 - Graph Theory / Java

by graycode 2023. 10. 15.

 문제 링크

 

26169번: 세 번 이내에 사과를 먹자

5 x 5 크기의 보드가 주어진다. 보드는 1 x 1 크기의 정사각형 격자로 이루어져 있다. 보드의 격자는 사과가 1개 있는 격자, 장애물이 있는 격자, 빈칸으로 되어 있는 격자로 구분된다. 격자의 위치

www.acmicpc.net

 

 풀이 코드

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class Main {

    static int[][] mat = new int[5][5];
    static boolean[][] visit = new boolean[5][5];
    static int[] dy = {-1, 1, 0, 0}, dx = {0, 0, -1, 1};

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        for (int i = 0; i < 5; i++) for (int j = 0; j < 5; j++) mat[i][j] = read();

        bw.write(dfs(read(), read(), 0, 0) ? "1" : "0");
        bw.flush();
    }

    private static boolean dfs(int y, int x, int depth, int cnt) {
        if (mat[y][x] == 1) cnt++;
        if (cnt == 2) return true;
        if (depth == 3) return false;

        visit[y][x] = true;
        for (int i = 0; i < 4; i++) {
            int ny = y + dy[i], nx = x + dx[i];
            if (ny < 0 || ny >= 5 || nx < 0 || nx >= 5 || visit[ny][nx] || mat[ny][nx] == 131) continue;

            if (dfs(ny, nx, depth + 1, cnt)) return true;
        }

        return false;
    }

    private static int read() throws IOException {
        int c, n = System.in.read() & 15;
        while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);

        return n;
    }

}

댓글