📜  打扫房间

📅  最后修改于: 2021-06-27 00:19:14             🧑  作者: Mango

给定一个带有“ *”和“。”的方格的房间分别代表不整齐的细胞和正常的细胞。
您需要查找是否可以打扫房间。
有台机器可以帮助您完成此任务,但只能清洁普通电池。除非您已经清洁了其行或列中的普通单元格,否则无法使用机器清洁不整洁的单元格。现在,查看是否可以打扫房间。

输入如下:
第一行包含n房间的大小。接下来的n行包含对每一行的描述,其中row [i] [j]为’ * “如果它比其他人更不整洁,那就是” . ‘如果是正常细胞。

例子:

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

方法 :
最小单元数可以为n。这是唯一可能的答案,因为它需要具有类型为’的元素.在每个不同的行和列中。如果特定的列和给定的行包含’ *那么,在所有牢房中,人们都知道无法打扫房子。遍历每一行并找到“ . ‘可以用于机器。使用此步骤两次,检查每一行的每一列,然后检查每一列的每一行。然后检查两者中是否有任何一个答案为n。如果是,那么可以打扫房子,否则不打扫。这种方法将为我们提供所需的最低限度的答案。

在第一个示例中,机器将清洁单元(1、1),(2、1),(3、1),以便清洁整个房间。
在第二个示例中, 1^{st}行有“ * ‘和其中的每个单元格3^{rd}列包含“ * ‘,因此房屋无法打扫。 1^{st}行不能以任何方式清洗。

C++
// CPP code to find whether
// house can be cleaned or not
#include 
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 > 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;
}


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


输出:

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

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。