- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Swim in Rising Water in C++
Suppose we have one N x N grid, each square grid[i][j] represents the elevation at that point (i,j). Now consider it has started raining. At time t, the depth of the water everywhere is t. We can swim from a square to another 4-directionally adjacent square when elevation of both squares individually is at most t. We can swim infinite distance in zero time.
We should start from position (0, 0). We have to find the least time until we can reach the bottom right square (N-1, N-1)
So if the input is like
0 | 1 | 2 | 3 | 4 |
24 | 23 | 22 | 21 | 5 |
12 | 13 | 15 | 15 | 16 |
11 | 17 | 18 | 19 | 20 |
10 | 9 | 8 | 7 | 6 |
The correct way is colored. So the answer will be 16.
To solve this, we will follow these steps −
- Define Data, this will take three parameters like time, x and y.
- Define an array dir of size: 4 x 2 := { { 1, 0 }, { - 1, 0 }, { 0, 1 }, { 0, - 1 } }
- n := row of grid, m := column of grid
- define priority queue q
- Define one 2D array visited of size n x m, fill this with 0
- visited[0, 0] := 1
- insert Data(grid[0, 0], 0, 0) into q
- while (not q is empty), do −
- node = top element of q and delete element from q
- time := time of node
- x := x of node, y := y of node
- if x is same as n - 1 and y is same as m - 1, then −
- return time
- for initialize i := 0, when i < 4, update (increase i by 1), do −
- nx := dir[i, 0] + x, ny := dir[i, 1] + y
- if nx >= 0 and nx < n and ny >= 0 and ny < m and not visited[nx, ny] then −
- visited[nx, y] := 1
- insert Data(maximum of grid[nx, ny] and time, nx, ny) into q
- return -1
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; struct Data{ int time, x, y; Data(int a, int b, int y){ time = a; x = b; this->y = y; } }; struct Comparator{ bool operator()(Data a, Data b){ return !(a.time < b.time); } }; int dir[4][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1}}; class Solution { public: int swimInWater(vector<vector<int>>& grid) { int n = grid.size(); int m = grid[0].size(); priority_queue <Data, vector <Data>, Comparator> q; vector < vector <int> > visited(n, vector <int>(m, 0)); visited[0][0] = 1; q.push(Data(grid[0][0], 0, 0)); while(!q.empty()){ Data node = q.top(); q.pop(); int time = node.time; int x = node.x; int y = node.y; if(x == n - 1 && y == m - 1)return time; for(int i = 0; i < 4; i++){ int nx = dir[i][0] + x; int ny = dir[i][1] + y; if(nx >= 0 && nx < n && ny >= 0 && ny < m && !visited[nx][ny]){ visited[nx][y] = 1; q.push(Data(max(grid[nx][ny], time), nx, ny)); } } } return -1; } }; main(){ Solution ob; vector<vector<int>> v = {{0,1,2,3,4},{24,23,22,21,5},{12,13,15,15,16},{11,17,18,19,20}, {10,9,8,7,6}}; cout << (ob.swimInWater(v)); }
Input
{{0,1,2,3,4},{24,23,22,21,5},{12,13,15,15,16},{11,17,18,19,20},{10,9,8,7,6}}
Output
16
Advertisements