Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ABC_191_C - Digital Graffiti #45

@zeikar

Description

@zeikar

Problem link

https://atcoder.jp/contests/abc191/tasks/abc191_c

Problem Summary

그리드에 #, .으로 이루어진 문자열이 주어질 때 이 도형이 몇각형인지 구하는 문제.

Solution

문제 설명, 예제가 불친절한데... 오목 다각형도 존재할 수 있다.

어쨌든 다각형이 몇각형인지 구하려면 꼭지점의 개수를 구해야 한다.
이건 에디토리얼을 참고했는데 상하좌우 2칸씩 개수를 세서 검은색 블록 (#)이 1개 또는 3개이면 꼭지점이다.

예시를 보면 좀 이해가 가긴 하는데

.....
.###.
.###.
.###.
.....

이건 4각형이고

.....
.#.#.
.###.
.###.
.....

이건 8각형이 된다

Source Code

#include <iostream>
#include <string>
using namespace std;

int main() {
	int h, w;
	cin >> h >> w;

	string grid[10];

	for (int i = 0; i < h; i++)
	{
		cin >> grid[i];
	}

	int ans = 0;

	for (int i = 0; i < h - 1; i++)
	{
		for (int j = 0; j < w - 1; j++)
		{
			int cnt = 0;

			for (int a = 0; a < 2; a++)
			{
				for (int b = 0; b < 2; b++)
				{
					cnt += (grid[i + a][j + b] == '#');
				}
			}

			if (cnt == 1 || cnt == 3)
			{
				ans++;
			}
		}
	}

	cout << ans << endl;
}

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions