티스토리 뷰
문제의 규칙에 따라 B, O, J 칸으로 갈 수 있는 칸은 J, B, O이다.
바텀업으로 현재 칸에서 갈 수 있는 칸으로 min 갱신을 하면 된다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
using namespace std;
char arr[1001];
int dp[1001];
const int INF = 123456789;
bool isValid(char curr,char next) {
if (curr == 'B' && next == 'O')return true;
if (curr == 'O' && next == 'J')return true;
if (curr == 'J' && next == 'B')return true;
return false;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
fill(&dp[0], &dp[1001], INF);
int N; cin >> N;
for (int i = 1; i <= N; i++)cin >> arr[i];
dp[1] = 0;
for (int i = 1; i <= N; i++) {
for (int j = i + 1; j <= N; j++) {
if (!isValid(arr[i], arr[j])) continue;
if (dp[j] > dp[i] + (j - i) * (j - i)) {
dp[j] = dp[i] + (j - i) * (j - i);
}
}
}
if (dp[N] == INF)cout << "-1";
else cout << dp[N];
}'PS' 카테고리의 다른 글
| [SWEA] 1213 String (1) | 2024.01.11 |
|---|---|
| [SWEA] 1204 최빈수 구하기 (0) | 2024.01.11 |
| [백준] 25187번 고인물이 싫어요 C++ (0) | 2023.01.20 |
| [백준] 24480번 알고리즘 수업 - 깊이 우선 탐색 2 C++ (0) | 2022.08.05 |
| [백준] 24479번 알고리즘 수업 - 깊이 우선 탐색 1 C++ (0) | 2022.08.05 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 골목길C++
- 11657
- 벨만-포드
- 7511
- 스택
- 어린왕자 C++
- 상범빌딩
- 6018
- 1004
- 중위 표기식 후위 표기식으로 변환
- 1738
- 후위 표기식
- 백준
- 타임머신
- C++
- 골목길
- 1918
- 소셜네트워킹어플리케이션
- 6539
- tea time
- 벨만포드
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
글 보관함
