Puzzled Grid
Practice
3 (2 votes)
Algorithms
Binary search
Medium
Searching
Problem
33% Success 728 Attempts 50 Points 2s Time Limit 256MB Memory 1024 KB Max Code

Today, you need to solve a problem on a Puzzled Grid game. "In a grid of \(n \times n\) size, where each cell has a positive integer, two players compete in several rounds.

Each round starts by both players putting a rock in a random location of the grid. Then, the two players must connect the two rocks with a line that passes through the grid cells (e.g: from a grid cell the line can can expand either horizontally or vertically and never out the grid). Since there may be a lot of ways to connect the two stones, we are only interested in those lines that minimizes the highest cell in their path (e.g: the cell with the highest number should be as small as possible).

You need to find and print this minimized maximum. Can you do it?

Input:

First line contains two integers n and Q, where n is the grid size and Q is the number of rounds of the game. Next n lines contain each n integer denoting the grid cell numbers. Then, next Q lines contain each 4 integers \(x1, y1, x2, y2\) denoting the first and second stone locations -- the indexes are 0 based and the origin is the top leftmost cell.

Output:

Print Q lines, each denoting the answer for each round.

Constraints:

  • \(2 \le n \le 500\).
  • Grid cell numbers are less than or equal \(10^6\).
  • At the start of each round the stones are in different locations.
  • \(1 \le Q \le 10^5\).

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
No similar problems found