# 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 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 row has ‘‘ and every cell in column contains ‘‘, therefore the house cannot be cleaned. 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; ` `} ` |

*chevron_right*

*filter_none*

Output:

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

## Recommended Posts:

- Arrow operator -> in C/C++ with Examples
- Minimum increment or decrement operations required to make the array sorted
- Longest Increasing Subsequence using Longest Common Subsequence Algorithm
- Minimum number of additons to make the string balanced
- Find a pair (n,r) in an integer array such that value of nPr is maximum
- Find a pair (n,r) in an integer array such that value of nCr is maximum
- Minimize the cost of partitioning an array into K groups
- Range maximum query using Sparse Table
- Maximize the Expression | Bit Manipulation
- What is Competitive Programming and How to Prepare for It?
- Print nodes having maximum and minimum degrees
- Maximum length sub-array which satisfies the given conditions
- Largest sub-matrix with all equal elements
- Color all boxes in line such that every M consecutive boxes are unique

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.