[프로그래머스]게임 맵 최단거리

[프로그래머스]게임 맵 최단거리

import java.util.LinkedList;

import java.util.Queue;

public class GameMapShortDistance {

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

int answer,x,y,nx,ny;

//상하좌우를 모두 확인

int [] dx = { - 1 , 0 , 1 , 0 };

int [] dy = { 0 , - 1 , 0 , 1 };

int n = maps. length ; int m = maps[ 0 ]. length ;

//방문여부 검증

boolean [][]visited = new boolean [n][m];

Queue < Integer > que = new LinkedList < > ();

que.offer( 0 );que.offer( 0 );

visited[ 0 ][ 0 ] = true ;

while ( ! que.isEmpty()){

x = que.poll();

y = que.poll();

for ( int i = 0 ; i < dx. length ; i + + ){

nx = x + dx[i];

ny = y + dy[i];

if (nx < 0 | | nx > = n | | ny < 0 | | ny > = m) continue ;

if (maps[nx][ny] = = 0 ) continue ;

if ( ! visited[nx][ny]){

que.offer(nx);

que.offer(ny);

maps[nx][ny] = maps[x][y] + 1 ;

visited[nx][ny] = true ;

}

}

}

if (maps[n - 1 ][m - 1 ] = = 1 ){

answer = - 1 ;

}

answer = maps[n - 1 ][m - 1 ];

return answer;

}

}

from http://jarvics.tistory.com/64 by ccl(A) rewrite - 2021-10-21 21:01:41