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
- 재귀
- 그리디 알고리즘
- VR
- c++
- 문자열
- 유니티
- 투 포인터
- 자료구조
- Unreal Engine 5
- BFS
- 다이나믹 프로그래밍
- ue5
- 브루트포스
- 알고리즘
- Team Fortress 2
- 그래프
- DFS
- 다익스트라
- 시뮬레이션
- 수학
- 백준
- XR Interaction Toolkit
- 유니온 파인드
- 누적 합
- 구현
- 백트래킹
- 스택
- 우선순위 큐
- 정렬
- 트리
Archives
- Today
- Total
1일1알
백준 14271번 그리드 게임 C++ 본문
https://www.acmicpc.net/problem/14271
14271번: 그리드 게임
첫째 줄에 처음 그리드의 행의 개수 N과 열의 개수 M이 주어진다. (1 ≤ N, M ≤ 50) 둘째 줄부터 N개의 줄에는 처음 그리드의 상태가 주어진다. 살아있는 칸은 'o'로, 죽어있는 칸은 '.'으로 주어진다
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 <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
int dRow[4] = { -1,0,1,0 };
int dCol[4] = { 0,1,0,-1 };
const int ROW_OFFSET = 1500;
const int COL_OFFSET = 1500;
vector<vector<bool>> found(3100, vector<bool>(3100, false));
struct Info {
int row;
int col;
int moveCnt;
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m, k;
cin >> n >> m;
int64 cnt = 0;
queue<Info> q;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
char ch;
cin >> ch;
if (ch == 'o') {
found[i + ROW_OFFSET][j + COL_OFFSET] = true;
q.push({ i + ROW_OFFSET,j + COL_OFFSET,0 });
}
}
}
cin >> k;
while (!q.empty()) {
auto curr = q.front();
q.pop();
cnt++;
if (curr.moveCnt >= k) continue;
for (int i = 0; i < 4; i++) {
int nextRow = curr.row + dRow[i];
int nextCol = curr.col + dCol[i];
if (found[nextRow][nextCol]) continue;
found[nextRow][nextCol] = true;
q.push({ nextRow,nextCol,curr.moveCnt + 1 });
}
}
cout << cnt;
}
'알고리즘' 카테고리의 다른 글
백준 17129번 윌리암슨수액빨이딱따구리가 정보섬에 올라온 이유 C++ (0) | 2022.10.23 |
---|---|
백준 12869번 뮤탈리스크 C++ (0) | 2022.10.22 |
백준 17352번 여러분의 다리가 되어 드리겠습니다! C++ (0) | 2022.10.12 |
백준 10974번 모든 순열 C++ (0) | 2022.10.11 |
백준 2056번 작업 C++ (0) | 2022.10.10 |