| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- C++
- booksr.co.kr
- 김진홍 옮김
- 잡생각 정리글
- (주)책만
- 생능출판
- https://insightbook.co.kr/
- BOJ
- 게임 수학
- 백준
- 일기
- C#
- hanbit.co.kr
- 밑바닥부터 만드는 컴퓨팅 시스템 2판
- 알고리즘
- 박기현
- The Elements of Computing Systems 2/E
- 데이터 통신과 컴퓨터 네트워크
- 이득우
- unity6
- 메타버스
- Noam Nisan
- 주우석
- cyphenengine
- 전공자를 위한 C언어 프로그래밍
- 이득우의 게임수학
- 입출력과 사칙연산
- JavaScript
- C
- HANBIT Academy
Archives
- Today
- Total
cyphen156
백준-그래프와 순회 1012 유기농 배추 본문
이전 문제와 비슷하다.
행렬을 사용하여 사방 탐색을 통해 형성된 단지 수를 세면 된다.
제약사항
- TEST T
- 1 <= N < 51 (Column)
- 1 <= M < 51 (Row)
- 1 <= K < 2501
주의 사항
없다.
CPP풀이
유기농 배추_1012.cpp
/**
* 백준 유기농 배추_1012
* 이전 문제와 비슷하다.
* 행렬을 사용하여 사방 탐색을 통해 형성된 단지 수를 세면 된다.
*
* 제한사항
*****************************************
* TEST T *
* 1 <= N < 51 (Column) *
* 1 <= M < 51 (Row) *
* 1 <= K < 2501 *
*****************************************
*
*
*
* 주의
* 없다.
*
* 풀이시간 (문제 해석 + 구현)
* 1 + 20분
*/
#include <iostream>
#include <stack>
const static int MAX_SIZE = 50;
const int dx[4] = { 1, -1, 0, 0 };
const int dy[4] = { 0, 0, 1, -1};
using namespace std;
static int T, N, M, K;
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> T;
for (int i = 0; i < T; ++i)
{
cin >> N >> M >> K;
// 행렬 초기화
int matrix[MAX_SIZE][MAX_SIZE] = { 0 };
bool isVisited[MAX_SIZE][MAX_SIZE] = { 0 };
int cnt = 0;
for (int j = 0; j < K; ++j)
{
int row, column;
cin >> row >> column;
matrix[column][row] = 1;
}
for (int row = 0; row < M; ++row)
{
for (int column = 0; column < N; ++column)
{
if (matrix[row][column] == 1 && isVisited[row][column] == false)
{
cnt++;
stack<pair<int, int>> stack;
stack.push({row, column});
isVisited[row][column] = true;
while (stack.empty() != true)
{
pair<int, int> current = stack.top();
stack.pop();
for (int k = 0; k < 4; ++k)
{
int ny = current.first + dy[k];
int nx = current.second + dx[k];
if (ny < 0 || ny >= M || nx < 0 || nx >= N)
{
continue;
}
if (matrix[ny][nx] == 0)
{
continue;
}
if (isVisited[ny][nx])
{
continue;
}
isVisited[ny][nx] = true;
stack.push({ny, nx});
}
}
}
}
}
cout << cnt << '\n';
}
return 0;
}
모든 예제 코드의 소스파일은 제 개인 깃허브 레포지토리에 있습니다.
Workspace/알고리듬 풀이 at main · cyphen156/Workspace
Studying . Contribute to cyphen156/Workspace development by creating an account on GitHub.
github.com
'컴퓨터공학 > 알고리듬 풀이' 카테고리의 다른 글
| 백준-그래프와 순회 1697 숨바꼭질 (0) | 2025.09.02 |
|---|---|
| 백준-그래프와 순회 2178 미로 탐색 (1) | 2025.09.01 |
| 백준-그래프와 순회 2667 단지번호붙이기 (0) | 2025.09.01 |
| 백준-그래프와 순회 1260 DFS와 BFS (1) | 2025.09.01 |
| 백준-그래프와 순회 2606 바이러스 (2) | 2025.08.28 |