Notice
Recent Posts
Recent Comments
Link
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Archives
Today
Total
관리 메뉴

소소한 개발자

[백준] 1388 - 바닥 장식 본문

온라인 저지/백준

[백준] 1388 - 바닥 장식

rrrmaster 2021. 8. 5. 22:12

문제 링크

1388번: 바닥 장식 (acmicpc.net)

 

1388번: 바닥 장식

형택이는 건축가이다. 지금 막 형택이는 형택이의 남자 친구 기훈이의 집을 막 완성시켰다. 형택이는 기훈이 방의 바닥 장식을 디자인했고, 이제 몇 개의 나무 판자가 필요한지 궁금해졌다. 나

www.acmicpc.net

 

코드 

#include <iostream>

void vertical(int x, int y, int h, char map[][101], bool visited[][101])
{
	visited[y][x] = true;
	if (y > 0 && map[y - 1][x] == '|' && !visited[y - 1][x])
		vertical(x, y - 1, h, map, visited);
	if (y < h - 1 && map[y + 1][x] == '|' && !visited[y + 1][x])
		vertical(x, y + 1, h, map, visited);

	return;
}

void horizontal(int x, int y, int w, char map[][101], bool visited[][101])
{
	visited[y][x] = true;
	if (x > 0 && map[y][x - 1] == '-' && !visited[y][x - 1])
		horizontal(x - 1, y, w, map, visited);
	if (x < w - 1 && map[y][x + 1] == '-' && !visited[y][x + 1])
		horizontal(x + 1, y, w, map, visited);

	return;
}

int main()
{
	int n, m;
	int count = 0;
	char map[101][101] = {};
	bool visited[101][101] = {};

	std::cin >> n >> m;
	for (int y = 0; y < n; y++)
	{
		for (int x = 0; x < m; x++)
		{
			std::cin >> map[y][x];
		}
	}
	for (int y = 0; y < n; y++)
	{
		for (int x = 0; x < m; x++)
		{
			if (visited[y][x])
				continue;
			
			if (map[y][x] == '|')
				vertical(x, y, n, map, visited);
			else
				horizontal(x, y, m, map, visited);
			
			count += 1;
		}
	}
	std::cout << count;
}

 

풀이 해설

이 문제는 그래프 탐색 문제이다.

입력은 |, -이 있고

| 는 상하 이동

-는 좌우 이동

으로 가능하다.

 

그렇다면

|를 만나면 vertical

-를 만나면 horizontal을 호출한다.

 

그리고 맵의 정보는 map, 방문한 정보는 visited이다.

 

사실 x,y를 제외한 매개변수를 전역변수로 만들어서 사용해도 상관이 없다.

'온라인 저지 > 백준' 카테고리의 다른 글

[백준] 14726 - 신용카드 판별  (0) 2021.08.06
[백준] 14730 - 謎紛芥索紀 (Small)  (0) 2021.08.05
[백준] 13771 - Presents  (0) 2021.08.01
[백준] 2959 - 거북이  (0) 2021.08.01
[백준] 17608 - 막대기  (0) 2021.08.01
Comments