본문 바로가기
Problem Solving/Baekjoon

[백준] 28063 동전 복사 - Greedy / Java

by graycode 2023. 8. 23.

 문제 링크

 

28063번: 동전 복사

돈이 없어 슬픈 레이무는 어느 날, 한 기계를 발견했다. 이 기계는 한 변의 길이가 \(N\)인 정사각형 모양이고, \(1 \times 1\) 크기의 정사각형 칸들로 이루어져 있다. 각 칸의 위치는 좌표로 나타낼

www.acmicpc.net

 

 풀이 코드

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

public class Main {

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

        int n = read(), x = read(), y = read();

        if (n == 1) bw.write("0");
        else if ((x == 1 || x == n) && (y == 1 || y == n)) bw.write("2");
        else if (x == 1 || x == n || y == 1 || y == n) bw.write("3");
        else bw.write("4");

        bw.flush();
    }

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

}

댓글