Algorithm
Problem link- https://www.spoj.com/problems/WATER/
WATER - Water among Cubes
On a rectangular mesh comprising n*m fields, n*m cuboids were put, one cuboid on each field. A base of each cuboid covers one field and its surface equals to one square inch. Cuboids on adjacent fields adhere one to another so close that there are no gaps between them. A heavy rain pelted on a construction so that in some areas puddles of water appeared.
Task
Write a program which:
- reads from the standard input a size of the chessboard and heights of cuboids put on the fields,
- computes maximal water volume, which may gather in puddles after the rain,
- writes results in the standard output.
Input
The number of test cases t is in the first line of input, then t test cases follow separated by an empty line. In the first line of each test case two positive integers 1 <= n <= 100, 1 <= m <= 100 are written. They are the size of the mesh. In each of the following n lines there are m integers from the interval [1..10000]; i-th number in j-th line denotes a height of a cuboid given in inches put on the field in the i-th column and j-th raw of the chessboard.
Output
Your program should write for each tes case one integer equal to the maximal volume of water (given in cubic inches), which may gather in puddles on the construction.
Example
Sample input: 1 3 6 3 3 4 4 4 2 3 1 3 2 1 4 7 3 1 6 4 1 Sample output: 5
The picture below shows the mesh after the rain (seen from above). Puddles are drawn in gray.
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <sstream>
#include <fstream>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
#define INF (1<<25)
typedef bool (*comp)(pair<ii,int> , pair<ii,int> );
vector<vi> grid;
vector<vi>visited;
int m,n;
int res;
bool compare (pair<ii,int> a, pair<ii,int> b){
return a.second > b.second;
}
int dx[] = {1,-1,0,0};
int dy[] = {0,0,-1,1};
void floodfill(){
priority_queue<pair<ii,int> , vector<pair<ii,int> > , comp> pq(compare);
for(int j=0;j<m;++j){
pq.push(pair<ii,int> (ii(0,j),grid[0][j]));
}
for(int j=0;j<m;++j){
pq.push(pair<ii,int> (ii(n-1,j),grid[n-1][j]));
}
for(int j=1;j<n-1;++j){
pq.push(pair<ii,int> (ii(j,0),grid[j][0]));
}
for(int j=1;j<n-1;++j){
pq.push(pair<ii,int> (ii(j,m-1),grid[j][m-1]));
}
while(!pq.empty()){
pair<ii,int> top1 = pq.top();
while(!pq.empty()){
pair<ii,int> top2 = pq.top();
if(top2.second>top1.second){
break;
}
pq.pop();
if(visited[top2.first.first][top2.first.second]++){
continue;
}
res+=top1.second-top2.second;
for(int i=0;i<4;++i){
int cx=top2.first.first+dx[i];
int cy=top2.first.second+dy[i];
if(cx>=0&&cx<n&&cy>=0&&cy<m){
pq.push(pair<ii,int> (ii(cx,cy),grid[cx][cy]));
}
}
}
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
res = 0;
scanf("%d %d",&n,&m);
grid = vector<vi> (n,vi(m,0));
visited = vector<vi> (n,vi(m,0));
for(int i=0;i<n;++i){
for(int j=0;j<m;++j){
scanf("%d",&grid[i][j]);
}
}
floodfill();
printf("%d\n",res);
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
3 6
3 3 4 4 4 2
3 1 3 2 1 4
7 3 1 6 4 1
Output
Demonstration
SPOJ Solution-WATER Water among Cubes-Solution in C, C++, Java, Python,SPOJ Solution