Skip to content

Commit

Permalink
게임 맵 최단 거리 - level2
Browse files Browse the repository at this point in the history
  • Loading branch information
sey2 authored Aug 15, 2022
1 parent af7d536 commit 0a3bdaf
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions programmers/GameMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.util.*;
class Pos{
int x;
int y;

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

class Solution {
static int dx[] = {0, 0, -1, 1};
static int dy[] = {-1, 1, 0, 0};
static int visited[][];

public int solution(int[][] maps) {

return bfs(maps,new Pos(0,0));
}

public static int bfs(int[][] maps, Pos start){
Queue<Pos> queue = new LinkedList<>();
queue.add(start);

visited = new int[maps.length][maps[0].length];
visited[start.y][start.x] = 1;

while(!queue.isEmpty()){
Pos cur = queue.poll();

for(int i=0; i<4; i++){
int nextY = cur.y + dy[i];
int nextX = cur.x + dx[i];

if(nextX >=0 && nextX < maps[0].length && nextY >=0 && nextY < maps.length){
if(visited[nextY][nextX] == 0 && maps[nextY][nextX]==1){
visited[nextY][nextX] = visited[cur.y][cur.x] + 1;
queue.add(new Pos(nextY, nextX));
}
}
}
}

return (visited[visited.length-1][visited[0].length-1] == 0) ? -1 : visited[visited.length-1][visited[0].length-1];
}
}

0 comments on commit 0a3bdaf

Please sign in to comment.