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

本文详细解析 CodeForces 平台上的 957B 题目,通过分析题目要求及给出的代码示例,阐述了解决方案的思路与实现过程。重点介绍了如何判断给定矩阵是否能通过特定的操作序列生成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值