Construction of New Buildings

Construction of New Buildings 

PROBLEM STATEMENT :

The program must accept a character of size R*C representing a city as the input. The matrix consists of asterisks and hyphens. Each asterisk represents a building and each hyphen represents a land. The government has planned to construct new buildings in the city based on the following condition. 

- If a land is present in one of the four corners (top-left, top-right, bottom-left and bottom-right) of a building, then a new building is allowed to construct on that land. 

The program must print the maximum number of new buildings that can be built based on the given conditions. 

Boundary Condition(s): 

2 <= R, C <= 50 

Input Format: 

The first line contains R and C separated by a space. 

The next R lines, each contains C characters separated by a space. 

Output Format: 

The first line contains the maximum number of new buildings that can be built. 


Example Input/Output 1: 
Input: () 
5 6 
- - * - * - 
- * - - - * 
* - - - - * 
- * - - - * 
* * * * - -  
Output: 10

Explanation: 

The 10 new buildings can be constructed as given below. 

# - * - * - 
- * - # # * 
* - # - # * 
# * # # # * 
* * * * # - 

Each hash symbol in the above matrix represents a new building. 

So 10 is printed as the output.  


Example Input/Output 2: 
Input: () 
3 3 
- * - 
* - * 
- * - 
Output: 0



SOLUTION :

C (Programming Language)


      

#include<stdio.h> #include<stdlib.h> int main() { int cnt=0,r,c,x,i,j,i1,j1,a1[]={1,1,-1,-1},a2[]={1,-1,1,-1}; scanf("%d%d",&r,&c); char ch[r][c],s[2]; for(i=0;i<r;++i) { for(j=0;j<c;++j) { scanf("%s",s); ch[i][j]=s[0]; } } for(i=0;i<r;++i) { for(j=0;j<c;++j) { if(ch[i][j]=='*') { for(x=0;x<4;++x) { i1=i+a1[x]; j1=j+a2[x]; if((i1>=0&&i1<r&&j1>=0&&j1<c)&&ch[i1][j1]=='-') { ch[i1][j1]='#'; cnt++; } } } } } printf("%d",cnt); }



C++ (CPP)

       

#include <bits/stdc++.h> using namespace std; int main(int argc, char** argv) { int r,c,cnt=0,i,j,x,i1,j1,a1[]={1,1,-1,-1},a2[]={1,-1,1,-1}; cin>>r>>c; char ch[r][c]; for(i=0;i<r;++i) { for(j=0;j<c;++j) { cin>>ch[i][j]; } } for(i=0;i<r;++i) { for(j=0;j<c;++j) { if(ch[i][j]=='*') { for(x=0;x<4;++x) { i1=i+a1[x]; j1=j+a2[x]; if((i1>=0&&i1<r&&j1>=0&&j1<c)&&ch[i1][j1]=='-') { ch[i1][j1]='#'; cnt++; } } } } } cout<<cnt; }



JAVA

       

import java.util.*; public class Hello { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int r=sc.nextInt(),c=sc.nextInt(),i,j,i1,j1,x,cnt=0; int a1[]={1,1,-1,-1}, a2[]={1,-1,1,-1}; char[][] ch=new char[r][c]; for(i=0;i<r;++i) { for(j=0;j<c;++j) { ch[i][j]=sc.next().charAt(0); } } for(i=0;i<r;++i) { for(j=0;j<c;++j) { if(ch[i][j]=='*') { for(x=0;x<4;++x) { i1=i+a1[x]; j1=j+a2[x]; if((i1>=0&&i1<r&&j1>=0&&j1<c)&&ch[i1][j1]=='-') { ch[i1][j1]='#'; cnt++; } } } } } System.out.println(cnt); } }



PYTHON

       

r,c=map(int,input().split()) cnt=0 l=[input().strip().split() for i in range(r)] for i in range(r): for j in range(c): dir1=[-1,1,-1,1] dir2=[-1,1,1,-1] if l[i][j]=='*': for x in range(4): i1=i+dir1[x] j1=j+dir2[x] if((i1>=0 and i1<r and j1>=0 and j1<c) and l[i1][j1]=='-'): l[i1][j1]='#' cnt+=1 print(cnt)



Never Stop Learning !!


Comments

Popular posts from this blog

DP (1) - Count number of ways to cover a distance

Zero Insert After K Times One

Left Number Twice Right