-> 자세한 내용 보러가기
import java.util.*;
class Solution {
public int[] solution(String[] park, String[] routes) {
//공원의 넓이 및 높이
int h = park.length;
int w = park[0].length();
//움직임을 저장
int[][] nav = {
{1, 0}, //S
{-1, 0}, //N
{0, -1}, //W
{0, 1} //E
};
//현재 위치 값
int x = 0, y = 0;
//시작 지점을 찾는다.
for(int idx = 0; idx < park.length; idx++){
char[] ch = park[idx].toCharArray();
for(int j = 0; j < ch.length; j++){
if(ch[j] == 'S'){
x = idx;
y = j;
}
}
}
//routes를 순회하면서 해당 위치를 조절
// 벗어나는 경우 X
// 장애물이 있으면 안됨
for(String route : routes){
String[] parts = route.split(" ");
//방향
String direction = parts[0];
//움직일 거리
int distance = Integer.parseInt(parts[1]);
int flag = 0;
int step_x = x;
int step_y = y;
//거리만큼 park를 순회해야함
for(int i = 1; i <= distance; i++){
step_x += nav[Arrays.asList("S", "N", "W", "E").indexOf(direction)][0];
step_y += nav[Arrays.asList("S", "N", "W", "E").indexOf(direction)][1];
//순회했을 때 범위를 벗어나거나, 혹은 장애물을 만났다? -> break;
if (step_x >= h || step_x <= -1 || step_y >= w || step_y <= -1 || park[step_x].charAt(step_y) == 'X') {
flag = 1;
break;
}
}
//아닐 때만 거리를 그만큼 옮긴다
if (flag == 0) {
x += nav[Arrays.asList("S", "N", "W", "E").indexOf(direction)][0] * distance;
y += nav[Arrays.asList("S", "N", "W", "E").indexOf(direction)][1] * distance;
}
}
int[] answer = {x, y};
return answer;
}
}