A prison is represented as an 𝑁×𝑀N×M grid containing 0's and 1's, where 0 denotes a prisoner and 1 denotes a guard.In one move, a prisoner can move one step left, right, up, or down. Formally, from cell (𝑥,𝑦)(x,y), a prisoner can move to any of the four cells (𝑥−1,𝑦),(𝑥+1,𝑦),(𝑥,𝑦−1),(𝑥,𝑦+1)(x−1,y),(x+1,y),(x,y−1),(x,y+1).A prisoner can escape if he reaches outside the prison: formally, if he reaches any cell (𝑥,𝑦)(x,y) such that 𝑥=0x=0 or 𝑦=0y=0 or 𝑥>𝑁x>N or 𝑦>𝑀y>M.Let the escape time of a prisoner be the minimum number of guards he needs to cross before he is able to escape the prison.What is the maximum escape time of any prisoner?Input FormatThe first line of input will contain a single integer 𝑇T, denoting the number of test cases.Each test case consists of multiple lines of input.The first line of each test case contains two space-separated integers 𝑁N and 𝑀M — the dimensions of the prison.The next 𝑁N lines each contain a binary string of length 𝑀M, representing the structure of the prison.Output FormatFor each test case, output on a new line the maximum escape time of any prisoner.Constraints1≤𝑇≤10001≤T≤10001≤𝑁≤3⋅1051≤N≤3⋅10 5 1≤𝑀≤3⋅1051≤M≤3⋅10 5 Each character of grid is either 0 or 1.The sum of 𝑁⋅𝑀N⋅M over all test cases won't exceed 3⋅1053⋅10 5 .
Question
A prison is represented as an 𝑁×𝑀N×M grid containing 0's and 1's, where 0 denotes a prisoner and 1 denotes a guard.In one move, a prisoner can move one step left, right, up, or down. Formally, from cell (𝑥,𝑦)(x,y), a prisoner can move to any of the four cells (𝑥−1,𝑦),(𝑥+1,𝑦),(𝑥,𝑦−1),(𝑥,𝑦+1)(x−1,y),(x+1,y),(x,y−1),(x,y+1).A prisoner can escape if he reaches outside the prison: formally, if he reaches any cell (𝑥,𝑦)(x,y) such that 𝑥=0x=0 or 𝑦=0y=0 or 𝑥>𝑁x>N or 𝑦>𝑀y>M.Let the escape time of a prisoner be the minimum number of guards he needs to cross before he is able to escape the prison.What is the maximum escape time of any prisoner?Input FormatThe first line of input will contain a single integer 𝑇T, denoting the number of test cases.Each test case consists of multiple lines of input.The first line of each test case contains two space-separated integers 𝑁N and 𝑀M — the dimensions of the prison.The next 𝑁N lines each contain a binary string of length 𝑀M, representing the structure of the prison.Output FormatFor each test case, output on a new line the maximum escape time of any prisoner.Constraints1≤𝑇≤10001≤T≤10001≤𝑁≤3⋅1051≤N≤3⋅10 5 1≤𝑀≤3⋅1051≤M≤3⋅10 5 Each character of grid is either 0 or 1.The sum of 𝑁⋅𝑀N⋅M over all test cases won't exceed 3⋅1053⋅10 5 .
Solution
The problem you've presented is a classic example of a breadth-first search (BFS) problem in a 2D grid. Here's a step-by-step guide on how to solve it:
- Initialize a 2D grid with the same dimensions as the prison. This grid will be used to store the escape time for each prisoner. Set the initial value for each cell in
Similar Questions
For the most part, supermax prisons operate in a state of_______________, in which all inmates are confined to their cells and activities such as meals, recreational sports, and treatment programs are nonexistent.Group of answer choices12-hour cell confinementlimited inmate contactlockdownpodular cellblock
Look at the word and phrases below, rearrange them to form meaningful sentences in your answer sheet. (4) a) a clever/an/prisoner/planned/escape/once
We are given a two-dimensional board of size N × M (N rows and M columns). Each field of the board can be empty ('.'), may contain an obstacle ('X') or may have a character in it. The character might be either an assassin ('A') or a guard. Each guard stands still and looks straight ahead, in the direction they are facing.Every guard looks in one of four directions (up, down, left or right on the board) and is represented by one of four symbols. A guard denoted by '<' is looking to the left; by '>', to the right; '^', up; or 'v', down. The guards can see everything in a straight line in the direction in which they are facing, as far as the first obstacle ('X' or any other guard) or the edge of the board.The assassin can move from the current field to any other empty field with a shared edge. The assassin cannot move onto fields containing obstacles or enemies.Write a function:bool solution(vector<string> &B);that, given an array B consisting of N strings denoting rows of the array, returns true if is it possible for the assassin to sneak from their current location to the bottom-right cell of the board undetected, and false otherwise.Examples:1. Given B = ["X.....>", "..v..X.", ".>..X..", "A......"], your function should return false. All available paths lead through a field observed by a guard.2. Given B = ["...Xv", "AX..^", ".XX.."], your function should return true. The guard in the second row is blocking the other one from watching the bottom-right square.3. Given B = ["...", ">.A"], your function should return false, as the assassin gets spotted right at the start.4. Given B = ["A.v", ..."], your function should return false. It's not possible for the assassin to enter the bottom-right cell undetected, as the cell is observed.Write an efficient algorithm for the following assumptions:N is an integer within the range [1..500];all strings in B are of the same length M from range [1..500];there is exactly one assassin on the board;there is no guard or wall on B[N−1][M−1];every string in B consists only of the following characters '.', 'X', '<', '>', 'v', '^' and/or 'A'.
You are given four integers sx, sy, fx, fy, and a non-negative integer t.In an infinite 2D grid, you start at the cell (sx, sy). Each second, you must move to any of its adjacent cells.Return true if you can reach cell (fx, fy) after exactly t seconds, or false otherwise.A cell's adjacent cells are the 8 cells around it that share at least one corner with it. You can visit the same cell several times.
Fill in the blanks from all the option below- According to the ________ prisons are systems to reduce recidivism and their main objective is to induce _______ in/from people *1 pointSegregationUtilitariansRetributionistRehabilitationReformationDeterrenceHumanitarians
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.