fragment-header
fragment-markdown
홈
로그인
로그아웃
내 블로그
설정
로그인
AtCoder Beginner Contest 422-B (C++) Looped Rope
최초 업로드: 2025-09-07 07:02:52
최근 수정 시간: 2025-09-07 07:03:09
게시자: rlatjwls3333
카테고리: Atcoder
조회수: 7
# B - Looped Rope [문제 링크](https://atcoder.jp/contests/abc422/tasks/abc422_b) ## Problem Statement There is a grid with \$H\$ rows and \$W\$ columns. Let \$(i, j)\$ denote the cell at the \$i\$-th row \$(1 \le i \le H)\$ from the top and the \$j\$-th column \$(1 \le j \le 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 \$S\_1, S\_2, \ldots, S\_H\$. When the \$j\$-th character \$(1 \le j \le W)\$ of \$S\_i\$ \$(1 \le i \le H)\$ is `.` , cell \$(i, j)\$ is painted white; when the \$j\$-th character \$(1 \le j \le W)\$ of \$S\_i\$ \$(1 \le i \le 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 \le i \le H, 1 \le j \le W)\$ and \$(k, l)\$ \$(1 \le k \le H, 1 \le l \le W)\$ are horizontally or vertically adjacent if and only if \$|i-k| + |j-l| = 1\$. ## Constraints * \$1 \le H \le 20\$ * \$1 \le W \le 20\$ * \$H\$ and \$W\$ are integers. * \$S\_i\$ is a string of length \$W\$ consisting of `.` and `#` \$(1 \le i \le H)\$. ## Input The input is given from Standard Input in the following format: $H$ $W$ $S_1$ $S_2$ $\vdots$ $S_H$ ## Output Output `Yes` if the given grid satisfies the condition, and `No` if it does not satisfy the condition. ## 풀이 bfs처럼 상하좌우에 있는 개수를 세 주었다. ``` c++ #include<bits/stdc++.h> using namespace std; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; string s[20]; int main() { ios::sync_with_stdio(0); cin.tie(0); int h, w; cin >> h >> w; for(int i=0;i<h;i++) cin >> s[i]; for(int i=0;i<h;i++) { for(int j=0;j<w;j++) { if(s[i][j]=='#') { int cnt=0; for(int k=0;k<4;k++) { int nx = i+dx[k]; int ny = j+dy[k]; if(nx<0 || nx>=h || ny<0 || ny>=w || s[nx][ny]=='.') continue; cnt++; } if(cnt!=2 && cnt!=4) { cout << "No"; return 0; } } } } cout << "Yes"; } ```