Split Houses
Problem
You live in a village. The village can be represented as a line that contains n grids. Each grid can be denoted as a house that is marked as H or a blank space that is marked as .
A person lives in each house. A person can move to a grid if it is adjacent to that person. Therefore, the grid must be present on the left and right side of that person.
Now, you are required to put some fences that can be marked as B on some blank spaces so that the village can be divided into several pieces. A person cannot walk past a fence but can walk through a house.
You are required to divide the house based on the following rules:
- A person cannot reach a house that does not belong to that specific person.
- The number of grids each person can reach is the same and it includes the grid in which the house is situated.
- In order to show that you are enthusiastic and if there are many answers, then you are required to print the one where most fences are placed.
Your task is to decide whether there is a possible solution. Print the possible solution.
C Solution:
- #include<stdio.h>
- int main()
- { int i, n;
- char ch[20];
- scanf("%d",&n);
- scanf("%s",&ch);
- for(i=0; i<n; i++) {
- if(ch[i]=='H' && ch[i+1]=='H') {
- printf("NO");
- return 0;
- }
- else if(ch[i]=='.')
- ch[i]='B';
- }
- printf("YES\n");
- for(i=0; i<n; i++)
- printf("%c",ch[i]);
- return 0;
- }
Python Solution:
intInput=int(input())
stringInput=input()
count=0
if "HH" in StringInput:
print("NO")
else:
print("YES")
count=1
if count==1:
stringInput=StringInput.replace(".","B")
print(stringInput)
JAVA Solution:
- import java.util.Scanner;
- public class SplitHouse {
- public static void main(String args[] ) throws Exception {
- Scanner in = new Scanner(System.in);
- int n = in.nextInt();
- char[] ch = new char[n];
- for(int i=0;i<n;i++){
- ch[i] = in.next().charAt(0);
- }
- try {
- for(int j=0;j<n+1;j++) {
- if((ch[j]=='H')&&(ch[j+1]=='H')) {
- System.out.println("No");
- }
- else {
- if(ch[j]=='.') {
- ch[j]='B';
- }
- }
- }
- }
- catch(Exception ArrayIndexOutOfBoundException) {
- }
- for(int ai=0;ai<n;ai++){
- System.out.println(ch[ai]);
- }
- }
- }
C++ Solution:
- #include<bits/stdc++.h>
- #include <iostream>
- using namespace std;
- int main(){
- int n,flag=0;
- string str;
- cin>>n>>str;
- for(int i=0;i<n;i++){
- if(str[i]=='H' && str[i+1]=='H') flag=1;
- else if(str[i]=='.') str[i]='B';
- }
- if(flag==1) cout<<"NO";
- if(flag!=1){
- cout<<"YES"<<endl;
- cout<<str<<endl;
- }
- }
Kotlin Solution:
- fun main() {
- val n = readLine()!!.toInt()
- val input = readLine()!!.toCharArray()
- var ifPossible = false
- if (input.contains('.')) {
- for (i in input.indices) {
- if (input[i] == '.') {
- input[i] = 'B'
- ifPossible = true
- }
- if (i != input.size - 1) {
- if (input[i] == 'H' && input[i + 1] == 'H') {
- ifPossible = false
- break
- }
- }
- }
- } else if (n == 1 && input[0] == 'H')
- ifPossible = true
- if (ifPossible) {
- println("YES")
- println(input)
- } else
- println("NO")
- }
Note:
We
Are Providing Every Content in this Blog Inspired From Many People and
Provide it Open To All People Who Are Willing Gain Some Stuff in
Computer Science.
Comment below👇 for Any Errors & Doubts.
Post a Comment
Post a Comment
Learn, Share and Enjoy