Codeforces Round 472-2B题解报告

谁践踏了优雅 2022-05-28 01:53 281阅读 0赞

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

  1. input
  2. 5 8
  3. .#.#..#.
  4. .....#..
  5. .#.#..#.
  6. #.#....#
  7. .....#..
  8. output
  9. Yes
  10. input
  11. 5 5
  12. ..#..
  13. ..#..
  14. #####
  15. ..#..
  16. ..#..
  17. output
  18. No
  19. input
  20. 5 9
  21. ........#
  22. #........
  23. ..##.#...
  24. .......#.
  25. ....#.#.#
  26. output
  27. No

分析

举三个简单的例子。
(1)

  1. 3 3
  2. #.#
  3. ...
  4. #.#

第一步操作:取R1包含第一行和第三行,C1包含第一列和第三列,则四个#所在的格子就会都被涂成黑色。
所以经过一步操作即能达到结果,输出“Yes”

(2)

  1. 3 3
  2. ..#
  3. ...
  4. #.#

第一步操作:取R1包含第一行和第三行,C1包含第三列,则右上角和右下角的#所在的格子会被涂成黑色。
第二步操作:取R2包含第三行,C2包含第一列,可把左下角的#所在的格子涂成黑色。但是第三行上一步已经被涂过一次了,违反了题目中R1∩R2必须为空的要求,所以结果为”No”

(3)

  1. 3 3
  2. #.#
  3. .#.
  4. #.#

第一步操作:取R1包含第一行和第三行,C1包含第一列第三列,则四个角#所在的格子会被涂成黑色。
第二步操作:取R2包含第二行,C2包含第二列,可把中央的#所在的格子涂成黑色。
这样,所有的#所在的格子都被涂成了黑色,并且R1∩R2为空,C1∩C2为空,符合题意,输出”Yes”

思路

观察上面三个例子,可以发现:
如果两行不一样,那么这两行中的#格子所在的列必须也不一样,结果才为”Yes”。比如例(3),第二行和第一行不一样,第二行的#所在的列(第2列)与第一行的#所在的列(第1,3列)也不一样,结果为”Yes”
反过来,如果两行不一样,但是这两行中的#格子所在的列有一样的,相当于是有了交集,结果为”No”。比如例(2)。

代码

  1. #include <cstdio>
  2. typedef long long int64;
  3. static const int MAXN = 53;
  4. static int n, m;
  5. static bool a[MAXN][MAXN];
  6. int main()
  7. {
  8. scanf("%d%d", &n, &m);
  9. getchar();
  10. for (int i = 0; i < n; ++i)
  11. {
  12. for (int j = 0; j <= m; ++j)
  13. {
  14. a[i][j] = (getchar() == '#');
  15. }
  16. }
  17. for (int i = 0; i < n - 1; ++i)
  18. {
  19. for (int j = i + 1; j < n; ++j)
  20. {
  21. bool all_same = true, no_intersect = true;
  22. for (int k = 0; k < m; ++k)
  23. {
  24. if (a[i][k] != a[j][k])
  25. {
  26. all_same = false;
  27. }
  28. if (a[i][k] && a[j][k])
  29. {
  30. no_intersect = false;
  31. }
  32. }
  33. if (!all_same && !no_intersect)
  34. {
  35. puts("No");
  36. return 0;
  37. }
  38. }
  39. }
  40. puts("Yes");
  41. return 0;
  42. }

Codeforces & TopCoder QQ交流群:648202993
更多内容请关注微信公众号
feicuisenlin\_12x12.jpg

发表评论

表情:
评论列表 (有 0 条评论,281人围观)

还没有评论,来说两句吧...

相关阅读