functionsolution(board){/*
ex)
visited : {
'h': [[0,0,0],[0,0,0],[0,0,0]],
'v': [[0,0,0],[0,0,0],[0,0,0]]
};
*/const visited ={'h':Array(board.length).fill(0).map(i=>Array(board.length).fill(0)),'v':Array(board.length).fill(0).map(i=>Array(board.length).fill(0))}constisVisited=(dir,y,x)=> visited[dir][y][x];// board 내에 존재하는 좌표이면서 장애물이 없을 경우 true 반환constisSafe=(y,x)=>{if(board[y]===undefined){// y is not boundedreturnfalse;}const val = board[y][x]if(val===undefined|| val===1){// x is not bounded or point is obstaclereturnfalse;}returntrue;}const stack =[['h',0,1,0]];// [방향, ...head좌표 , 시간]while(stack.length){const head = stack.shift();// head is right or bottom part of robotconst[dir,y,x, time]= head;// break 조건 : 종점일 때if(y===board.length-1&& x===board.length-1){return time;}// visited에 체크하지 않을 경우 성능 저하 (불필요한 search, 시간 초과 발생)if(isVisited(dir,y,x)){continue;}
visited[dir][y][x]=1;if(dir==='h'){// 가로로 놓였을 때 // 1. 좌 우 상 하가 안전한지 확인const isRight =isSafe(y,x+1);const isBottom =isSafe(y+1, x)&&isSafe(y+1, x-1);const isLeft =isSafe(y, x-2);const isTop =isSafe(y-1, x)&&isSafe(y-1, x-1);// 2. 안전할 경우 stack에 다음에 올 수 있는 로봇의 위치를 pushif(isRight){
stack.push(['h',y,x+1, time+1]);}if(isBottom){
stack.push(['h',y+1,x, time+1]);
stack.push(['v',y+1,x, time+1]);// 회전 BR
stack.push(['v',y+1,x-1, time+1]);// 회전 BL}if(isLeft){
stack.push(['h',y,x-1, time+1]);}if(isTop){
stack.push(['h',y-1,x, time+1]);
stack.push(['v',y,x-1, time+1])// 회전 TL
stack.push(['v',y,x, time+1]);// 회전 TR}}else{// 세로로 놓였을 떄const isRight =isSafe(y, x+1)&&isSafe(y-1, x+1);const isBottom =isSafe(y+1, x);const isLeft =isSafe(y, x-1)&&isSafe(y-1,x-1);const isTop =isSafe(y-2, x);if(isLeft){
stack.push(['v',y,x-1,time+1]);
stack.push(['h',y-1,x,time+1]);// 회전
stack.push(['h',y,x,time+1])// 회전}if(isTop){
stack.push(['v',y-1,x,time+1]);}if(isRight){
stack.push(['v',y,x+1,time+1]);
stack.push(['h',y,x+1,time+1]);// 회전
stack.push(['h',y-1,x+1,time+1]);// 회전}if(isBottom){
stack.push(['v',y+1,x,time+1]);}}}}