Thursday, July 4, 2024

Cleaning the room

Given a room with square grids having ‘*’ and ‘.’ representing untidy and normal cells respectively. You need to find whether room can be cleaned or not. There is a machine which helps you in this task, but it is capable of cleaning only normal cell. Untidy cells cannot be cleaned with machine, until you have cleaned the normal cell in its row or column. Now, see to it whether room can be cleaned or not. The input is as follows : First line contains n         the size of the room. The next n lines contains description for each row where the row[i][j] is ‘*         ‘ if it is more untidy than others else it is ‘.         ‘ if it is normal cell. Examples:

Input : 3
        .**
        .**
        .**
Output :Yes, the room can be cleaned.
        1 1
        2 1
        3 1
Input :4
       ****
       ..*.
       ..*.
       ..*.
Output : house cannot be cleaned.

Approach : The minimum number of cells can be n. It is the only answer possible as it need to have an element of type ‘.         ‘ in every different row and column. If particular column and a given row contain ‘*         ‘ in all the cells then, it is known that the house cannot be cleaned. Traverse every row and find the ‘.         ‘ that can be used for the machine. Use this step two times, check every column for every row and then check for every row for every column. Then check if any of the two gives answer as n. If yes then house can be cleaned otherwise not. This approach will give us the minimum answer required. In the first example the machine will clean cell (1, 1), (2, 1), (3, 1) in order to clean the entire room. In the second example every cell in the 1^{st}         row has ‘*         ‘ and every cell in 3^{rd}         column contains ‘*         ‘, therefore the house cannot be cleaned. 1^{st}         row cannot be cleaned in any way. 

C++




// CPP code to find whether
// house can be cleaned or not
#include <bits/stdc++.h>
using namespace std;
 
// Matrix A stores the string
char A[105][105];
 
// ans stores the pair of indices
// to be cleaned by the machine
vector<pair<int, int> > ans;
 
// Function for printing the
// vector of pair
void print()
{
    cout << "Yes, the house can be"
         << " cleaned." << endl;
 
    for (int i = 0; i < ans.size(); i++)
        cout << ans[i].first << " "
             << ans[i].second << endl;
}
 
// Function performing calculations
int solve(int n)
{
    // push every first cell in
    // each row containing '.'
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (A[i][j] == '.') {
                ans.push_back(make_pair(i + 1, j + 1));
                break;
            }
        }
    }
 
    // If total number of cells are
    // n then house can be cleaned
    if (ans.size() == n) {
        print();
        return 0;
    }
 
    ans.clear();
 
    // push every first cell in
    // each column containing '.'
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (A[j][i] == '.') {
                ans.push_back(make_pair(i + 1, j + 1));
                break;
            }
        }
    }
 
    // If total number of cells are
    // n then house can be cleaned
    if (ans.size() == n) {
        print();
        return 0;
    }
    cout << "house cannot be cleaned."
         << endl;
}
 
// Driver function
int main()
{
    int n = 3;
    string s = "";
    s += ".**";
    s += ".**";
    s += ".**";
    int k = 0;
 
    // Loop to insert letters from
    // string to array
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            A[i][j] = s[k++];
    }
    solve(n);
    return 0;
}


Java




import java.util.*;
 
class Main {
    // Matrix A stores the string
    static char[][] A = new char[105][105];
 
    // ans stores the pair of indices
    // to be cleaned by the machine
    static ArrayList<Pair<Integer, Integer>> ans = new ArrayList<>();
 
    // Function for printing the
    // ArrayList of pair
    static void print() {
        System.out.println("Yes, the house can be cleaned.");
 
        for (Pair<Integer, Integer> p : ans)
            System.out.println(p.getKey() + " " + p.getValue());
    }
 
    // Function performing calculations
    static int solve(int n) {
        // push every first cell in
        // each row containing '.'
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (A[i][j] == '.') {
                    ans.add(new Pair<>(i + 1, j + 1));
                    break;
                }
            }
        }
 
        // If total number of cells are
        // n then house can be cleaned
        if (ans.size() == n) {
            print();
            return 0;
        }
 
        ans.clear();
 
        // push every first cell in
        // each column containing '.'
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (A[j][i] == '.') {
                    ans.add(new Pair<>(i + 1, j + 1));
                    break;
                }
            }
        }
 
        // If total number of cells are
        // n then house can be cleaned
        if (ans.size() == n) {
            print();
            return 0;
        }
        System.out.println("house cannot be cleaned.");
        return 0;
    }
 
    // Driver function
    public static void main(String[] args) {
        int n = 3;
        String s = "";
        s += ".**";
        s += ".**";
        s += ".**";
        int k = 0;
 
        // Loop to insert letters from
        // string to array
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++)
                A[i][j] = s.charAt(k++);
        }
        solve(n);
    }
}
 class Pair<K, V> {
    private final K key;
    private final V value;
 
    public Pair(K key, V value) {
        this.key = key;
        this.value = value;
    }
 
    public K getKey() {
        return key;
    }
 
    public V getValue() {
        return value;
    }
}


