[백준 17472] 다리 만들기 2

rhkr9080·2023년 5월 7일
0

문제링크 : https://www.acmicpc.net/problem/17472

🔔 마을 정하기

void define_island(int now_row, int now_col)
{
	queue<Coord> nowQ;
	nowQ.push({ now_row, now_col });
	visited[now_row][now_col] = 1;
	TEMP_MAP[now_row][now_col] = idx;
	while (!nowQ.empty()) 
	{
		Coord now = nowQ.front();
		nowQ.pop();
		for (int i = 0; i < 4; i++)
		{
			int next_row = now.row + dr[i];
			int next_col = now.col + dc[i];
			if (next_row < 0 || next_col < 0 || next_row >= N || next_col >= M) continue;
			if (visited[next_row][next_col] == 1) continue;
			if (MAP[next_row][next_col] == 0) continue;
			visited[next_row][next_col] = 1;
			TEMP_MAP[now.row][now.col] = idx;
			nowQ.push({ next_row, next_col });
		}
	}
}

int SOLVE()
{
	int answer = 0;

	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < M; j++)
		{
			// 1. 섬의 index 정하기
			// # MAP이 1이면
			if (MAP[i][j] == 1)
			{
				if (visited[i][j] == 0)
				{
					define_island(i, j);
					idx++;
				}
			}
			// # 방문한 적이 없으면 index 증가하고 index 넣기
		}
	}
	return answer;
}

💻 문제 풀이 : C++

📌 memo 😊

profile
공부방

0개의 댓글