From 13d53c1490bc92bd72ae3a8d0bb0efe806e98d83 Mon Sep 17 00:00:00 2001 From: "yogurt05j@gmail.com" Date: Wed, 10 Aug 2022 22:26:29 +0900 Subject: [PATCH 1/2] SayHelloFork --- src/week2/forkTest/SayHelloFork.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/week2/forkTest/SayHelloFork.java b/src/week2/forkTest/SayHelloFork.java index 7a08898..3266947 100644 --- a/src/week2/forkTest/SayHelloFork.java +++ b/src/week2/forkTest/SayHelloFork.java @@ -1,4 +1,7 @@ package week2.forkTest; public class SayHelloFork { + public static void main(String[] args) { + System.out.println("Hello Fork!"); + } } From 1fa2d21570e56b1d08c5f4d8e8a501d00d68f85d Mon Sep 17 00:00:00 2001 From: "yogurt05j@gmail.com" Date: Wed, 19 Oct 2022 01:37:15 +0900 Subject: [PATCH 2/2] =?UTF-8?q?1600=20=EB=A7=90=EC=9D=B4=EB=90=98=EA=B3=A0?= =?UTF-8?q?=ED=94=88=EC=9B=90=EC=88=AD=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/week6/monkeyWhoWannabeHorse1600/Main.java | 105 +++++++++++++++++- 1 file changed, 104 insertions(+), 1 deletion(-) diff --git a/src/week6/monkeyWhoWannabeHorse1600/Main.java b/src/week6/monkeyWhoWannabeHorse1600/Main.java index 51b6856..bab02bc 100644 --- a/src/week6/monkeyWhoWannabeHorse1600/Main.java +++ b/src/week6/monkeyWhoWannabeHorse1600/Main.java @@ -1,4 +1,107 @@ package week6.monkeyWhoWannabeHorse1600; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayDeque; +import java.util.Queue; +import java.util.StringTokenizer; + public class Main { -} + + public static class Monkey { + int r; // 행 좌표 + int c; // 열 좌표 + int k; // 말처럼 움직인 횟수 + int dist; // 총 동작수 + public Monkey(int r, int c, int k, int dist) { + this.r = r; + this.c = c; + this.k = k; + this.dist = dist; + } + } + + static int K; // 원숭이가 말처럼 움직일 수 있는 횟수 + static int W, H; // 가로 길이, 세로 길이 + static int[][] board; // 격자판 + static int[][][] DP; // [행 좌표][열 좌표][말처럼 움직인 횟수] + + public static void main(String[] args) throws IOException { + input(); + + System.out.println(bfs()); + } + + // 입력 + public static void input() throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + K = Integer.parseInt(br.readLine()); + StringTokenizer st = new StringTokenizer(br.readLine()); + W = Integer.parseInt(st.nextToken()); + H = Integer.parseInt(st.nextToken()); + + board = new int[H][W]; + DP = new int[H][W][K+1]; + + for(int h = 0; h < H; h++) { + st = new StringTokenizer(br.readLine()); + for(int w = 0; w < W; w++) { + board[h][w] = Integer.parseInt(st.nextToken()); + } + } + } + + public static int bfs() { + int[][] dirH = {{-1, -2}, {-2, -1}, {-2, 1}, {-1, 2}, {1, -2}, {2, -1}, {2, 1}, {1, 2}}; + int[][] dirM = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}}; + + Queue q = new ArrayDeque<>(); + q.add(new Monkey(0, 0, 0, 0)); + + while(!q.isEmpty()) { + Monkey temp = q.poll(); + + // 도착지점에 왔으면 + if(temp.r == H - 1 && temp.c == W - 1) { + return temp.dist; + } + + // 말처럼 움직이기 + if(temp.k < K) { + for(int i = 0; i < 8; i++) { + int nr = temp.r + dirH[i][0]; + int nc = temp.c + dirH[i][1]; + // 범위 체크 & 장애물 없는지 체크 + if(isIn(nr, nc) && board[nr][nc] == 0) { + if(DP[nr][nc][temp.k + 1] == 0) { + q.offer(new Monkey(nr, nc, temp.k + 1, temp.dist + 1)); + DP[nr][nc][temp.k + 1] = 1; + } + } + } + + } + + // 인접한 칸으로 움직이기 + for(int i = 0; i < 4; i++) { + int nr = temp.r + dirM[i][0]; + int nc = temp.c + dirM[i][1]; + // 범위 체크 & 장애물 없는지 체크 + if(isIn(nr, nc) && board[nr][nc] == 0) { + if(DP[nr][nc][temp.k] == 0) { + q.offer(new Monkey(nr, nc, temp.k, temp.dist + 1)); + DP[nr][nc][temp.k] = 1; + } + } + } + } + return -1; + } + + // 격자판 범위 내인지 체크 + public static boolean isIn(int x, int y) { + if(x < 0 || y < 0 || x >= H || y >= W) return false; + else return true; + } +} \ No newline at end of file