function solution(park, routes) {
let answer = [];
const height = park.length;
const width = park[0].length;
// 방향 선언
const direction = {
E: [0, 1],
W: [0, -1],
S: [1, 0],
N: [-1, 0],
};
// 시작 위치 구하기
for (let i = 0; i < height; i++) {
for (let j = 0; j < width; j++) {
if (park[i][j] === "S") {
answer = [i, j];
break;
}
}
if (answer.length !== 0) break;
}
// 이동
routes.forEach((s) => {
let isMove = true;
let [y, x] = answer;
const [op, n] = s.split(" ");
const [dy, dx] = direction[op];
for (let i = 0; i < n; i++) {
const newY = y + dy;
const newX = x + dx;
if (newX >= 0 && newX < width && newY >= 0 && newY < height && park[newY][newX] !== "X") {
y = newY;
x = newX;
} else {
isMove = false;
break;
}
}
if (isMove) answer = [y, x];
});
return answer;
}
function solution(park, routes) {
// 공원이 가로 세로 길이
const maxRow = park.length - 1;
const maxCol = park[0].length - 1;
// 시작 지점 좌표
let row = park.findIndex((s) => s.includes("S"));
let col = park[row].indexOf("S");
routes.forEach((position) => {
// 요소를 빈칸 기준으로 나눠준다.
const [d, n] = position.split(" ");
// 임시 좌표를 만들어주고 flag도 하나 만들어준다.
let tempRow = row;
let tempCol = col;
let flag = true;
// 구한 이동거리 수 만큼 for문을 돌려준다.
for (let i = 0; i < Number(n); i++) {
if (d === "E") tempCol++;
else if (d === "W") tempCol--;
else if (d === "S") tempRow++;
else if (d === "N") tempRow--;
// 공원을 벗어나는지 장애물이 없는지를 확인한다.
if (
tempRow > maxRow ||
tempRow < 0 ||
tempCol > maxCol ||
tempCol < 0 ||
park[tempRow][tempCol] === "X"
) {
// 장애물이 있거나 공원을 벗어났다면
// flag에 false를 넣어주고 break를 통해 for문을 종료시킨다.
flag = false;
break;
}
}
// for문을 무사히 통과했다면 이동을 한 것이기 때문에
// 임시 좌표를 실제 좌표에 대입해준다.
if (flag) {
col = tempCol;
row = tempRow;
}
});
// 다 확인 후 마지막 좌표를 반환한다.
return [row, col];
}
function solution(park, routes) {
// park에서 routes 대로 실행시 마지막에 있는 위치[y,x]
//park는 직사각형, s는 스타트, o는 이동가능, x는 장애물
//routes는 "방향 칸수"
// 명령결과가 범위 밖, 장애물을 만나면 실행x, 다음 명령
const dir={N:[-1,0], S:[1,0], W:[0,-1], E:[0,1]};
const h=park.length;
const w=park[0].length;
let s=[];
const x=[];
//s지점 찾기
for(let i=0; i<h; i++ ){
let flag=false;
for(let j =0; j<w; j++){
if(park[i][j]==="S"){
s.push(i,j);
flag=true;
break;
}
}
if(flag)break;
}
// 각 명령에 따라 움직여보기
for(let a of routes){
const [d, c]=a.split(" ");
let [cy, cx]=s;
const [my, mx]=dir[d];
let flag=false;
for(let i =0; i<c; i++){
const [ny,nx]=[cy+my, cx+mx];
// "x"체크는 마지막에, 앞에서 한다면 범위를 벗어났을때 런타임에러가 뜬다.
if( ny<h && ny>=0 && nx>=0 && nx<w && park[ny][nx]!=="X"){
cy=ny, cx=nx;
}else{
flag=true;
break;
}
}
if(!flag)s=[cy,cx];
}
return s
}
function solution(park, routes) {
const directions = {
'E': [0, 1],
'W': [0, -1],
'S': [1, 0],
'N': [-1, 0]
}
let posRow = park.findIndex(row => row.includes('S'));
let posCol = park[posRow].indexOf('S');
const sizeRow = park.length;
const sizeCol = park[0].length;
routes.forEach((route) => {
let tempRow = posRow;
let tempCol = posCol;
let canMove = true;
const [op, n] = route.split(' ')
// n만큼 한 칸씩 이동하면서 확인 (장애물, 범위)
for (let i = 0; i < n; i++) {
tempRow += directions[op][0];
tempCol += directions[op][1];
if (tempRow < sizeRow && tempRow >= 0 && tempCol < sizeCol && tempCol >= 0 && park[tempRow][tempCol] !== 'X') {
// 주의!!! 반드시 !== 'X' 조건으로... 'S'를 지날 수 있기 때문에....
continue;
} else {
canMove = false;
break;
}
}
if (canMove) {
posRow = tempRow;
posCol = tempCol;
}
})
return [posRow, posCol];
}
function solution(park, routes) {
const answer = [];
let curY = 0;
let curX = 0;
const H = park.length;
const W = park[0].length;
const direction = {
E : [0, 1],
W : [0, -1],
S : [1, 0],
N : [-1, 0]
};
const route = routes.map(v => v.split(" "));
//시작 위치 탐색
for(let i = 0; i < H; i++){
for(let j = 0; j < W; j++){
if(park[i][j] === "S"){
curY = i;
curX = j;
}
}
}
for(let [dir, dis] of route){
const [dy, dx] = direction[dir];
let [ty, tx] = [curY, curX];
let [ny, nx] = [curY + dy , curX + dx];
dis = +dis;
// 만약 이동 횟수가 남았고, 범위 밖으로 나가지 않으며 경로에 장애물이 없다면
while(dis && ny >= 0 && ny < H && nx >= 0 && nx < W && park[ny][nx] !== "X"){
[ty, tx] = [ny, nx];
[ny, nx] = [ty + dy, tx + dx];
dis--;
}
// 만약 해당 명령을 완수할 수 있다면 이동
// 명령을 완수하지 못한다면 이동 취소
if(dis === 0){
[curY, curX] = [ty, tx];
}
}
return [curY, curX];
}
function solution(park, routes) {
let start = [0,0];
const direction = {
"N": [-1, 0],
"S": [1, 0],
"W": [0, -1],
"E": [0, 1]
}
// 시작 point 찾기
for(let i= 0; i < park.length; i++){
const point = park[i].indexOf('S');
if(point > -1){
start = [i, park[i].indexOf('S')];
break;
}
}
routes.forEach((route) =>{
const [d, n] = route.split(' ');
let tmp = [start[0],start[1]]; // 임시 이동 경로
let hasObstacle = false; // 장애물이 있는지 확인하는 변수
// n만큼 이동하는 for문
for(let i =0 ; i< n; i++){
tmp[0] += direction[d][0];
tmp[1] += direction[d][1];
// 공원 범위를 벗어났을 때 for문에서 나온다.
if(tmp[0] < 0 || tmp[1] <0 || tmp[0]> park.length -1 || tmp[1] > park[0].length -1){
hasObstacle = true;
break;
}
// 장애물이 있을 경우 for문에서 나온다.
if(park[tmp[0]][tmp[1]] === 'X'){
hasObstacle = true;
break;
}
}
//장애물이 없는 경우만 start 포인트를 변경
if(!hasObstacle){
start = tmp
}
})
return start;
}