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
- 백준
- Team Fortress 2
- 유니티
- VR
- 구현
- 시뮬레이션
- Unreal Engine 5
- 트리
- 다이나믹 프로그래밍
- 자료구조
- 그래프
- 그리디 알고리즘
- 누적 합
- 백트래킹
- c++
- 스택
- 브루트포스
- 문자열
- 정렬
- BFS
- 투 포인터
- XR Interaction Toolkit
- 수학
- 재귀
- 유니온 파인드
- ue5
- 알고리즘
- 우선순위 큐
- 다익스트라
- DFS
Archives
- Today
- Total
1일1알
백준 1385번 벌집 C++ 본문
빙글빙글 돌면서 증가하는 벌집을 2차원 배열로 구현하고 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 ll = long long;
int dRow[6] = { -1,-2,-1,1,2,1 };
int dCol[6] = { 1,0,-1,-1,0,1 };
int moveCnt[6] = { 1,0,1,1,1,1 };
vector<vector<int>> board(3001, vector<int>(3001, 0));
vector<vector<bool>> found(3001, vector<bool>(3001, false));
vector<vector<pair<int, int>>> parent(3001, vector<pair<int, int>>(3001));
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int a, b;
cin >> a >> b;
pair<int, int> start, end;
int row = 1500;
int col = 1500;
board[row][col] = 1;
if (a == 1) start = { row,col };
int dir = 0;
int cnt = 0;
for (int i = 2; i <= 1000000;) {
if (cnt == moveCnt[dir]) {
moveCnt[dir]++;
dir = (dir + 1) % 6;
cnt = 0;
continue;
}
row += dRow[dir];
col += dCol[dir];
board[row][col] = i;
if (a == i) start = { row,col };
cnt++;
i++;
}
queue<pair<int, int>> q;
q.push(start);
found[start.first][start.second] = true;
parent[start.first][start.second] = start;
while (!q.empty()) {
auto curr = q.front();
q.pop();
if (board[curr.first][curr.second] == b) {
end = { curr.first, curr.second };
break;
}
for (int i = 0; i < 6; i++) {
int nextRow = curr.first + dRow[i];
int nextCol = curr.second + dCol[i];
if (nextRow < 0 || nextRow > 3000) continue;
if (nextCol < 0 || nextCol > 3000) continue;
if (found[nextRow][nextCol]) continue;
found[nextRow][nextCol] = true;
parent[nextRow][nextCol] = curr;
q.push({ nextRow, nextCol });
}
}
vector<int> ans;
pair<int, int> curr = end;
pair<int, int> prt = parent[end.first][end.second];
while (curr != prt) {
ans.push_back(board[curr.first][curr.second]);
curr = prt;
prt = parent[curr.first][curr.second];
}
ans.push_back(board[start.first][start.second]);
for (int i = ans.size() - 1; i >= 0; i--) {
cout << ans[i] << " ";
}
};
'알고리즘' 카테고리의 다른 글
백준 1417번 국회의원 선거 C++ (0) | 2022.04.28 |
---|---|
백준 1421번 나무꾼 이다솜 C++ (0) | 2022.04.27 |
백준 1380번 귀걸이 C++ (0) | 2022.04.25 |
백준 1347번 미로 만들기 C++ (0) | 2022.04.24 |
백준 1337번 올바른 배열 C++ (0) | 2022.04.23 |