https://www.acmicpc.net/problem/2310
각 방에 대하여 레프리 콘인 경우와 트롤인 경우, 정상인 경우를 모두 나누어 생각하여 각각에 대해서 예외처리를 해주면 되는 문제.
깊이 우선 탐색을 이용하여, 끝까지 도달하지 못한 경우에는 .No를, 그 외에는 Yes를 출력하도록 하면 된다.
#include<iostream>
#include<vector>
using namespace std;
bool flag;
int mon, N;
vector<bool> visited(1001);
vector<pair<pair<char, int>, vector<int>>> room(1001);
void DFS(int U){
if(U==N){
flag=true;
return;
}
visited[U]=true;
for(int i=0; i<room[U].second.size(); i++){
int nx=room[U].second[i];
int cst=room[nx].first.second;
char tp=room[nx].first.first;
if(visited[nx])continue;
if(tp=='T'){
if(mon<cst)return;
mon-=cst;
DFS(nx);
mon+=cst;
} else if(tp=='L') {
int tmp=mon;
if(mon<cst)mon=cst;
DFS(nx);
mon=tmp;
} else {
DFS(nx);
}
if(flag)return;
}
}
int main(){
int cost;
char type;
while(1){
visited.assign(1001, false);
room.assign(1001, {{'E', 0}, {}});
flag = false;
mon = 0;
cin>>N;
if(N==0)break;
for(int i=1; i<=N; i++){
cin>>type>>cost;
room[i].first.first=type;
room[i].first.second=cost;
while(1){
int v;
cin>>v;
room[i].second.push_back(v);
if(v==0)break;
}
}
DFS(1);
if(flag==true){
cout<<"Yes\n";
} else {
cout<<"No\n";
}
}
}
'알고리즘' 카테고리의 다른 글
| Boj 1043 거짓말 Python - 분리 집합 (3) | 2025.08.17 |
|---|---|
| Boj 14499 주사위 굴리기 Python (6) | 2025.07.29 |
| Boj 14501 c++ 퇴사 (0) | 2025.07.20 |
| Boj 7571 c++ 점 모으기 (0) | 2025.07.20 |
| Boj 32069 c++ 가로등 (1) | 2025.07.20 |