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.
There are 2 issues
* player can stop anywhere and change direction, does not need to go "until it reaches the edge of the grid or a blocked cell"
* Xs and Ys are swapped, iterating over Xs == going through the rows
C++ solution:
usingxy_distance=array<int,3>;charvisited[100][100];intminimumMoves(vector<string>grid,intstartX,intstartY,intgoalX,intgoalY){// iterating over Xs == iterating over rows in the problem statementintstart_row=startX;intstart_col=startY;intgoal_row=goalX;intgoal_col=goalY;introws=grid.size();intcols=grid[0].size();memset(visited,0,sizeofvisited);queue<xy_distance>q;autovisit=[&](intr,intc,intd){if(!visited[r][c]){q.push({r,c,d});visited[r][c]=1;}};automove=[&](introw,intcol,intd,introw_dir,intcol_dir){intr=row,c=col;while(r>=0&&c>=0&&r<rows&&c<cols){if(grid[r][c]=='X'){break;}else{visit(r,c,d+1);}r+=row_dir;c+=col_dir;}};visit(start_row,start_col,0);while(!q.empty()){auto[r,c,d]=q.front();q.pop();if(r==goal_row&&c==goal_col){returnd;}move(r,c,d,-1,0);move(r,c,d,+1,0);move(r,c,d,0,-1);move(r,c,d,0,+1);}return-1;}
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 →
There are 2 issues * player can stop anywhere and change direction, does not need to go "until it reaches the edge of the grid or a blocked cell" * Xs and Ys are swapped, iterating over Xs == going through the rows
C++ solution: