일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 분할 정복
- 다익스트라
- 백트래킹
- pytorch
- Overfitting
- tensorflow
- 2023
- back propagation
- c++
- 플로이드 와샬
- object detection
- 가끔은_말로
- 너비 우선 탐색
- dfs
- 알고리즘
- 자바스크립트
- NEXT
- 미래는_현재와_과거로
- 회고록
- DP
- BFS
- dropout
- 우선 순위 큐
- 세그먼트 트리
- 크루스칼
- 조합론
- 이분 탐색
- lazy propagation
- 가끔은 말로
- 문자열
Archives
- Today
- Total
Doby's Lab
백준 14397번: 해변 (C++) 본문
https://www.acmicpc.net/problem/14397
Level: Silver IV
Solved By: Graph
육각형이기 때문에 행에 따라 인접할 수 있는 블록의 위치를 구현할 수 있다면 그래프 탐색으로 쉽게 풀 수 있습니다.
#include <iostream>
#include <vector>
#include <queue>
#define pii pair<int, int>
#define MAX 51
using namespace std;
int N, M;
char m[MAX][MAX];
bool visited[MAX][MAX];
struct dir{
int y, x;
};
dir dir_odd[6] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {1, -1}};
dir dir_even[6] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, 1}, {1, 1}};
int bfs(int row, int col){
int ret = 0;
queue<pii> q;
q.push({row, col});
visited[row][col] = true;
while(!q.empty()){
int ny = q.front().first;
int nx = q.front().second;
q.pop();
for(int i = 0; i < 6; i++){
int dy, dx;
if(ny % 2){
dy = ny + dir_odd[i].y;
dx = nx + dir_odd[i].x;
}
else{
dy = ny + dir_even[i].y;
dx = nx + dir_even[i].x;
}
if(dy > N || dy < 1 || dx > M || dx < 1) continue;
if(visited[dy][dx]) continue;
if(m[dy][dx] == '#') ret++;
else{
q.push({dy, dx});
visited[dy][dx] = true;
}
}
}
return ret;
}
int main(){
cin >> N >> M;
for(int i = 1; i <= N; i++){
for(int j = 1; j <= M; j++){
cin >> m[i][j];
}
}
int res = 0;
for(int i = 1; i <= N; i++){
for(int j = 1; j <= M; j++){
if(visited[i][j]) continue;
else if(m[i][j] == '#') continue;
res += bfs(i, j);
}
}
cout << res;
return 0;
}
728x90
'PS > BOJ' 카테고리의 다른 글
백준 1755번: 숫자놀이 (C++) (0) | 2023.03.01 |
---|---|
백준 1544번: 사이클 단어 (C++) (0) | 2023.03.01 |
백준 2477번: 참외밭 (C++) (0) | 2023.01.08 |
백준 1004번: 어린 왕자 (C++) (0) | 2023.01.08 |
백준 1769번: 3의 배수 (C++) (0) | 2023.01.08 |