Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- unorder_map
- 정렬
- 강참조
- BFS
- 애셋로드
- 알고리즘
- 언리얼가비지컬렉터
- 자료구조
- 약참조
- UE_LOG
- 선택정렬
- dataasset
- 람다
- 람다사용정렬
- 스마트포인터
- 크리티컬섹션
- 구조적 바인딩
- stl
- 데이터애셋
- UML관련
- C++
- 델리게이트
- map
- enumasByue
- UE4 커스텀로그
- 프로그래머스
- moreeffectiveC++
- 정렬알고리즘
- C++최적화
- UELOG
Archives
- Today
- Total
기억을 위한 기록들
[백준 2573: 빙산] - C++ 본문
2573번: 빙산
첫 줄에는 이차원 배열의 행의 개수와 열의 개수를 나타내는 두 정수 N과 M이 한 개의 빈칸을 사이에 두고 주어진다. N과 M은 3 이상 300 이하이다. 그 다음 N개의 줄에는 각 줄마다 배열의 각 행을
www.acmicpc.net
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
int n, m;
int map[300][300];
int check[300][300];
int checkTemp[300][300];
int result = 0;
int year = 0;
int dir[4][2] = {
{-1,0}
,{0,1}
,{1,0}
,{0,-1}
};
//얼음 몇 덩어리인지 세기용
void CheckIce(int _x, int _y)
{
for (int i = 0; i < 4; i++)
{
int nextX = _x + dir[i][0];
int nextY = _y + dir[i][1];
if (nextX < 0 || nextY < 0 || n <= nextX || m <= nextY)
continue;
if (map[nextX][nextY] != 0 && !checkTemp[nextX][nextY])
{
checkTemp[nextX][nextY] = true;
CheckIce(nextX, nextY);
}
}
}
//얼음 몇 덩어리인지 세기용
int Search(int _x, int _y)
{
memset(checkTemp, false, sizeof(checkTemp));
int cnt = 0;
int result2 = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (map[i][j] != 0 && !checkTemp[i][j])
{
result2++;
checkTemp[i][j] = true;
CheckIce(i, j);
}
}
}
return result2;
}
//얼음 녹게함
void MeltIce(int _x, int _y)
{
int cnt = 0;
for (int i = 0; i < 4; i++)
{
int nextX = _x + dir[i][0];
int nextY = _y + dir[i][1];
if (nextX < 0 || nextY < 0 || n <= nextX || m <= nextY)
continue;
if (map[nextX][nextY] != 0 && !check[nextX][nextY])
{
check[nextX][nextY] = true;
MeltIce(nextX, nextY);
}
if (map[nextX][nextY] == 0 && !check[nextX][nextY])
{
cnt++;
}
}
map[_x][_y] -= cnt;
if (map[_x][_y] <= 0)
{
map[_x][_y] = 0;
}
}
int main() {
cin >> n >> m;
memset(map, 0, sizeof(map));
//입력
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> map[i][j];
}
}
//계산
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (map[i][j] != 0 && !check[i][j])
{
check[i][j] = true;
MeltIce(i, j); //얼음 녹이기
year++;
result = Search(i, j); //몇덩어리 인지 세기
if (1 < result) //2덩어리 이상이면 종료
{
cout << year << endl;
return 0;
}
memset(check, false, sizeof(check));
}
}
}
cout << 0 << endl;
return 0;
}
'Coding Test - cpp > DFS' 카테고리의 다른 글
[프로그래머스 lv2] 단어 변환- c++ (0) | 2021.02.16 |
---|---|
[백준 1937: 욕심쟁이 판다] - C++ (0) | 2021.02.15 |
[백준 10026: 적록색약] - C++ (0) | 2021.02.15 |
[백준 1987: 알파벳] - C++ (0) | 2021.02.15 |
[백준 2617: 구슬 찾기] - C++ (0) | 2021.02.11 |