Submission #818927

#TimeUsernameProblemLanguageResultExecution timeMemory
818927vjudge1Growing Vegetable is Fun 3 (JOI19_ho_t3)C++17
5 / 100
1088 ms162184 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll MAXN = 1e5 + 5; const ll INF = 1e9; #define fi first #define se second ll n; string s; map <string, bool> vis; queue <pair <ll, string> > q; bool cek(string x){ for(ll i = 0; i < n-1; i++){ if(x[i] == x[i+1]){ return false; } } return true; } void bfs(string x){ vis[x] = true; q.push({0, x}); while(!q.empty()){ ll discur = q.front().fi; string cur = q.front().se; // cout << cur << " " << discur << endl; q.pop(); if(cek(cur)){ cout << discur << endl; exit(0); } for(ll i = 0; i < n-1; i++){ if(((i-1 >= 0 && cur[i] == cur[i-1]) || (i+2 <n && cur[i+1] == cur[i+2])) && cur[i] != cur[i+1]){ swap(cur[i], cur[i+1]); if(!vis[cur]){ vis[cur] = true; q.push({discur+1, cur}); } swap(cur[i], cur[i+1]); } } } cout << -1 << endl; return; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> s; bfs(s); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...