Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 오에스
- OS
- Operating System
- DP
- 정석
- 구현
- 문제풀이
- 컴공과
- c++
- bfs
- 정석학술정보관
- 오퍼레이팅시스템
- 백준
- 그래프
- Computer science
- 알고리즘
- 컴공
- cs
- Stack
- 자료구조
- vector
- 북리뷰
- coding
- 스택
- 컴퓨터공학과
- 브루트포스
- 개발
- 너비우선탐색
- 코테
- 코딩
Archives
- Today
- Total
Little Jay
[C++] 백준 3187번 - 양치기 꿍 본문
간단했던 BFS 문제였는데 멍청하게 BFS 수행 조건을 잘못 줘서 30분동안 맞왜틀 하고 있던 문제였다.
특별한 로직은 따로 없었고 양의 수 > 늑대 수 일때만 양의 수를 업데이트 시키는 것만 잘 파악했다면 크게 거렵지는 않았다.
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const int dx[] = { 1, -1, 0, 0 };
const int dy[] = { 0, 0, 1, -1 };
char board[255][255];
bool visited[255][255];
int r, c;
int wolves = 0, sheep = 0;
void bfs(int x, int y) {
queue<pair<int, int>> q;
q.push({ x, y });
visited[x][y] = true;
int wCnt = 0;
int sCnt = 0;
if (board[x][y] == 'v') wCnt++;
if (board[x][y] == 'k') sCnt++;
while (!q.empty()) {
auto current = q.front(); q.pop();
for (int dir = 0; dir < 4; dir++) {
int ny = current.second + dy[dir];
int nx = current.first + dx[dir];
if (ny < 0 || nx < 0 || ny >= c || nx >= r) continue;
if (visited[nx][ny] == false && board[nx][ny] != '#') {
if (board[nx][ny] == 'v') wCnt++;
if (board[nx][ny] == 'k') sCnt++;
q.push({ nx, ny });
visited[nx][ny] = true;
}
}
}
if (sCnt > wCnt)
sheep += sCnt;
else
wolves += wCnt;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> r >> c;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cin >> board[i][j];
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (board[i][j] != '#' && !visited[i][j])
bfs(i, j);
}
}
cout << sheep << " " << wolves << endl;
return 0;
}
'알고리즘 > BOJ' 카테고리의 다른 글
[C++] 백준 1717번 - 집합의 표현 (0) | 2022.07.11 |
---|---|
[C++] 백준 24524번 - 아름다운 문자열 (0) | 2022.07.10 |
[C++] 백준 2608번 - 로마 숫자 (0) | 2022.07.04 |
[C++] 백준 1541번 - 잃어버린 괄호 (0) | 2022.07.04 |
[C++] 백준 9375번 - 패션왕 신해빈 (0) | 2022.07.02 |
Comments