Python3




# Python3 code to find whether
# house can be cleaned or not
 
# Matrix A stores the string
A = [[0 for i in range(105)] for j in range(105)]
 
# ans stores the pair of indices
# to be cleaned by the machine
ans = []
 
# Function for printing the
# vector of pair
def printt():
     
    print("Yes, the house can be cleaned.")
    for i in range(len(ans)):
        print(ans[i][0], ans[i][1])
         
# Function performing calculations
def solve(n):
    global ans
     
    # push every first cell in
    # each row containing '.'
    for i in range(n):
        for j in range(n):
            if (A[i][j] == '.'):
                ans.append([i + 1, j + 1])
                break
             
    # If total number of cells are
    # n then house can be cleaned
    if (len(ans) == n):
        printt()
        return 0
         
    ans = []
     
    # push every first cell in
    # each column containing '.'
    for i in range(n):
        for j in range(n):
            if (A[j][i] == '.'):
                ans.append([i + 1, j + 1])
                break
             
    # If total number of cells are
    # n then house can be cleaned
    if (len(ans) == n):
        printt()
        return 0
    print("house cannot be cleaned.")
 
# Driver function
n = 3
s = ""
s += ".**"
s += ".**"
s += ".**"
k = 0
 
# Loop to insert letters from
# string to array
for i in range(n):
    for j in range(n):
        A[i][j] = s[k]
        k += 1
 
solve(n)
 
# This code is contributed by shubhamsingh10


Javascript




// JS code to find whether
// house can be cleaned or not
 
// Matrix A stores the string
let A = [];
 
// ans stores the pair of indices
// to be cleaned by the machine
let ans = [];
 
// Function for printing the
// vector of pair
function print() {
console.log("Yes, the house can be cleaned."+"<br>");
 
for (let i = 0; i < ans.length; i++)
console.log(ans[i][0] + " " + ans[i][1] + "<br>");
}
 
// Function performing calculations
function solve(n) {
// push every first cell in
// each row containing '.'
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (A[i][j] == ".") {
ans.push([i + 1, j + 1]);
break;
}
}
}
 
// If total number of cells are
// n then house can be cleaned
if (ans.length == n) {
print();
return 0;
}
 
ans = [];
 
// push every first cell in
// each column containing '.'
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (A[j][i] == ".") {
ans.push([i + 1, j + 1]);
break;
}
}
}
 
// If total number of cells are
// n then house can be cleaned
if (ans.length == n) {
print();
return 0;
}
document.write("house cannot be cleaned.");
}
 
// Driver function
function main() {
let n = 3;
let s = "...**";
let k = 0;
 
// Loop to insert letters from
// string to array
for (let i = 0; i < n; i++) {
A.push([]);
for (let j = 0; j < n; j++) A[i].push(s[k++]);
}
 
solve(n);
}
 
main();


C#




// C# code to find whether
// house can be cleaned or not
using System;
using System.Collections.Generic;
 
public class Program
{
 
// Matrix A stores the string
static char[,] A = new char[105, 105];
// ans stores the pair of indices
// to be cleaned by the machine
static List<Tuple<int, int>> ans = new List<Tuple<int, int>>();
 
 
// Function for printing the
// vector of pair
static void Print()
{
    Console.WriteLine("Yes, the house can be cleaned.");
 
    for (int i = 0; i < ans.Count; i++)
    {
        Console.WriteLine(ans[i].Item1 + " " + ans[i].Item2);
    }
}
 
// Function performing calculations
static int Solve(int n)
{
     // push every first cell in
    // each row containing '.'
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (A[i, j] == '.')
            {
                ans.Add(Tuple.Create(i + 1, j + 1));
                break;
            }
        }
    }
 
    // If total number of cells are
    // n then house can be cleaned
    if (ans.Count == n)
    {
        Print();
        return 0;
    }
 
    ans.Clear();
   // push every first cell in
    // each column containing '.'
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (A[j, i] == '.')
            {
                ans.Add(Tuple.Create(i + 1, j + 1));
                break;
            }
        }
    }
 
    // If total number of cells are
    // n then house can be cleaned
    if (ans.Count == n)
    {
        Print();
        return 0;
    }
 
    Console.WriteLine("house cannot be cleaned.");
    return -1;
}
//Driver code
static void Main()
{
    int n = 3;
    string s = "";
    s += ".**";
    s += ".**";
    s += ".**";
    int k = 0;
 
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            A[i, j] = s[k++];
        }
    }
 
    Solve(n);
}
}


Output

Yes, the house can be cleaned.
1 1
2 1
3 1

Time Complexity: O(n2)
Auxiliary Space: O(n2)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Thapelo Manthata
I’m a desktop support specialist transitioning into a SharePoint developer role by day and Software Engineering student by night. My superpowers include customer service, coding, the Microsoft office 365 suite including SharePoint and power platform.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments