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 | 31 |
Tags
- ๊ทธ๋ฆฌ๋
- ๋ฐ์ดํฌ์คํธ๋ผ
- ๋ฌธ์์ด
- ๋งต
- error
- ์คํ
- java
- ์ด๋ถ ํ์
- DP
- dynamic debugging
- ๋ถํ ์ ๋ณต
- web
- ์ต๋จ ๊ฒฝ๋ก
- thymeleaf
- ์ฐ์ ์์ ํ
- BFS
- ๋ฐฑํธ๋ํน
- JPA
- GCP
- ์์ ์ ๋ ฌ
- Reversing
- CVE
- ๊ตฌํ
- ๋์ ํฉ
- Spring
- c++
- OS
- dfs
- ์ฌ๊ท
- ์๋ฎฌ๋ ์ด์
Archives
- Today
- Total
hades
[Baekjoon] 1149๋ฒ: RGB๊ฑฐ๋ฆฌ ๋ณธ๋ฌธ
๐ฅ ๋ฌธ์
https://www.acmicpc.net/problem/1149
๐ ์ค๊ณ
๋ฐ๋ก ์ด์ ์ง์์์ ์๊ณผ ๋ค๋ฅธ ์์ ์น ํ๊ธฐ ์ํด ๊ฐ ์ง์ ๊ณ ๋ คํ ๋๋ง๋ค ํ์์ ํ๋์ฉ ๊บผ๋ด์ ์น ํ ์์ ๊ฒฐ์ ํ๋ค. ํ์๋ ์ด์ ์ง์์์ ์๊ณผ ๋น์ฉ์ด ์ ์ฅ๋์ด ์๋ค.
ํ์ง๋ง, ์น ํ ์ ์๋ ์์ ๋ํ์ฌ ๋ชจ๋ ๊ฒฝ์ฐ๋ฅผ ๊ณ ๋ คํ ๊ฒฝ์ฐ, ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ๊ฐ ๋ฐ์ํ๋ค.
๋ฐ๋ผ์, ์ด๋ค ์ง๊น์ง ์์ ์น ํ์ ๋, ์ต์ ๋น์ฉ์ ๊ฐฑ์ ํ๊ณ , ๊ทธ ๋์ ์๊ณผ ๋น์ฉ์ ํ์ ์ ์ฅํ๋ฉด ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์๋ค.
ํ๋ฅผ ์ด์ฉํ์ง ์๊ณ , ๊ฐ๋จํ๊ฒ ๋ค์ด๋๋ฏน ํ๋ก๊ทธ๋๋ฐ์ผ๋ก ํด๊ฒฐํ ์๋ ์๋ค. ์์ด๋์ด๋ ๊ฐ๋ค.
๐ ํ์ด1
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int n, result = 1e9 + 7;
queue<pair<int, int>> q;
vector<vector<int>> cur_min_cost(1000, vector<int>(3, 1e9 + 7));
vector<vector<int>> costs;
vector<int> cost(3);
void bfs() {
for (int i = 0; i < 3; i++) {
q.push({ i, costs[0][i] });
}
for (int i = 1; i < n; i++) {
int size = q.size();
for (int k = 0; k < size; k++) {
int before_color = q.front().first;
int before_cost = q.front().second;
q.pop();
for (int j = 0; j < 3; j++) {
if (j != before_color) {
int cur_color = j;
int cur_cost = before_cost + costs[i][j];
cur_min_cost[i][j] = min(cur_min_cost[i][j], cur_cost);
}
}
}
for (int j = 0; j < 3; j++) {
q.push({ j, cur_min_cost[i][j] });
}
}
while (!q.empty()) {
result = min(result, q.front().second);
q.pop();
}
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
cin >> cost[j];
}
costs.push_back(cost);
}
bfs();
cout << result << "\n";
return 0;
}
๐ ํ์ด2
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int n, result = 1e9 + 7;
queue<pair<int, int>> q;
vector<vector<int>> cur_min_cost(1000, vector<int>(3, 1e9 + 7));
vector<vector<int>> costs;
vector<int> cost(3);
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
cin >> cost[j];
}
costs.push_back(cost);
}
for (int i = 0; i < 3; i++) {
cur_min_cost[0][i] = costs[0][i];
}
for (int i = 1; i < n; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
if (j != k) {
cur_min_cost[i][j] = min(cur_min_cost[i][j], cur_min_cost[i - 1][k] + costs[i][j]);
}
}
}
}
for (int i = 0; i < 3; i++) {
result = min(result, cur_min_cost[n - 1][i]);
}
cout << result << "\n";
return 0;
}
'๐ PS > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 1629๋ฒ: ๊ณฑ์ (0) | 2024.08.03 |
---|---|
[Baekjoon] 1167๋ฒ: ํธ๋ฆฌ์ ์ง๋ฆ (0) | 2024.08.02 |
[Baekjoon] 1043๋ฒ: ๊ฑฐ์ง๋ง (0) | 2024.08.01 |
[Baekjoon] 28702๋ฒ: FizzBuzz (0) | 2024.07.31 |
[Baekjoon] 30804๋ฒ: ๊ณผ์ผ ํํ๋ฃจ (0) | 2024.07.30 |