您的位置:首页 > 大数据 > 人工智能

VK Cup 2018 Round 2: A. Mystical Mosaic(思维)

2018-03-25 16:28 387 查看
B. Mystical Mosaictime limit per test 1 secondmemory limit per test 256 megabytesinput standard inputoutput standard outputThere 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.InputThe 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.OutputIf 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).Examplesinput
5 8
.#.#..#.
.....#..
.#.#..#.
#.#....#
.....#..
output
Yes
input
5 5
..#..
..#..
#####
..#..
..#..
output
No
input
5 9
........#
#........
..##.#...
.......#.
....#.#.#
output
No


题意:一个n*m的矩阵,一开始所有格子都是空的,你每次可以选择若干列和若干行,并在这些行和列的交点上放上一枚棋子,每一行/列至多被选一次,先给你一种最终情况,问你能否实现

仔细分析一下就会发现,如果有三个棋子在某个矩形的三个顶点上,那么这个矩形的第四个顶点上一定要有棋子(三个棋子作为定点可以构成一个直角三角形,其中直角边平行于x轴和y轴,那么这三个棋子就一定在某个矩形的三个顶点上)
所以只要暴力枚举任意一对棋子作为矩形的其中一条斜对角线,然后判断下另一条对角线上棋子情况就OK了
复杂度O(n²m²)

#include<stdio.h>
char str[55][55];
int main(void)
{
int n, m, i, j, x, y;
scanf("%d%d", &n, &m);
for(i=1;i<=n;i++)
scanf("%s", str[i]+1);
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(str[i][j]=='.')
continue;
for(x=1;x<=n;x++)
{
for(y=1;y<=n;y++)
{
if(str[x][y]=='.' || i==x || j==y)
continue;
if(str[i][y]=='#' && str[x][j]=='.' || str[i][y]=='.' && str[x][j]=='#')
{
printf("No\n");
return 0;
}
}
}
}
}
printf("Yes\n");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: