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
- 유니티
- 백트래킹
- ue5
- XR Interaction Toolkit
- c++
- 다이나믹 프로그래밍
- Unreal Engine 5
- 백준
- 트리
- VR
- 정렬
- 알고리즘
- 자료구조
- 유니온 파인드
- DFS
- 구현
- 누적 합
- 우선순위 큐
- 시뮬레이션
- 문자열
- 그래프
- Team Fortress 2
- 재귀
- 수학
- 다익스트라
- 투 포인터
- 스택
- 브루트포스
- 그리디 알고리즘
- BFS
Archives
- Today
- Total
1일1알
백준 1303번 전쟁-전투 C++ 본문
간단한 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>
using namespace std;
typedef long long ll;
int n, m;
int posR[4] = { -1,0,1,0 };
int posC[4] = { 0,1,0,-1 };
int bfs(char c, int row, int col, const vector<vector<char>> &v, vector<vector<bool>> &visited) {
queue<pair<int, int>> q;
visited[row][col] = true;
q.push({ row,col });
int cnt = 1;
while (!q.empty()) {
auto a = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int nextRow = a.first + posR[i];
int nextCol = a.second + posC[i];
if (nextRow < 0 || nextRow >= n) continue;
if (nextCol < 0 || nextCol >= m) continue;
if (visited[nextRow][nextCol]) continue;
if (v[nextRow][nextCol] != c) continue;
visited[nextRow][nextCol] = true;
q.push({ nextRow,nextCol });
cnt++;
}
}
return cnt * cnt;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> m >> n;
vector<vector<char>> v(n, vector<char>(m));
vector<vector<bool>> visited(n, vector<bool>(m, false));
string str;
for (int i = 0; i < n; i++) {
cin >> str;
for (int j = 0; j < m; j++) {
v[i][j] = str[j];
}
}
int ans1 = 0;
int ans2 = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!visited[i][j]) {
if (v[i][j] == 'W') {
ans1 += bfs('W', i, j, v, visited);
}
else {
ans2 += bfs('B', i, j, v, visited);
}
}
}
}
cout << ans1 << " " << ans2;
};
'알고리즘' 카테고리의 다른 글
백준 2138번 전구와 스위치 C++ (0) | 2021.11.28 |
---|---|
백준 18428번 감시 피하기 C++ (0) | 2021.11.27 |
백준 14716번 현수막 C++ (0) | 2021.11.25 |
백준 15989번 1, 2, 3 더하기 4 C++ (0) | 2021.11.24 |
백준 21608번 상어 초등학교 C++ (0) | 2021.11.23 |