We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
functionminimumMoves(grid,startX,startY,goalX,goalY){// Write your code hereconstqueue=[[startX,startY,0]];constvisited=newSet();constdirections=[[-1,0],[1,0],[0,-1],[0,1]];while(queue.length>0){const[x,y,moves]=queue.shift();constposition=`${x}-${y}`;if(x===goalX&&y===goalY){returnmoves;}visited.add(position);for(const[dx,dy]ofdirections){letnewX=x+dx;letnewY=y+dy;while(newX>=0&&newY>=0&&newX<grid.length&&newY<grid.length&&grid[newX][newY]!=='X'){constnewPosition=`${newX}-${newY}`;if(!visited.has(newPosition)){queue.push([newX,newY,moves+1]);visited.add(newPosition);}newX+=dx;newY+=dy;}}}return-1;// If a path is not found}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Castle on the Grid
You are viewing a single comment's thread. Return to all comments →
JavaScript