 # RESERVOI - Editorial

Practice

Contest

Author: Praveen Dhinwa

Tester: Istvan Nagy

Editorialist: Misha Chorniy

Simple

# Pre-Requisites:

none

## Problem Statement

You are given matrix which presents a water reservoir, each cell can be water, air or brick. You need to check whether a state of the reservoir is stable or not? The state is stable if it will remain in the same state forever.

## Explanation

There are no water cells in matrix. We need to check if no one brick don’t have air under itself. If brick has air under itself, it fall down.

``````
ok = true
for i=1..n-1
for j=1..m
if s[i][j]=='B' and s[i+1][j]== 'A' //this block (i, j) will fall down
ok = false

``````

Observe that air can’t change something, we are interested in movements of bricks and water. Brick only can fall down, if under isn’t brick. Water can move to the left(if there are empty or air), to the right(if there are empty or air) or move down(if there are nothing or air).

``````
ok = true
for i=1..n
for j=1..m
if s[i][j]=='B' and i+1<=n and s[i+1][j]!='B'	// brick (i, j) fall down
ok = false
if s[i][j]=='W'
if j==1 or j==m or i==n	//water overflow bounds
ok = false
else if s[i][j-1]=='A' or s[i][j+1]=='A' or s[i+1][j]=='A'	//water can move into adjacent blocks(left, right, down)
ok = false

``````

Overall time complexity of this approach is O(N*M).

## Solution:

Setter’s solution can be found here

Please feel free to post comments if anything is not clear to you.

I do this much more simple I make walls of air on left and right side of this matrix and put a brick base as

``````       A            A
A            A
.            .
.            .
A            A
BBB...BBBBBBBB
``````

Then checked if there is air in left, right, or below water block then it fails and if there is any water or air block below brick then it also fails these two conditions are sufficient to solve this problem. My solution is here

1 Like

Setter solution does not pass the test case
1
2 4
BWWB
BWWB
He did not consider that if there is water in nth row the system is unstable.

According to me we should not check at if(i == n) for water as I hope it is allowed to place it at the bottom but surrounded by the bricks. As BWWWWB is correct as it also get passed and according to your editorials it would not get correct. So please make the questions more clear to understand!

//