Mystical Mosaic CodeForces - 957B(暴力,思维)

Mystical Mosaic

CodeForces - 957B

There is a rectangular grid of n rows of m initially-white cells each.

Arkady performed a certain number (possibly zero) of operations on it. In the i-th operation, a non-empty subset of rows Ri and a non-empty subset of columns Ci are chosen. For each row r in Ri and each column c in Ci, the intersection of row r and column c is coloured black.

There's another constraint: a row or a column can only be chosen at most once among all operations. In other words, it means that no pair of (i, j) (i < j) exists such that or , where denotes intersection of sets, and denotes the empty set.

You are to determine whether a valid sequence of operations exists that produces a given final grid.


Input

The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 50) — the number of rows and columns of the grid, respectively.

Each of the following n lines contains a string of m characters, each being either '.' (denoting a white cell) or '#' (denoting a black cell), representing the desired setup.

Output

If the given grid can be achieved by any valid sequence of operations, output "Yes"; otherwise output "No" (both without quotes).

You can print each character in any case (upper or lower).

Examples
Input
5 8
.#.#..#.
.....#..
.#.#..#.
#.#....#
.....#..
Output
Yes
Input
5 5
..#..
..#..
#####
..#..
..#..
Output
No
Input
5 9
........#
#........
..##.#...
.......#.
....#.#.#
Output
No
Note

For the first example, the desired setup can be produced by 3 operations, as is shown below.

For the second example, the desired setup cannot be produced, since in order to colour the center row, the third row and all columns must be selected in one operation, but after that no column can be selected again, hence it won't be possible to colour the other cells in the center column.

题意:在一个n x m的方格中,初始状态全是‘.’,有几个操作,每个操作,选择一个行集合R和一个列集合C,把行集合中的每行ri和列集合中的每列ci的交点变成#,而且每次操作所选的R,C两个集合和之前的集合不能有交集,也就是说,每个行和列只能选择一次,给定一个目标状态,问是否可以通过若干次操作变成这样,输出Yes或No

思路:题目最关键的一点就是每次操作选择的R,C集合中的行列只能选择一次,也就是每个行列只能操作一次,所以我们枚举整个图,当枚举到#的时候假设所在行ri,这个时候我们检查,当前列固定,继续往下看每一行,如果还有#在rj行,如果是符合题意的,那么这个#一定是在同一次操作中完成的就像这样:

             ci                  cj

 ri           #                 #          

                                     

 rj           #                 #            

之后这些行列就不可能在进行操作了,所以只要发现同一列中,有若干行有#,那么这几行的字符必须是完全相同的才说明是按照规则得到的,否则就不能得到。

code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
string str[150];
int n,m;
bool check(int i,int j){
    for(int x = 0; x < n; x++){
        if(str[x][j] == '#'){
            if(str[x] != str[i])
                return false;
        }
    }
    return true;
}
int main(){
    cin >> n >> m;
    for(int i = 0; i < n; i++){
        cin >> str[i];
    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            if(str[i][j] == '#'){
                if(!check(i,j)){
                    cout << "No" << endl;
                    return 0;
                }
            }
        }
    }
    cout << "Yes" << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/codeswarrior/article/details/80389136