티스토리 뷰
https://www.acmicpc.net/problem/2468
비가 안 올 때, 즉 check 함수를 호출할 때 i가 0일 때도 고려했어야 했는데 안 해서 틀렸다.
#include <iostream>
#include <cstring>
using namespace std;
int N;
int Map[101][101];
int Check[101][101];
bool isVisited[101][101];
int dy[4] = {0, 0, 1, -1};
int dx[4] = {1, -1, 0, 0};
int MAX = 0, ans = 0, cnt = 0;
bool isPossible(int row, int col)
{
if(row < 0 || row >= N || col < 0 || col >= N)
return false;
return true;
}
void dfs(int row, int col)
{
isVisited[row][col] = true;
for(int i=0; i<4; i++)
{
int ny = row + dy[i];
int nx = col + dx[i];
if(false == isPossible(ny,nx) || 0 == Check[ny][nx] || true == isVisited[ny][nx])
continue;
dfs(ny, nx);
}
}
void check(int height)
{
//초기화
memset(Check, 0, sizeof(Check));
memset(isVisited, false, sizeof(isVisited));
cnt = 0;
for(int i=0; i<N; i++)
{
for(int j=0; j<N; j++)
{
//비에 잠긴다면 -> 0
if(Map[i][j] <= height)
Check[i][j] = 0;
//아니면 -> 건물의 높이
else
Check[i][j] = Map[i][j];
}
}
for(int i=0; i<N; i++)
{
for(int j=0; j<N; j++)
{
//잠기지 않았고 방문하지 않았으면
if(Check[i][j] != 0 && false == isVisited[i][j])
{
cnt++; //안전지대 +1
dfs(i, j); //dfs 실행
//dfs가 다 끝나고 cnt가 기존 ans보다 많으면 ans update
if(cnt > ans)
ans = cnt;
}
}
}
}
int main()
{
cin >> N;
for(int i=0; i<N; i++)
{
for(int j=0; j<N; j++)
{
int tmp;
cin >> tmp;
Map[i][j] = tmp;
if(MAX < tmp)
MAX = tmp;
}
}
//비 내림
for(int i=0; i<=MAX; i++)
{
check(i);
}
cout << ans << '\n';
return 0;
}
'PS > BOJ C++' 카테고리의 다른 글
1987번 - 알파벳 (0) | 2022.01.30 |
---|---|
4963번 - 섬의 개수 (0) | 2022.01.27 |
2636번 - 치즈 (0) | 2022.01.25 |
1013번 - Contact (0) | 2022.01.22 |
2667번 - 단지번호붙이기 (0) | 2022.01.20 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 2805
- 1358
- 스택
- dp
- 러스트
- 덱
- 1764
- 빌림
- 1715
- 백준
- 1759
- heapq
- 백트래킹
- 프로그래머스
- 조합
- 10815
- 파이썬
- 11051
- 큐
- 자료구조
- 1182
- 브루트포스
- 싸피
- 수학
- 10816
- 10845
- 딕셔너리
- 삼성청년소프트웨어아카데미
- 10971
- 17478
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함