From 9ded970d355df2af50b11a15369e84b3a0443137 Mon Sep 17 00:00:00 2001 From: rl544 <111494264+rl544@users.noreply.github.com> Date: Tue, 24 Jan 2023 20:23:21 +0900 Subject: [PATCH] =?UTF-8?q?uploaded=20at=202023.=201.=2024.=20=EC=98=A4?= =?UTF-8?q?=ED=9B=84=208:23:20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- programmers/게임 맵 최단거리/solution.java | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 programmers/게임 맵 최단거리/solution.java diff --git a/programmers/게임 맵 최단거리/solution.java b/programmers/게임 맵 최단거리/solution.java new file mode 100644 index 0000000..ebf6f8e --- /dev/null +++ b/programmers/게임 맵 최단거리/solution.java @@ -0,0 +1,24 @@ +// [문제 링크]: https://school.programmers.co.kr/learn/courses/30/lessons/1844 + +import java.util.LinkedList; +class Solution { + public int solution(int[][] maps) { + boolean[][] visit = new boolean[maps.length][maps[0].length]; + Integer[][] n = new Integer[][] {{-1,0},{1,0},{0,-1},{0,1}}; + LinkedList linkedList = new LinkedList<>(); + linkedList.add(new Integer[] {0,0,1}); + visit[0][0] = true; + while(!linkedList.isEmpty()){ + Integer[] pos = linkedList.poll(); + if(pos[0] == maps.length-1 && pos[1] == maps[0].length-1) return pos[2]; + for(int x = 0; x < 4; x++){ + int cx = pos[0]+n[x][0], cy = pos[1]+n[x][1]; + if(cx >= 0 && cy >= 0 && cx < maps.length && cy < maps[0].length &&maps[cx][cy] != 0 && !visit[cx][cy]){ + visit[cx][cy] = true; + linkedList.add(new Integer[] {cx,cy,pos[2]+1}); + } + } + } + return -1; + } +} \ No newline at end of file