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
- 백준
- DFS
- 투 포인터
- c++
- 그래프
- 수학
- 문자열
- 다이나믹 프로그래밍
- 유니티
- 다익스트라
- 구현
- ue5
- Unreal Engine 5
- 알고리즘
- 시뮬레이션
- VR
- 브루트포스
- 자료구조
- 재귀
- BFS
- Team Fortress 2
- XR Interaction Toolkit
- 백트래킹
- 정렬
- 스택
- 우선순위 큐
- 그리디 알고리즘
- 유니온 파인드
- 트리
- 누적 합
Archives
- Today
- Total
1일1알
백준 17259번 선물이 넘쳐흘러 C++ 본문


직원과 선물을 구조체로 관리하면서 1초마다 일어나는 상황을 전부 시뮬레이션 하면서 문제를 해결하였다.
다른 사람들 코드는 내 코드보다 엄청 짧던데 어떻게 저렇게 짧게 짜는거지...
#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;
using ll = long long;
int b, n, m;
int ans = 0;
int findRow[3] = { 1,0,-1 };
int findCol[3] = { 0,1,0 };
int dRow[3] = { 0,1,0 };
int dCol[3] = { 1,0,-1 };
struct present {
present(int row, int col) :
row(row), col(col), dir(0) {}
int row;
int col;
int dir;
};
struct staff {
staff(int row, int col, int time) :
row(row), col(col), time(time), workingTime(0), isWorking(false) {}
int row;
int col;
int time;
int workingTime;
bool isWorking;
};
void Init(vector<vector<int>>& board, vector<staff>& staffs) {
for (int i = 0; i < b; i++) {
board[0][i] = 2;
board[b - 1][i] = 2;
board[i][b - 1] = 2;
}
for (int i = 0; i < staffs.size(); i++) {
int row = staffs[i].row;
int col = staffs[i].col;
board[row][col] = 1;
}
}
void MovePresents(vector<present>& presents, vector<vector<int>>& board) {
for (auto it = presents.begin(); it != presents.end();) {
if (it->row == b - 1 && it->col == 0) {
board[b - 1][0] = 2;
it = presents.erase(it);
continue;
}
board[it->row][it->col] = 2;
int nextRow = it->row + dRow[it->dir];
int nextCol = it->col + dCol[it->dir];
if (nextRow >= b || nextCol >= b) {
it->dir = it->dir + 1;
nextRow = it->row + dRow[it->dir];
nextCol = it->col + dCol[it->dir];
}
it->row = nextRow;
it->col = nextCol;
board[it->row][it->col] = 3;
++it;
}
}
void DoPacking(vector<staff>& staffs, vector<vector<int>>& board, vector<present>& presents) {
for (int i = 0; i < staffs.size(); i++) {
if (staffs[i].isWorking) {
staffs[i].workingTime++;
if (staffs[i].workingTime == staffs[i].time) {
staffs[i].isWorking = false;
staffs[i].workingTime = 0;
}
}
else {
for (int j = 0; j < 3; j++) {
int row = staffs[i].row + findRow[j];
int col = staffs[i].col + findCol[j];
if (board[row][col] == 3) {
ans++;
staffs[i].isWorking = true;
staffs[i].workingTime++;
auto it = find_if(presents.begin(), presents.end(),
[&](present p) {return row == p.row && col == p.col; }
);
board[row][col] = 2;
presents.erase(it);
break;
}
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> b >> n >> m;
vector<staff> staffs;
for (int i = 0; i < n; i++) {
int row, col, time;
cin >> row >> col >> time;
staffs.push_back(staff(row, col, time));
}
// 0 : 빈공간
// 1 : 직원
// 2 : 컨베이어
// 3 : 선물
vector<vector<int>> board(b, vector<int>(b, 0));
Init(board, staffs);
vector<present> presents;
int cnt = 0;
while (true) {
DoPacking(staffs, board, presents);
MovePresents(presents, board);
if (cnt < m) {
presents.push_back({ 0,0 });
board[0][0] = 3;
cnt++;
}
if (presents.size() == 0) break;
}
cout << ans;
};
'알고리즘' 카테고리의 다른 글
백준 8972번 미친 아두이노 C++ (0) | 2022.02.03 |
---|---|
백준 10836번 여왕벌 C++ (0) | 2022.02.03 |
백준 2818번 숙제하기 싫을 때 C++ (0) | 2022.02.01 |
백준 1680번 쓰레기 수거 C++ (0) | 2022.02.01 |
백준 2016번 미팅 주선하기 C++ (0) | 2022.02.01 |