AMAZON_HACKON Coding Question – Solved

2 Live
You are given an 8x8 chess board. The board has your rook denoted by 'R' and the rest of the cells are either blank, denoted by 'B' or they have some piece upon them denoted by 'P'. You can move over the blank cells only. A rook can move along its entire row or column in 1 move, up to the point where blank spaces are available. If current coordinates of a rook are (x, y) then rook can move in the following ways in 1 move: Move from (x, y) to (x-1, y), (x-2, y), (x-3, y) and so on; Move from (x, y) to (x+1, y), (x+2, y), (x+3, y) and so on; Move from (x, y) to (x, y-1), (x, y-2), (x, y-3) and so on; Move from (x, y) to (x, y+1), (x, y+2), (x, y+3) and so on. The rook can't move outside the board. As soon as the rook encounters a 'P' cell in its path it stops and can't move further. The rook needs to reach cell (8,8) in order to prove its worth. Find the minimum number of moves the rook would take to reach cell (8,8) or print -1 if it is impossible to do so. The board follows 1 based indexing. The board contains a single cell with value 'R'. Each string consists of only 3 types of characters: 'P', 'B', or 'R'. Input Format: The first line contains a single integer T, the number of testcases. Lines from 1 to 8 of each test case take input of a string of size 8 denoting the arrangement on the chess board. Output Format: Print the minimum number of moves required to reach cell (8,8) or print -1 if it is impossible to do so. Constraints: 1 <= T <= 10^8, length of each string = 8. Sample Testcase 1: Input: 1 BBBBBBBB BRPPPPPP PBPPPPPP PBPPPPPP PBBBBPPP PPBBBBBB PPPPPPPB PPPPPPPB Output: 5 Explanation: In the first move the rook can move from cell (2,2) to (5,2) as all cells in between are blank, i.e. cells (3,2), (4,2), (5,2) are blank. In the second move the rook can move from cell (5,2) to (5,4) as all cells in between are blank, i.e. cells (5,3), (5,4) are blank. In the third move the rook can move from cell (5,4) to (6,4). In the fourth move the rook can move from cell (6,4) to (6,8). In the fifth move the rook can move from cell (6,8) to (8,8). Path: (2,2) > (5,2) > (5,4) > (6,4) > (6,8) > (8,8). Thus, it took 5 moves for the rook to reach cell (8,8). There might be other possible paths too but none of them would take less than 5 moves. Sample Testcase 2: Input: BBBBBBBB BBBBBBBB RBBBBBBB BBBBBBBB BBBBBBBB BBBBBBBB BBBBBBPP BBBBBBPB Output: -1 Explanation: Since cell (8,8) is blocked by pieces ('P') from all directions, it is impossible for the rook to reach cell (8,8) regardless of the path taken.

Asked in: AMAZON_HACKON

Image of the Question

Question Image Question Image Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To solve this problem of finding the minimum number of moves a rook needs to travel from its starting position 'R' on an 8x8 chessboard to the target cell (8,8), given the movement constraints and obstacles, you need a systematic approach that carefully models the rook’s allowed moves and efficiently explores possible paths. Here's a detailed thought process and strategy to tackle the problem:

1. **Problem Restatement and Constraints**
You have an 8x8 grid with cells containing:
- 'R' — the rook’s starting position (exactly one on the board)
- 'B' — blank cells where the rook can move freely
- 'P' — cells occupied by pieces that block movement; the rook cannot move onto or beyond these cells

The rook can move any number of cells in one move along its current row or column, but only over blank cells 'B'. It stops as soon as it encounters a 'P' piece or the board’s edge. The rook moves strictly orthogonally (up, down, left, right).

The objective is to find the **minimum number of moves** to reach the cell at position (8,8), or return -1 if unreachable.

2. **Understanding Rook Movements**
In one move, a rook at position (x,y) can move in four directions:
- Up: (x-1, y), (x-2, y), ... until a 'P' or edge is encountered
- Down: (x+1, y), (x+2, y), ... until blocked
- Left: (x, y-1), (x, y-2), ... until blocked
- Right: (x, y+1), (x, y+2), ... until blocked

The rook can stop on any blank cell in these directions within a single move, but cannot jump over a piece 'P'.

3. **Modeling the Problem as a Graph Search**
Treat the chessboard cells as nodes in a graph. The rook’s moves from one cell to reachable cells in one move define edges to neighbors.

- Each node corresponds to a cell.
- Edges connect the current cell to all other reachable blank cells along the same row or column that can be reached in one rook move (not passing through pieces).
- The weight of each edge is 1, since each move counts as one step.

Your task reduces to finding the shortest path from the node representing the rook’s starting cell to the node representing (8,8).

4. **Choosing a Search Algorithm**
Since all moves cost equal weight (1 move), a **Breadth-First Search (BFS)** is a natural and optimal choice to find the shortest path in terms of moves.

Why BFS?
- BFS explores the graph level by level, guaranteeing the shortest path in an unweighted graph.
- It suits the problem because you are searching for minimum moves, which translates directly into minimum edges traversed.

5. **Key Steps for BFS Implementation**
- Identify the rook’s starting position by scanning the board.
- Initialize a queue for BFS and a visited structure (e.g., a 2D boolean array) to track visited cells and avoid redundant searches.
- Start BFS from the rook’s position with move count = 0.
- For each current position dequeued:
- Generate all reachable positions in one rook move:
- For each of the four directions, move stepwise until you hit a 'P' or board edge.
- For every reachable cell on this path, if not visited, add it to the BFS queue with move count incremented by 1.
- Mark cells as visited when enqueued to avoid re-processing.
- Continue BFS until you reach cell (8,8) or exhaust all possibilities.

6. **Handling the Movement Generation Efficiently**
For each BFS iteration:
- From the current cell, you scan in the four directions.
- Instead of moving one step at a time and enqueueing each intermediate cell, the rook can jump directly to any reachable cell on the line in one move.
- So, for each direction:
- Move stepwise along the row or column until hitting 'P' or edge.
- Collect all reachable cells in that direction.
- Add all these reachable cells as neighbors to explore.

7. **Termination Conditions**
- If during BFS, you dequeue cell (8,8), return the move count associated with it, since this is the minimum moves.
- If BFS ends without reaching (8,8), return -1.

8. **Complexity and Constraints**
- The board size is fixed at 8x8, so even a brute-force BFS with repeated scanning in four directions is efficient enough.
- The maximum number of test cases (T) may be very large according to the problem, so an efficient solution per test case is crucial.
- Marking visited cells prevents infinite loops and repeated work.

9. **Example Walkthrough**
Consider the first sample:
- Starting at (2,2) with rook 'R'.
- From (2,2), look up, down, left, right for reachable blank cells:
- Down: cells (3,2), (4,2), (5,2) are blank until blocked.
- All these can be reached in 1 move, so enqueue them.
- From each new position, repeat the process.
- Eventually reach (8,8) in 5 moves as explained.

10. **Summary of the Approach**
- Parse input and locate rook.
- Use BFS from rook’s position.
- At each step, generate neighbors reachable in one move by scanning rows and columns.
- Maintain visited structure to avoid repeated states.
- Once (8,8) is reached, return move count.
- If unreachable, return -1.

This approach cleanly models the rook's special movement rules and efficiently finds the shortest path to the goal, considering obstacles and board boundaries.
```


Related Questions