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 |
Tags
- XR Interaction Toolkit
- c++
- 자료구조
- 우선순위 큐
- 투 포인터
- 유니온 파인드
- 백트래킹
- 다이나믹 프로그래밍
- 시뮬레이션
- 수학
- ue5
- 브루트포스
- 알고리즘
- 백준
- 문자열
- 다익스트라
- VR
- 누적 합
- 그리디 알고리즘
- 구현
- DFS
- 정렬
- 그래프
- 재귀
- 트리
- Team Fortress 2
- Unreal Engine 5
- 유니티
- 스택
- BFS
Archives
- Today
- Total
1일1알
백준 3184번 양 C++ 본문
https://www.acmicpc.net/problem/3184
3184번: 양
첫 줄에는 두 정수 R과 C가 주어지며(3 ≤ R, C ≤ 250), 각 수는 마당의 행과 열의 수를 의미한다. 다음 R개의 줄은 C개의 글자를 가진다. 이들은 마당의 구조(울타리, 양, 늑대의 위치)를 의미한다.
www.acmicpc.net
bfs
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
using namespace std;
using int64 = long long;
int dRow[4] = { -1,0,1,0 };
int dCol[4] = { 0,1,0,-1 };
int r, c;
vector<vector<char>> board;
vector<vector<bool>> found;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> r >> c;
board = vector<vector<char>>(r, vector<char>(c));
found = vector<vector<bool>>(r, vector<bool>(c, false));
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cin >> board[i][j];
}
}
int totalO = 0;
int totalV = 0;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (board[i][j] == '#') continue;
if (found[i][j]) continue;
queue<pair<int, int>> q;
q.push({ i,j });
found[i][j] = true;
int o = 0;
int v = 0;
while (!q.empty()) {
auto curr = q.front();
q.pop();
if (board[curr.first][curr.second] == 'o') o++;
else if (board[curr.first][curr.second] == 'v') v++;
for (int k = 0; k < 4; k++) {
int nextRow = curr.first + dRow[k];
int nextCol = curr.second + dCol[k];
if (nextRow >= r || nextRow < 0) continue;
if (nextCol >= c || nextCol < 0) continue;
if (board[nextRow][nextCol] == '#') continue;
if (found[nextRow][nextCol]) continue;
found[nextRow][nextCol] = true;
q.push({ nextRow,nextCol });
}
}
if (o > v) totalO += o;
else totalV += v;
}
}
cout << totalO << " " << totalV;
};
'알고리즘' 카테고리의 다른 글
백준 27172번 수 나누기 게임 C++ (골드5) (0) | 2024.05.26 |
---|---|
백준 20529번 가장 가까운 세 사람의 심리적 거리 C++ (실버1) (0) | 2024.05.26 |
백준 3187번 양치기 꿍 C++ (0) | 2023.06.29 |
백준 16507번 어두운 건 무서워 C++ (0) | 2023.06.22 |
백준 1431번 시리얼 번호 C++ (0) | 2023.06.21 |