본문 바로가기
Problem Solving/Baekjoon

[백준] 31575 도시와 비트코인 - Graph Theory / Java

by graycode 2024. 4. 6.

 문제 링크

 

31575번: 도시와 비트코인

전날에 비해 비트코인의 시세가 백만원이나 오른 어느 아침, 진우는 거래소에 가서 비트코인을 매도하려고 한다. 현재 비트코인의 시세가 점점 떨어지고 있기 때문에 진우는 최대한 빨리 거래

www.acmicpc.net

 

 풀이 코드

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayDeque;
import java.util.Queue;

public class Main {

    static int n, m;
    static boolean[][] mat;

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

        n = read();
        m = read();
        mat = new boolean[m][n];
        for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) mat[i][j] = read() == 1;

        bw.write(bfs() ? "Yes" : "No");
        bw.flush();
    }

    private static boolean bfs() {
        Queue<Pair> q = new ArrayDeque<>();
        q.offer(new Pair(0, 0));
        mat[0][0] = true;

        n--;
        m--;
        while (!q.isEmpty()) {
            Pair cur = q.poll();
            if (cur.y == m && cur.x == n) return true;

            int ny = cur.y + 1, nx = cur.x + 1;
            if (ny <= m && mat[ny][cur.x]) {
                q.offer(new Pair(ny, cur.x));
                mat[ny][cur.x] = false;
            }

            if (nx <= n && mat[cur.y][nx]) {
                q.offer(new Pair(cur.y, nx));
                mat[cur.y][nx] = false;
            }
        }

        return false;
    }

    private static class Pair {
        int y, x;

        Pair(int y, int x) {
            this.y = y;
            this.x = x;
        }
    }

    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;
    }

}

댓글