BoardGamesMarks: 30 Problem Description Ankitha enjoys finding new games. One day, she…
- User
BoardGamesMarks: 30 Problem Description Ankitha enjoys finding new games. One day, she found a grid with dimensions M*N and decided to make up a special game to play on it. When Ankitha came up with the idea for the new game, her friend Akhil joined her. She then decided to share and explain the game to him.
Akhil is given a grid with dimensions M*N, where each cell contains either 0 or 1. Additionally, he is provided with the coordinates of source and destination cells.You can only move to places whose value is 0. Furthermore, he is given the move rule (x, y) which helps in finding the location for the next move. From the given cell, you can move in four directions (forward, back,right ,left), unless they are out of grid. The rules for finding the next move from a current cell are given below.
For moving forward, add the move rule to the current cell. For moving right, from current position add the move rule, rotate the path 90 degree clockwise, For moving left, from current position add the move rule, rotate the path 90 degree anticlockwise direction, For moving backward, from current position add the move rule, rotate the path 180 degree in clock or anti clockwise. The rules can be understood better from the following example. Let the current cell be (1,1) and the move rule as (1,2)
com.tcs.cv.automata.ei.middleware.DocxToHtmlConverter@65ae095c:image1.png
on forward the next cell would be (2,3) , on right the next cell would be (3,0), left (-1,2) and back (0,-1) are out of grid.
Obeying the given move rule, find out minimum how many cells he need to travel in order to reach the destination.
Constraints 4 <= M, N <= 50
Input First line consist of two space separated integers M and N denoting the number of rows and columns in the grid.
Next M lines consists of N space separated integers representing the grid.
Next line consists of two space separeted integers denoting source cell.
Next line consists of two space separated integers denoting destination cell.
Last line consists of two space separated integer representing move rule.
Output Print a single integer denoting the minimum moves required to reach the destination.
Time Limit (secs) 1
Examples Example 1
Input
6 6
0 1 0 0 0 0
0 0 0 0 0 1
0 1 0 0 0 0
1 1 0 0 0 1
0 0 0 0 0 0
1 1 0 0 1 0
1 0
5 3
1 2
Output
3
Explanation
Akhil needs to move from (1, 0) to (5, 3) and the given step for next move is (1, 2).
In order to minimise the number of moves, he follows the path (1,0) -> (2,2) -> (3,4) ->(5,3) where in total he made 3 moves. No other paths will give moves less than 3. Hence print 3 as the output.
- Assistant
import sys from collections import deque def main(): import sys # Read all input tokens tokens = sys.stdin.read().split() it = iter(tokens) try: M = int(next(it)) N = int(next(it)) except StopIteration: print(-1) return # Read grid grid = [] for _ in range(M): row = [] for _ in range(N): try: val = int(next(it)) row.append(val) except StopIteration: row.append(1) # Assume blocked if not enough inputs grid.append(row) # Read source try: src_r = int(next(it)) src_c = int(next(it)) except StopIteration: print(-1) return # Read destination try: dest_r = int(next(it)) dest_c = int(next(it)) except StopIteration: print(-1) return # Read move rule try: rule_x = int(next(it)) rule_y = int(next(it)) except StopIteration: print(-1) return # Define move deltas forward = (rule_x, rule_y) right = (rule_y, -rule_x) left = (-rule_y, rule_x) back = (-rule_x, -rule_y) deltas = [forward, right, left, back] # Initialize visited visited = [[False for _ in range(N)] for _ in range(M)] queue = deque() # Check if source and destination are valid if not (0 <= src_r < M and 0 <= src_c < N) or grid[src_r][src_c] !=0: print(-1) return if not (0 <= dest_r < M and 0 <= dest_c < N) or grid[dest_r][dest_c] !=0: print(-1) return # Initialize BFS queue.append((src_r, src_c, 0)) visited[src_r][src_c] = True while queue: r, c, steps = queue.popleft() if r == dest_r and c == dest_c: print(steps) return for dr, dc in deltas: new_r = r + dr new_c = c + dc if 0 <= new_r < M and 0 <= new_c < N: if grid[new_r][new_c] ==0 and not visited[new_r][new_c]: visited[new_r][new_c] = True queue.append((new_r, new_c, steps +1)) # If destination not reached print(-1) if __name__ == "__main__": main()