제출 #290215

#제출 시각아이디문제언어결과실행 시간메모리
290215SamAnd고대 책들 (IOI17_books)C++17
12 / 100
2127 ms530936 KiB
#include "books.h"
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
typedef long long ll;
const int N = 1003;

int n;

int bfs(vector<int> p, int s)
{
    for (int i = 0; i < n; ++i)
        ++p[i];
    map<pair<vector<int>, pair<int, int> >, int> mp;
    deque<pair<vector<int>, pair<int, int> > > q;
    q.push_back(m_p(p, m_p(s, 0)));
    mp[m_p(p, m_p(s, 0))] = 0;
    while (1)
    {
        pair<vector<int>, pair<int, int> > t = q.front();
        q.pop_front();
        bool z = true;
        if (t.se.fi != s)
            z = false;
        for (int i = 0; i < n; ++i)
        {
            if (t.fi[i] != i + 1)
            {
                z = false;
                break;
            }
        }
        if (z)
            return mp[t];

        pair<vector<int>, pair<int, int> > h;

        h = t;
        if (h.se.fi != n - 1)
        {
            ++h.se.fi;
            if (mp.find(h) == mp.end())
            {
                mp[h] = mp[t] + 1;
                q.push_back(h);
            }
        }

        h = t;
        if (h.se.fi != 0)
        {
            --h.se.fi;
            if (mp.find(h) == mp.end())
            {
                mp[h] = mp[t] + 1;
                q.push_back(h);
            }
        }

        h = t;
        swap(h.se.se, h.fi[h.se.fi]);
        if (mp.find(h) == mp.end())
        {
            mp[h] = mp[t];
            q.push_front(h);
        }
    }
}

long long minimum_walk(std::vector<int> p, int s)
{
    n = sz(p);
	return bfs(p, 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...
#Verdict Execution timeMemoryGrader output
Fetching results...