- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
- Related Articles
- State for each of the following whether it is due to evaporation or condensation:(a) Water drops appear on the outer surface of a glass containing cold water.(b) Steam rising from wet clothes while they are ironed.(c) Fog appearing on a cold winter morning.(d) Blackboard dries up after wiping it.(e) Steam rising from a hot girdle when water is sprinkled on it.
- What factors could lead to the rising of a new species?
- Water and Jug Problem in C++
- Container with Most Water in C++
- Ocean currents are caused due to a) Convection in ocean water b) Radiation in ocean water c) Conduction in ocean water d) Large volume of ocean water
- Which of the following statement is incorrect for penguins?(a) They huddle together(b) They cannot swim(c) They have webbed feet(d) They have streamlined body
- We see the sun rising $8frac{1}{4}$ minutes after it has already risen. Why?
- Program to check if water tank overflows when n solid balls are dipped in the water tank in C++
- Solids and gases mix/dissolve in water(a) because water is a good solvent(b) because water has intermolecular space(c) diffusion is faster in water(d) all of the above
- Transpiration is a process in which plants(a) receive water from soil(b) absorb water vapour from air(c) prepare food from water(d) release water vapour.
- Which condition out of the following will increase the evaporation of water?(a) Increase in temperature of water.(b) Decrease in temperature of water.(c) Less exposed surface area of water.(d) Adding common salt to water.
- Clouds are(a) tiny drops of water floating in air(b) mixture of dust and water vapour(c) particles of water vapour(d) rain drops in air.
- The correct way of making a solution of acid in water is to(a) add water to acid(b) add acid to water(c) mix acid and water simultaneously(d) add water to acid in a shallow container
- Find amount of water wasted after filling the tank in C++
- What is the speed of sound:(a) in air? (b) in water? (c) in iron?

Advertisements