[ABC422B]Looped Rope题解

Time Limit: 2 sec / Memory Limit: 1024 MiB

Score : 200 points

Problem Statement

There is a grid with H rows and W columns. Let (i,j) denote the cell at the i-th row (1≤i≤H) from the top and the j-th column (1≤j≤W) from the left.

Each cell is painted with one color, white or black. The color painted on the cells is represented by H strings S1​,S2​,…,SH​. When the j-th character (1≤j≤W) of Si​ (1≤i≤H) is ., cell (i,j) is painted white, and when the j-th character (1≤j≤W) of Si​ (1≤i≤H) is #, cell (i,j) is painted black.

Determine whether the grid satisfies the following condition:

  • For every black cell, the number of horizontally or vertically adjacent cells that are painted black is 2 or 4.

Here, cells (i,j) (1≤i≤H,1≤j≤W) and (k,l) (1≤k≤H,1≤l≤W) are horizontally or vertically adjacent if and only if ∣i−k∣+∣j−l∣=1.

讯飞听见 翻译

###问题陈述

有一个包含 H 行和 W 列的网格。

设 (i,j) 表示从顶部起第 i 行 (1≤i≤H) 和从左侧起第 j 列 (1≤j≤W) 的单元格。每个单元格都涂有一种颜色,白色或黑色。绘制在单元格上的颜色由 H 字符串 S1​,S2​,…,SH​ 表示。

当 Si​ (1≤i≤H) 的第 j -个字符 (1≤j≤W) 是`时。',单元格 (i,j) 涂成白色,当 Si​ (1≤i≤H) 的第 j 个字符 (1≤j≤W) 为'#'时,单元格 (i,j) 涂成黑色。确定网格是否满足以下条件:

-对于每一个黑色细胞,

涂成黑色的水平或垂直相邻单元格的数量为 2 或 4 。这里,当且仅当 ∣i−k∣+∣j−l∣=1 时,单元格 (i,j) (1≤i≤H,1≤j≤W) 和 (k,l) (1≤k≤H,1≤l≤W) 水平或垂直相邻。

Constraints

  • 1≤H≤20
  • 1≤W≤20
  • H and W are integers.
  • Si​ is a string of length W consisting of . and # (1≤i≤H).

讯飞听见 翻译

###约束

  • 1≤H≤20

  • 1≤W≤20

  • H 和 W 是整数。

  • Si​ 是长度为 W 的字符串,由组成。# (1≤i≤H) 。


Input

The input is given from Standard Input in the following format:

H W
S1​
S2​
⋮
SH​

讯飞听见 翻译

###输入

输入来自标准输入,格式如下:

H W
S1​
S2​
⋮
SH​

Output

Output Yes if the given grid satisfies the condition, and No if it does not satisfy the condition.

讯飞听见 翻译

###输出

如果给定网格满足条件,则输出“是”,如果不满足条件,则输出“否”。


Sample Input 1

Copy

8 7
.######
##....#
#.###.#
#.#.#.#
#.#.#.#
#.#####
#...#..
#####..

Sample Output 1

Copy

Yes

For example, cell (6,3) is painted black, and among the adjacent cells (5,3),(6,2),(6,4),(7,3), cells (5,3) and (6,4) are painted black, which is 2 cells, so it satisfies the condition.

Every other black cell also satisfies the condition, so output Yes.


Sample Input 2

Copy

1 2
##

Sample Output 2

Copy

No

Cell (1,1) is painted black, but it has only one adjacent cell, so it does not satisfy the condition.

Therefore, output No.


Sample Input 3

Copy

4 3
...
...
...
...

Sample Output 3

Copy

Yes

There are no black cells, so the condition is satisfied.

Therefore, output Yes.


Sample Input 4

Copy

15 18
##.###..##.##..##.
##.#.##.##.##.####
...##.#.......####
###.###....###.##.
#.##.......#.#....
#..#.##.##.#.#....
#.########.####.##
#.##.##.#....##.##
#......##.........
##.##..#..##..####
.#.#####..#####..#
.#..#...##.#.....#
.#..#.####.#.....#
.##.#.#.#..##..###
..###.###...####..

Sample Output 4

Copy

Yes

思路

暴力

代码见下

#include<bits/stdc++.h>
using namespace std;
long long h,w,lk=0;
char s[1005][1005];
int main(){
	cin>>h>>w;
	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++){
			cin>>s[i][j];
		}
	}
	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++){
			if(s[i][j]=='#'){
				long long hj=0;
				if(s[i+1][j]=='#'){
					hj++;
				}
				if(s[i][j+1]=='#'){
					hj++;
				}
				if(s[i-1][j]=='#'){
					hj++;
				}
				if(s[i][j-1]=='#'){
					hj++;
				}
				if(hj==1||hj==3||hj==0){
					lk=1;
				}
			}
		}
	}
	if(lk==1){
		cout<<"No"<<endl;
	}
	else{
		cout<<"Yes"<<endl;
	}
	return 0;
}

posted @ 2025-10-04 19:23  bz02_2023f2  阅读(2)  评论(0)    收藏  举报  来源