Model flow of liquid

The problem is an over simplification of the flow of liquid. - A terrain is given as a grid of cells of random elevations. The grid is always odd sized and is always a square. - A liquid is poured at the central cell. Water can flow only north-south or east-west; not diagnonally. - At the first step, the water level is the same as the central cell. - Water from one cell flows to a neighbouring cell if the level of water is equal to greater than the elevation of the neighbouring cell. - When the water flows to the neighbouring cell, the level of water is maintained. - If the water cannot flow to any new cell, the water level rises. - The simulation stops when the water reaches the end of the domain. - The output consists of the domain represented by . and W representing dry and wet terrain.

Below is an example

Input Format

7 494 88 89 770 984 726 507 340 959 220 301 639 280 290 666 906 632 824 127 505 787 673 499 843 172 193 613 154 544 211 124 60 575 572 389 635 170 174 946 593 314 300 620 167 931 780 416 954 275

Water level and location of water:``

----------

Current water level: 172

.......

.......

.......

...W...

...W...

.......

.......

----------

Current water level: 172

.......

.......

.......

...W...

..WW...

.......

.......

----------

Current water level: 172

.......

.......

.......

...W...

..WW...

.......

.......

Cannot flow, increasing water level to 173

----------

Current water level: 173

.......

.......

.......

...W...

..WW...

.......

.......

Cannot flow, increasing water level to 174

----------

Current water level: 174

.......

.......

.......

...W...

..WW...

..W....

.......

----------

Current water level: 174

.......

.......

.......

...W...

..WW...

.WW....

.......

----------

Current water level: 174

.......

.......

.......

...W...

..WW...

.WW....

.W.....

----------

Current water level: 174 Reached edge, exiting. Solution:

Output Format

.......

.......

.......

...W...

..WW...

.WW....

.W.....

Constraints: First line of the input has dimension n of (n X n) matrix. Followed by the matrix itself as shown below in the sample input.

i didn’t understand this problem if possible can anyone solve this or give me hint how i create approach for this

#include<bits/stdc++.h>
using namespace std;

void print(vector<vector>&ans){
int n=ans.size();
char st[n][n];

for(int i=0 ;i<n ;i++){
    for(int j=0 ;j<n ;j++){
        if(ans[i][j]==-1)st[i][j]='W';
        else st[i][j]='.';
    }
}

for(int i=0 ;i<n ;i++){
    for(int j=0 ;j<n ;j++){
       cout<<st[i][j];
    }
    cout<<endl;
}

}

bool reachEnd;
void dfs(int r , int c ,vector<vector>&arr , vector<vector>&ans , int drow , int dcol , int &ini , int &tempr , int &tempc){
ans[r][c]=-1;
int n=arr.size();
if(r==0 || r==n-1 || c==0 || c==n-1){
reachEnd=true;
return;
}
tempr=r;
tempc=c;
for(int i=0 ;i<4 ; i++){
int nrow=r+drow[i];
int ncol=c+dcol[i];

    if(nrow>=0 && nrow<n && ncol>=0 && ncol<n && ans[nrow][ncol]!=-1 &&  arr[nrow][ncol]<ini){
       dfs(nrow ,ncol ,arr, ans , drow ,dcol ,ini , tempr ,tempc);  
    }
}

}
void solve(vector<vector>&arr , int sr , int sc ,int n){

 int ini=arr[sr][sc];
 vector<vector<int>>ans=arr;
 ans[sr][sc]=-1;
 int tempr=n/2,tempc=n/2;
 int drow[4]={-1,0,+1,0};
 int dcol[4]={0,+1 ,0,-1};
 while(!reachEnd){
 dfs(sr,sc,arr,ans , drow ,dcol ,ini , tempr ,tempc);
 ini++;
 sr=tempr;
 sc=tempc;
}


if(reachEnd){
    print(ans);
}

}

int main(){
int n;
cin>>n;

vector<vector>arr(n,vector(n));
for(int i=0 ;i<n ;i++){
for(int j=0 ;j<n ;j++){
cin>>arr[i][j];
}
}

solve(arr,n/2,n/2 ,n);
return 0;
}

//failed on private test case but

bro it doesn’t work on any test Case :frowning:

1 Like

bro got the solution please tell me

Nhi yaar goat grazing wala bna hai wo bhi 5/10 ho rahe hai

pass karne ke liye kitna karna h minimum

its the last day of test, did anyone find solution

fist u send then i will

I’ll send goat problem send me model flow of liquid

passed 7 out of 10 testcases

Can anyone share the solution to both the problems?

join this here is the solution

K send

Giving totally wrong output consider increasing the water level