베오
DCode
베오
전체 방문자
오늘
어제
  • 분류 전체보기 (218)
    • 공지사항 (1)
    • 잡설 (1)
    • Programming (33)
      • [C] (1)
      • [Java] (4)
      • [Python] (2)
      • [Android] (2)
      • [Network] (0)
      • [Operation System] (2)
      • [Spring Boot] (22)
      • [Docker] (0)
    • Algorithm (31)
      • 자료구조 (2)
      • 알고리즘 (Java) (14)
      • 알고리즘 (기초) (15)
    • Coding Test (131)
      • BOJ (131)
      • Algospat (0)
    • 이론적인거 (14)
      • 보안 (5)
      • 오류 해결 (2)
      • 디자인 패턴 (5)
      • 네트워크 (1)
      • 기타 (1)
    • 최신기술 (4)
      • 블록체인 (1)
    • [Project] (1)

블로그 메뉴

  • 🐈‍⬛ GitHub
  • 📫 방명록
  • 🔖 태그

공지사항

인기 글

티스토리

hELLO · Designed By 정상우.
베오

DCode

Coding Test/BOJ

[BOJ/JAVA] 9205번: 맥주 마시면서 걸어가기

2022. 8. 21. 10:32

https://www.acmicpc.net/problem/9205

 

9205번: 맥주 마시면서 걸어가기

송도에 사는 상근이와 친구들은 송도에서 열리는 펜타포트 락 페스티벌에 가려고 한다. 올해는 맥주를 마시면서 걸어가기로 했다. 출발은 상근이네 집에서 하고, 맥주 한 박스를 들고 출발한다.

www.acmicpc.net

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class java_9205 {
    final static int INF = Integer.MAX_VALUE;
    static int board[][];
    static Coordinate coordinateList[];

    static class Coordinate {
        int y;
        int x;

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

        @Override
        public String toString() {
            return "Coordinate [x=" + x + ", y=" + y + "]";
        }

    }

    static boolean bfs(int n) {
        boolean result = false;

        boolean isVisited[] = new boolean[n + 2];

        Queue<Integer> q = new LinkedList<>();

        q.add(0);
        isVisited[0] = true;

        while (!q.isEmpty()) {
            int here = q.poll();

            if (here == n + 1) {
                result = true;
                break;
            }

            // System.out.println(coordinateList[here]);

            for (int i = 1; i < board.length; i++) {
                if (!isVisited[i]) {
                    if (board[here][i] <= 1000) {
                        q.add(i);
                        isVisited[i] = true;
                    }
                }
            }
        }

        return result;
    }

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

        int t = Integer.parseInt(br.readLine());

        for (int testCase = 0; testCase < t; testCase++) {
            int n = Integer.parseInt(br.readLine());

            coordinateList = new Coordinate[n + 2];
            board = new int[n + 2][n + 2];

            // 데이터 좌표 입력
            for (int i = 0; i < n + 2; i++) {
                StringTokenizer st = new StringTokenizer(br.readLine());
                int y = Integer.parseInt(st.nextToken());
                int x = Integer.parseInt(st.nextToken());
                coordinateList[i] = new Coordinate(y, x);
            }

            // 맨헤튼 거리 계산
            for (int i = 0; i < n + 2; i++) {
                for (int j = i + 1; j < n + 2; j++) {
                    board[i][j] = Math.abs(coordinateList[i].y - coordinateList[j].y)
                            + Math.abs(coordinateList[i].x - coordinateList[j].x);
                    board[j][i] = board[i][j];
                }
                board[i][i] = INF;
            }

            if (bfs(n)) {
                bw.write("happy\n");
            } else {
                bw.write("sad\n");
            }

        }

        br.close();
        bw.close();

    }
}
저작자표시 (새창열림)

'Coding Test > BOJ' 카테고리의 다른 글

[BOJ/JAVA] 1474번: 밑 줄  (0) 2022.08.21
[BOJ/JAVA] 16918번: 봄버맨  (0) 2022.08.21
[BOJ/JAVA] 1937번: 욕심쟁이 판다  (0) 2022.08.21
[BOJ/JAVA] 1107번: 리모컨  (0) 2022.08.21
[BOJ/JAVA] 4920 테트리스 게임  (0) 2022.08.21
    'Coding Test/BOJ' 카테고리의 다른 글
    • [BOJ/JAVA] 1474번: 밑 줄
    • [BOJ/JAVA] 16918번: 봄버맨
    • [BOJ/JAVA] 1937번: 욕심쟁이 판다
    • [BOJ/JAVA] 1107번: 리모컨
    베오
    베오

    티스토리툴바