Bomb Blast - No Water
Bomb Blast - No Water
PROBLEM STATEMENT :
A matrix of size R*C containing positive integer values is passed as the input. An integer value which is a multiple of 5 is a bomb and will destroy all four adjacent cells (in the following order - left, right, top and bottom). But if the adjacent cell is water (the integer value will be a multiple of 3), then the bomb will stop destroying the adjacent cells. If a cell is destroyed then the value of that cell becomes 0. After the bomb is blasted then the cell value becomes 0. If the cell value is both a multiple of 5 and 3, then consider that as water. Finally, the program must print the values in the matrix as the output.
Note: The bombs are triggered from right to left starting from the last row.
Boundary Condition(s):
1 <= R, C <= 100
Input Format:
The first line contains R and C separated by a space.
The next R lines contain C integer values each.
Output Format:
The first R lines contain C integer values each.
Example Input/Output 1:
Input: ()
3 3
5 5 5
61 20 1
15 6 10
Output:
0 0 0
0 0 0
15 6 0
Explanation:
After the 1st bomb 10 in the last row is triggered, the left value is 6 and is water. So the explosion stops. Hence the matrix state is
61 20 1
15 6 0
Now 15 is both a multiple of 5 and 3. Hence it is considered as water.
In the 2nd row from the bottom, 20 is a bomb and it destroys the left cell with 61, right cell with 1, the top cell with 5. The bottom cell is water and hence remains unchanged. Hence the matrix state is
0 0 0
15 6 0
In the 3rd row from the bottom, two bombs(with the values 5) explode and hence the final state is
0 0 0
15 6 0
Example Input/Output 2:
Input: ()
3 3
5 56 51
63 22 1
15 6 10
Output:
0 0 51
63 22 1
15 6 0SOLUTION :
C (Programming Language)
#include<stdio.h>
#include<stdlib.h>
int R,C;
int mat[100][100];
int isValidBomb(int N)
{
return N%5==0 && N%3!=0 ? 1 : 0;
}
int isNotWater(int row,int col)
{
if(row<=R-1 && col<=C-1 && row>=0 && col>=0)
{
if(mat[row][col]%3 == 0) return 0;
else return 1;
}
return -1;
}
int main()
{
scanf("%d %d",&R,&C);
for(int i=0;i<R;i++)
{
for(int j=0;j<C;j++)
{
scanf("%d ",&mat[i][j]);
}
}
for(int i=R-1;i>=0;i--)
{
for(int j=C-1;j>=0;j--)
{
if(isValidBomb(mat[i][j]))
{
mat[i][j]=0;
if(isNotWater(i,j-1)==1) mat[i][j-1]=0;
else if(isNotWater(i,j-1)==0) continue;
if(isNotWater(i,j+1)==1) mat[i][j+1]=0;
else if(isNotWater(i,j+1)==0) continue;
if(isNotWater(i-1,j)==1) mat[i-1][j]=0;
else if(isNotWater(i-1,j)==0)continue;
if(isNotWater(i+1,j)==1) mat[i+1][j]=0;
else if(isNotWater(i+1,j)==0) continue;
}
}
}
for(int i=0;i<R;i++)
{
for(int j=0;j<C;j++)
{
printf("%d ",mat[i][j]);
}
printf("\n");
}
}
C++ (CPP)
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv)
{
int r , c ;
cin >> r >> c ;
int mat[r][c];
for(int i = 0 ; i < r ; i++)
{
for(int j = 0 ; j<c ; j++)
cin >> mat[i][j];
}
for(int i = r-1 ; i>=0 ; i--)
{
for(int j = c-1 ; j>=0 ; j--)
{
if(mat[i][j]%3 && mat[i][j]%5==0)
{
mat[i][j] = 0;
if(j-1>=0)
{
if(mat[i][j-1]%3==0)
continue;
else
mat[i][j-1]=0;
}
if(j+1<c)
{
if(mat[i][j+1]%3==0)
continue;
else
mat[i][j+1]=0;
}
if(i-1>=0)
{
if(mat[i-1][j]%3==0)
continue;
else
mat[i-1][j]=0;
}
if(i+1<r)
{
if(mat[i+1][j]%3==0)
continue;
else
mat[i+1][j]=0;
}
}
}
}
for(int i = 0 ; i<r ; i++)
{
for(int j = 0 ; j<c ; j++)
cout << mat[i][j] << " ";
cout << endl;
}
}
JAVA
import java.util.*;
public class Hello {
public static void main(String[] args) {
//Your Code Here
Scanner sc=new Scanner(System.in);
int i,j,k,l,n,r,c;
r=sc.nextInt();
c=sc.nextInt();
int a[][]=new int[r][c];
for(i=0;i<r;i++){
for(j=0;j<c;j++){
a[i][j]=sc.nextInt();
}
}
for(i=r-1;i>=0;i--){
for(j=c-1;j>=0;j--){
if(a[i][j]%5==0 && a[i][j]%3!=0){
a[i][j]=0;
if(j-1>=0){
if(a[i][j-1]%3==0) continue;
a[i][j-1]=0;
}
if(j+1<c){
if(a[i][j+1]%3==0) continue;
a[i][j+1]=0;
}
if(i-1>=0){
if(a[i-1][j]%3==0) continue;
a[i-1][j]=0;
}
if(i+1<r){
if(a[i+1][j]%3==0) continue;
a[i+1][j]=0;
}
}
}
}
for(i=0;i<r;i++){
for(j=0;j<c;j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
PYTHON
r,c=map(int,input().split())
l1=[0,0,-1,1]
l2=[-1,1,0,0]
l=[list(map(int,input().split())) for i in range(r)]
for i in range(r-1,-1,-1):
for j in range(c-1,-1,-1):
if l[i][j]%5==0 and l[i][j]%3!=0:
l[i][j]=0
for k in range(4):
i1=i+l1[k]
j1=j+l2[k]
if i1>=0 and i1<r and j1>=0 and j1<c:
if l[i1][j1]%3==0:
break
else:
l[i1][j1]=0
for i in l:
print(*i)
Comments
Post a Comment