이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
using namespace std;
int n, k;
int BFS(string s, string ans)
{
queue< pair<string, pair<int, int> > > q;
q.push({ s, {0, -1} });
map<string, bool> mp;
while(!q.empty()){
string x=q.front().first; int cnt=q.front().second.first, prv=q.front().second.second;
q.pop();
if(x==ans){ return cnt; }
if(mp.count(x)){ continue; }
mp[x]=1;
for(int i=0; i<n-1; i++){
if(i==prv){ continue; }
string sub=x.substr(i, x.size());
reverse(sub.begin(), sub.end());
string nxt=x.substr(0, i)+sub;
if(mp.count(nxt)==0){ q.push({nxt, {cnt+1, i}}); }
}
}
}
void solve()
{
cin >> n;
string s;
vector< pair<int, int> > v;
for(int i=0; i<n; i++){
int a; cin >> a;
v.push_back({a, i});
s+=(i+'1');
}
sort(v.begin(), v.end());
string ans;
for(int i=n-1; i>=0; i--){
ans+=v[i].second+'1';
}
cout << BFS(s, ans) << "\n";
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t; cin >> t;
while(t--){ solve(); }
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
pancake.cpp: In function 'int BFS(std::string, std::string)':
pancake.cpp:13:44: warning: control reaches end of non-void function [-Wreturn-type]
13 | queue< pair<string, pair<int, int> > > q;
| ^| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |