BF21JD - Editorial

PROBLEM LINK:

[Practice ]( Washout | CodeChef)

Setter: Rounak Das
Tester: Aritra Banerjee
Editorialist: CodeChef CIEM Chapter

PROBLEM EXPLANATION

There is a building with N floors (numbered 1 through N from bottom to top); on each floor, there are M windows (numbered 1 through M from left to right). Let’s denote the j-th window on the i-th floor by (i,j).

All windows in the building need to be cleaned one by one in a specific order. You are given this order as a matrix with N rows and M columns; for each valid i and j, the window (i,j) must be cleaned on the A_{i,j}-th turn.

Whenever we clean a window (i,j), it becomes clean, but the dirty water used for cleaning it flows down to windows (i−1,j−1), (i−1,j) and (i−1,j+1) (more precisely, to all of these windows which exist), so these windows become dirty again. Then, the water flows further down.

The next window is cleaned only after water flows down completely. Note that each window is cleaned only once and there may be dirty windows at the end.

For each window, determine whether it will be clean after the cleaning process ends.

Complexity : O(N*M)O(N∗M)

DIFFICULTY:

Easy

SOLUTION (in C) :

#include<stdio.h>
int MAX(int a,int b){return a<b?b:a;}
int main(){
  int n,m,h,w,i,j,k,d[1010][1010],e[1010][1010];
  scanf("%d",&m);
  while(m--){
    scanf("%d %d",&h,&w);
  for(i=0;i<h;i++){
  for(j=0;j<w;j++){
scanf("%d",&d[i][j]);
e[i][j]=d[i][j];
  }
  }
for(i=0;i<h;i++){
  for(j=0;j<w;j++){
for(k=-1;k<2;k++){
  if(j+k<0||j+k==w)continue;
  e[i+1][j+k]=MAX(e[i+1][j+k],e[i][j]);
}
  }
}
for(i=0;i<h;i++){
  for(j=0;j<w;j++)printf("%d",d[i][j]==e[i][j]);
  printf("\n");
}
}
  return 0;
}