- 문제 링크 : boj.kr/23749
- 난이도 : G4
- 태그 : BFS
코드
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define INF 0x7FFFFFFF
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll, ll>;
bool eval(string str) {
int score = 0;
for(int i = 0; i < str.size(); i+= 2) {
if(str[i] == str[i+1]) continue;
if(str[i] == 'O') score++;
else score--;
}
if(score > 0) return true;
else return false;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
string str;
for(int i = 0; i < n * 2; i++){
string s;
cin >> s;
str += s;
}
set<string> _s;
queue<pair<string, int>> _q;
_q.push({str, 0});
_s.insert(str);
while(!_q.empty()) {
auto curr = _q.front(); _q.pop();
string ss = curr.first;
if(eval(ss)) {
cout << curr.second;
break;
}
for(int i = 0; i < ss.size(); i++) {
string next = "";
next += ss[i];
next += ss.substr(0, i);
next += ss.substr(i+1);
if(_s.find(next) == _s.end()) {
_q.push({next, curr.second+1});
_s.insert(next);
}
}
}
return 0;
}
풀이
카드의 상태를 기준으로 BFS를 시행한다.
728x90
'PS' 카테고리의 다른 글
BOJ 1188 : 음식 평론가 (0) | 2024.05.23 |
---|---|
BOJ 31873 : 별 수호자 룰루 (0) | 2024.05.22 |
BOJ 31858 : 간단한 순열 문제 (0) | 2024.05.20 |
BOJ 1531 : 무한수열 (2) | 2024.05.19 |
BOJ 29792 : 규칙적인 보스돌이 (0) | 2024.05.18 |