Hiko Seijuurou is looking for a suitable training ground for his students. He has a found a suitably perilous terrain of MรN๐ร๐ cells. aij๐๐๐ is the altitude of the cell on the i๐-th row and the j๐-th column.Seijuurou plans to drop students at any of the cells and ask them to visit at least K๐พ other cells. A student with capability c๐ can only move to an adjacent cell (up/down/right/left) if the absolute difference in altitude between the two cells is at most c๐.For each cell, find the minimum capability a student needs to have to visit at least K๐พ other cells if they start at this cell.InputThree integers M,N,K๐,๐,๐พ on the first line.M๐ lines follow with N๐ integers on each line. The j๐-th integer on the (i+1)(๐+1)-th line is aij๐๐๐.Three integers M,N,K๐,๐,๐พ on the first line.Constraints1โคM,Nโค5001โค๐,๐โค5000โคKโคMNโ10โค๐พโค๐๐โ1โ109โคaijโค+109โ109โค๐๐๐โค+109OutputM๐ lines with N๐ integers on each line. The j๐-th integer on the i๐-th line should be the minimum capability a student needs to have to visit at least K๐พ other cells if they start at cell (i,j)(๐,๐).Examplesinput2 2 1-3 -9-8 2output5 6 5 10 input5 7 67 6 -40 -11 -16 -28 -4737 -31 -19 -18 -13 -28 3414 -6 -46 0 -16 35 22-34 -23 -42 -40 -27 11 -439 5 -30 0 50 -32 0output30 30 21 11 11 12 19 23 12 11 11 11 12 38 20 19 13 16 11 38 38 19 19 13 13 11 38 43 28 28 13 30 50 43 43 NoteIn the first example, you need to reach only one other cell.From (1,1)(1,1), if you have 55 capability, you can reach cell (2,1)(2,1).From (1,2)(1,2), if you have 66 capability, you can reach cell (1,1)(1,1).From (2,1)(2,1), if you have 55 capability, you can reach cell (1,1)(1,1).From (2,2)(2,2), if you have 1010 capability, you can reach cell (2,1)(2,1).
Question
Hiko Seijuurou is looking for a suitable training ground for his students. He has a found a suitably perilous terrain of MรN๐ร๐ cells. aij๐๐๐ is the altitude of the cell on the i๐-th row and the j๐-th column.Seijuurou plans to drop students at any of the cells and ask them to visit at least K๐พ other cells. A student with capability c๐ can only move to an adjacent cell (up/down/right/left) if the absolute difference in altitude between the two cells is at most c๐.For each cell, find the minimum capability a student needs to have to visit at least K๐พ other cells if they start at this cell.InputThree integers M,N,K๐,๐,๐พ on the first line.M๐ lines follow with N๐ integers on each line. The j๐-th integer on the (i+1)(๐+1)-th line is aij๐๐๐.Three integers M,N,K๐,๐,๐พ on the first line.Constraints1โคM,Nโค5001โค๐,๐โค5000โคKโคMNโ10โค๐พโค๐๐โ1โ109โคaijโค+109โ109โค๐๐๐โค+109OutputM๐ lines with N๐ integers on each line. The j๐-th integer on the i๐-th line should be the minimum capability a student needs to have to visit at least K๐พ other cells if they start at cell (i,j)(๐,๐).Examplesinput2 2 1-3 -9-8 2output5 6 5 10 input5 7 67 6 -40 -11 -16 -28 -4737 -31 -19 -18 -13 -28 3414 -6 -46 0 -16 35 22-34 -23 -42 -40 -27 11 -439 5 -30 0 50 -32 0output30 30 21 11 11 12 19 23 12 11 11 11 12 38 20 19 13 16 11 38 38 19 19 13 13 11 38 43 28 28 13 30 50 43 43 NoteIn the first example, you need to reach only one other cell.From (1,1)(1,1), if you have 55 capability, you can reach cell (2,1)(2,1).From (1,2)(1,2), if you have 66 capability, you can reach cell (1,1)(1,1).From (2,1)(2,1), if you have 55 capability, you can reach cell (1,1)(1,1).From (2,2)(2,2), if you have 1010 capability, you can reach cell (2,1)(2,1).
Solution
This problem is a search problem that can be solved using binary search and breadth-first search (BFS) algorithms. Here are the steps to solve it:
-
Initialize the minimum and maximum capability as the minimum and maximum altitude difference in the terrain respectively.
-
Perform a binary search between the minimum and maximum capability. For each capability 'c' in the binary search:
a. For each cell in the terrain, perform a BFS starting from this cell. In the BFS, a student can move to an adjacent cell if the absolute difference in altitude between the current cell and the adjacent cell is less than or equal to 'c'.
b. If there are at least 'K' cells that can be visited from the current cell, then update the maximum capability as 'c'. Otherwise, update the minimum capability as 'c' + 1.
-
The minimum capability for each cell is the minimum 'c' such that there are at least 'K' cells that can be visited from this cell.
This algorithm ensures that the minimum capability for each cell is found in O(MNlog(max-min)) time, where 'max' and 'min' are the maximum and minimum altitude difference in the terrain respectively. This is because the binary search takes O(log(max-min)) time and the BFS takes O(M*N) time for each 'c' in the binary search.
Similar Questions
There are n seats and n students in a room. You are given an array seats of length n, where seats[i] is the position of the ith seat. You are also given the array students of length n, where students[j] is the position of the jth student.You may perform the following move any number of times:Increase or decrease the position of the ith student by 1 (i.e., moving the ith student from positionย xย to x + 1 or x - 1)Return the minimum number of moves required to move each student to a seat such that no two students are in the same seat.Note that there may be multiple seats or students in the same position at the beginning.
You are given a map of a city represented as a 2D grid, where some cells are blocked, some are open, and some contain different types of terrain that affect the cost of traversing them. You need to find the shortest path between two points on the map while avoiding blocked cells and taking into account the terrain cost. How would you approach this problem?a)Dijkstra's Algorithmb)Breadth-First Searchc)A* Search Algorithmd)Depth-First Search
Write a short note on cell demarcation
You are on a 22-dimensional grid, where you start at (0,0)(0,0).You are given a binary string ๐S of length 44 where:๐1S 1โ refers to left direction;๐2S 2โ refers to right direction;๐3S 3โ refers to up direction;๐4S 4โ refers to down direction.๐๐=1S iโ =1 denotes that you are allowed to make a move in the respective direction and vice-versa.Find the number of cells (๐ฅ,๐ฆ)(x,y) you can possibly visit which satisfy โ10โค๐ฅ,๐ฆโค10โ10โคx,yโค10.Note that:You always include the cell (0,0)(0,0) in your answer.If you can visit (๐ด1,๐ต1)(A 1โ ,B 1โ ) and (๐ด2,๐ต2)(A 2โ ,B 2โ ) individually, but not both at the same time, you will still include both of them in your answer.Moves are defined as:A move in left direction is a move from cell (๐ด,๐ต)(A,B) to (๐ดโ1,๐ต)(Aโ1,B).A move in right direction is a move from cell (๐ด,๐ต)(A,B) to (๐ด+1,๐ต)(A+1,B).A move in up direction is a move from cell (๐ด,๐ต)(A,B) to (๐ด,๐ต+1)(A,B+1).A move in down direction is a move from cell (๐ด,๐ต)(A,B) to (๐ด,๐ตโ1)(A,Bโ1).Input FormatThe first line of input will contain a single integer ๐T, denoting the number of test cases.Each test case consists of a binary string ๐S of length 44 - denoting the directions in which moves are allowed.Output FormatFor each test case, output on a new line, the number of cells you can visit as mentioned in statement.Constraints1โค๐โค151โคTโค15โฃ๐โฃ=4โฃSโฃ=4๐๐โ{0,1}S iโ โ{0,1}๐๐=1S iโ =1 for at least one 1โค๐โค41โคiโค4.Sample 1:InputOutput5001011000110111011111121121231441Explanation:Test case 11: The only allowed direction is up. Thus, you can only visit cells (0,0),(0,1),(0,2),โฆ,(0,10)(0,0),(0,1),(0,2),โฆ,(0,10); which are a total of 1111 cells.Test case 22: The allowed directions are left and right. Thus, you can visit cells (โ10,0),(โ9,0),โฆ,(0,0),โฆ,(9,0),(10,0)(โ10,0),(โ9,0),โฆ,(0,0),โฆ,(9,0),(10,0), which are a total of 2121 cells.Test case 33: The allowed directions are right and up. You can visit all cells (๐ฅ,๐ฆ)(x,y) such that ๐ฅโฅ0xโฅ0 and ๐ฆโฅ0yโฅ0, which are a total of 121121 cells.
During the morning assembly in a school, students stand in the playground in a fixed pattern of rows. Each row consists of students of only one particular class.1) There are four more rows of Class 10 students than of Class 5 students.2) The number of rows of Class 6 students is one less than of Class 10 students.3) One of the classes has the maximum number of rows, i.e., seven.4) There are two rows of Class 5 students.5) There are two more rows of Class 8 students than of Class 5 students.6) The number of rows of Class 9 students is less than that of Class 7 students.Analyze this information and answer the question given below.Which of the following is the total number of rows of Class 10 students? 4 5 6 7 8Mark for Later
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.