문제 설명
지나다니는 길을 'O', 장애물을 'X'로 나타낸 직사각형 격자 모양의 공원에서 로봇 강아지가 산책을 하려합니다. 산책은 로봇 강아지에 미리 입력된 명령에 따라 진행하며, 명령은 다음과 같은 형식으로 주어집니다.
["방향 거리", "방향 거리" … ]
예를 들어 "E 5"는 로봇 강아지가 현재 위치에서 동쪽으로 5칸 이동했다는 의미입니다. 로봇 강아지는 명령을 수행하기 전에 다음 두 가지를 먼저 확인합니다.
주어진 방향으로 이동할 때 공원을 벗어나는지 확인합니다.
주어진 방향으로 이동 중 장애물을 만나는지 확인합니다.
위 두 가지중 어느 하나라도 해당된다면, 로봇 강아지는 해당 명령을 무시하고 다음 명령을 수행합니다.
공원의 가로 길이가 W, 세로 길이가 H라고 할 때, 공원의 좌측 상단의 좌표는 (0, 0), 우측 하단의 좌표는 (H - 1, W - 1) 입니다.
image
공원을 나타내는 문자열 배열 park, 로봇 강아지가 수행할 명령이 담긴 문자열 배열 routes가 매개변수로 주어질 때, 로봇 강아지가 모든 명령을 수행 후 놓인 위치를 [세로 방향 좌표, 가로 방향 좌표] 순으로 배열에 담아 return 하도록 solution 함수를 완성해주세요.
제한사항
3 ≤ park의 길이 ≤ 50
3 ≤ park[i]의 길이 ≤ 50
park[i]는 다음 문자들로 이루어져 있으며 시작지점은 하나만 주어집니다.
S : 시작 지점
O : 이동 가능한 통로
X : 장애물
park는 직사각형 모양입니다.
1 ≤ routes의 길이 ≤ 50
routes의 각 원소는 로봇 강아지가 수행할 명령어를 나타냅니다.
로봇 강아지는 routes의 첫 번째 원소부터 순서대로 명령을 수행합니다.
routes의 원소는 "op n"과 같은 구조로 이루어져 있으며, op는 이동할 방향, n은 이동할 칸의 수를 의미합니다.
op는 다음 네 가지중 하나로 이루어져 있습니다.
N : 북쪽으로 주어진 칸만큼 이동합니다.
S : 남쪽으로 주어진 칸만큼 이동합니다.
W : 서쪽으로 주어진 칸만큼 이동합니다.
E : 동쪽으로 주어진 칸만큼 이동합니다.
1 ≤ n ≤ 9
def solution(park, routes):
dic = {}
h = 0
w = 0
max_h = len(park)
max_w = len(park[0])
park_l =[[0 for j in range(len(park[0]))] for i in range(len(park))]
for i in range(len(park)):
for j, a in enumerate(park[i]):
park_l[i][j] = a
if a == "S":
h = i
w = j
for i in routes:
a, b = i.split(" ")
x=0
y=0
if a =="N":
y = -int(b)
elif a == "S":
y = int(b)
elif a == "W":
x = -int(b)
elif a == "E":
x = int(b)
if len(park)<=h+y or h+y<0 or len(park[0])<=w+x or w+x<0:
continue
is_continue = False
if y != 0:
for i in range(y):
if park_l[h+i][w] == "X":
is_continue = True
break;
else :
for i in range(x):
if park_l[h][w+i] == "X":
is_continue = True
break;
if is_continue:
continue
park_l[h][w], park_l[h+y][w+x] = "O", "S"
h = h+y
w = w+x
return [h, w]
def solution(park, routes):
r,c,R,C = 0,0,len(park),len(park[0]) # r,c : S위치 / R,C : 보드경계
move = {"E":(0,1),"W":(0,-1),"S":(1,0),"N":(-1,0)}
for i,row in enumerate(park): # 시작점 찾기
if "S" in row:
r,c = i,row.find("S")
break
for route in routes:
dr,dc = move[route[0]] # 입력받는 route의 움직임 방향
new_r,new_c = r,c # new_r,new_c : route 적용 후 위치
for _ in range(int(route[2])):
# 한칸씩 움직이면서, 보드 안쪽이고 "X"가 아니라면 한칸이동
if 0<=new_r+dr<R and 0<=new_c+dc<C and park[new_r+dr][new_c+dc] != "X":
new_r,new_c = new_r+dr,new_c+dc
else: # 아니라면 처음 위치로
new_r,new_c = r,c
break
r,c = new_r,new_c # 위치 업데이트
return [r,c]
import java.util.*;
class Solution {
public int[] solution(String[] park, String[] routes) {
int[] answer = new int[2];
int y =0;
int x =0;
int max_y=park.length;
int max_x =park[0].length();
char[][] parks = new char[max_y][max_x];
for(int i=0; i<max_y; i++){
for(int j=0; j<max_x; j++){
parks[i][j] = park[i].charAt(j);
if(park[i].charAt(j) == 'S'){
x = j;
y = i;
}
}
}
int[][] valueArray = { {-1,0}, {1,0},{0,-1},{0,1}};
String[] directions = {"N", "S", "W", "E"};
Map<String, int[]> map = new HashMap();
for(int i=0; i<directions.length;i++){
map.put(directions[i],valueArray[i]);
}
for(String p:routes){
String[] route = p.split(" ");
int range = Integer.parseInt(route[1]);
int[] move = map.get(route[0]);
int dx = move[1];
int dy = move[0];
int new_x=x;
int new_y=y;
for(int i=0; i<range;i++){
if( 0 <= new_x+dx && new_x+dx < max_x &&
0 <= new_y+dy && new_y+dy<max_y && parks[new_y+dy][new_x+dx] != 'X' ){
new_x += move[1];
new_y += move[0];
}else{
new_x=x;
new_y=y;
break;
}
}
x = new_x;
y = new_y;
}
answer[0]=y;
answer[1]=x;
return answer;
}
}
class Solution {
public static int[] solution(String[] park, String[] routes) {
int x = 0;
int y = 0;
int[][] arr = {{-1,0},{1,0},{0,-1},{0,1}};
boolean start = false;
for(int i=0; i< park.length;i++){
if(start){
break;
}
for(int j=0; j<park[i].length();j++){
if(park[i].charAt(j) == 'S'){
x = i;
y = j;
start = true;
break;
}
}
}
for (String route : routes) {
String[] routeArr = route.split(" ");
String direction = routeArr[0];
int distance = Integer.parseInt(routeArr[1]);
int index = getDirectionIndex(direction);
if(isWalk(park, x, y, distance, arr[index])){
x += distance*arr[index][0];
y += distance*arr[index][1];
}
}
return new int[]{x,y};
}
private static boolean isWalk(String[] park, int x, int y, int distance, int[] arr) {
for(int i = 0; i< distance; i++){
x+= arr[0];
y+= arr[1];
if(y<0 || y> park[0].length()-1 || x<0 || x> park.length-1 || park[x].charAt(y)=='X'){
return false;
}
}
return true;
}
private static int getDirectionIndex(String direction) {
int index = 0;
switch (direction){
case "N":
break;
case "S":
index = 1;
break;
case "W":
index = 2;
break;
case "E":
index = 3;
break;
}
return index;
}
}