From 013e4456e12331409c4646d81cd89f61f68d237e Mon Sep 17 00:00:00 2001 From: Lucid Date: Tue, 29 Nov 2022 12:53:56 +0900 Subject: [PATCH] Create 20221129_1.java --- .../20221129_1.java" | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 "\353\243\250\354\213\234\353\223\234/20221129_1.java" diff --git "a/\353\243\250\354\213\234\353\223\234/20221129_1.java" "b/\353\243\250\354\213\234\353\223\234/20221129_1.java" new file mode 100644 index 0000000..58d7ace --- /dev/null +++ "b/\353\243\250\354\213\234\353\223\234/20221129_1.java" @@ -0,0 +1,65 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; + +class Status{ + int spot; + int count; + + public Status(int spot, int count) { + this.spot = spot; + this.count = count; + } +} +public class Main { + + static boolean[] chk; + static int result; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine(), " "); + + int N = Integer.parseInt(st.nextToken()); // for ladder + int M = Integer.parseInt(st.nextToken()); // for snake + chk = new boolean[101]; // default == false + result = 100; + + HashMap map = new HashMap<>(); + + for (int i = 0; i < N+M; i++) { + st = new StringTokenizer(br.readLine(), " "); + int x = Integer.parseInt(st.nextToken()); + int y = Integer.parseInt(st.nextToken()); + map.put(x, y); + } + + Queue queue = new LinkedList<>(); + queue.offer(new Status(1, 0)); + + while(!queue.isEmpty()) { + + Status s = queue.poll(); + + if(s.spot == 100) { + result = Math.min(result, s.count); + } + + for(int i=1; i<=6; i++) { + int tmp = s.spot + i; + if(tmp>100 || chk[tmp]) { + continue; + } + chk[tmp] = true; + + if(map.containsKey(tmp)) { + queue.add(new Status(map.get(tmp), s.count+1)); + continue; + } + queue.add(new Status(tmp, s.count+1)); + } + } + System.out.println(result); + } +}