on
[백준] 9205번:맥주 마시면서 걸어가기 (Java 자바)
[백준] 9205번:맥주 마시면서 걸어가기 (Java 자바)
반응형
문제
https://www.acmicpc.net/problem/9205
풀이 및 소스코드
맥주를 마시면서 이동할 수 있는 거리는
50*20 = 1000 이다.
좌표들을 입력받고, 그 좌표들끼리 사이의 거리가 1000이하라면 서로 갈 수 있는 곳이기 때문에 정점을 양방향으로 연결해준다.
연결해준 정점을 토대로 집(g[0])부터 시작해서 bfs를 돌린다.
도착지 인덱스인 n+1에 도달하면 happy를, 도달하지 않는다면 sad를 출력해주면 된다.
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.Queue; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; StringBuilder sb = new StringBuilder(); int t = Integer.parseInt(br.readLine()); for(int tt=0;tt[] g = new LinkedList[n+2]; for(int i=0;i(); } for(int i=0;i q = new LinkedList<>(); q.add(0); v[0] = true; while(!q.isEmpty()) { int now = q.poll(); if(now==n+1) { c = true; break; } for(int i=0;i
"); }else { sb.append("sad
"); } } System.out.println(sb); } } class node { int x, y ; public node(int x, int y) { super(); this.x = x; this.y = y; } }
반응형
from http://jainn.tistory.com/268 by ccl(A) rewrite - 2021-09-16 18:27:45