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.
publicstaticintminimumMoves(List<string>grid,intstartX,intstartY,intgoalX,intgoalY){List<StringBuilder>gridBuilder=newList<StringBuilder>();for(inti=0;i<grid.Count;i++){gridBuilder.Add(newStringBuilder(grid[i]));}Queue<int[]>queue=newQueue<int[]>();intn=grid.Count;// table to keep trackvartrack=newint[n,n];for(inti=0;i<n;i++)for(intj=0;j<n;j++)track[i,j]=0;queue.Enqueue([startX,startY]);while(queue.Count>0){vararr=queue.Dequeue();intcurrentX=arr[0];intcurrentY=arr[1];if(currentX>=n||currentX<0||currentY>=n||currentY<0||gridBuilder[currentX][currentY]=='X'||gridBuilder[currentX][currentY]=='0')continue;// Mark as visitedgridBuilder[currentX][currentY]='0';if(currentX==goalX&¤tY==goalY){break;}intcount=track[currentX,currentY]+1;// Downintindex=currentX+1;while(index<n&&gridBuilder[index][currentY]!='X'&&gridBuilder[index][currentY]!='0'){if(track[index,currentY]==0){queue.Enqueue([index,currentY]);track[index,currentY]=count;}index++;}// Upindex=currentX-1;while(index>=0&&gridBuilder[index][currentY]!='X'&&gridBuilder[index][currentY]!='0'){if(track[index,currentY]==0){queue.Enqueue([index,currentY]);track[index,currentY]=count;}index--;}// Rightindex=currentY+1;while(index<n&&gridBuilder[currentX][index]!='X'&&gridBuilder[currentX][index]!='0'){if(track[currentX,index]==0){queue.Enqueue([currentX,index]);track[currentX,index]=count;}index++;}// Leftindex=currentY-1;while(index>=0&&gridBuilder[currentX][index]!='X'&&gridBuilder[currentX][index]!='0'){if(track[currentX,index]==0){queue.Enqueue([currentX,index]);track[currentX,index]=count;}index--;}}returntrack[goalX,goalY];}
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 →
Using C#: