#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int NOT_VIS = 0;
const int CUR_VIS = 1;
const int VIS = 2;
map<pair<vector<int>, pair<int, int>>, pair<int, ll>> vis {};
inline ll recSolve(int pos, vector<int> vals, int carry) {
// move out of bounds
if (pos >= (int)vals.size() || pos < 0)
return 1e9;
// don't allow to cycle
if (vis[{vals, {pos, carry}}].first == CUR_VIS)
return 1e9;
// if already visited, return answer
if (vis[{vals, {pos, carry}}].first == VIS)
return vis[{vals, {pos, carry}}].second;
ll mnAns = 1e9;
bool ok = true;
for (int i = 0; i < (int)vals.size(); i++)
if (vals[i] != i)
ok = false;
// if we have correct tables
if (ok) {
//printf("food bruh %d %d\n", pos);
vis[{vals, {pos, carry}}] = {VIS, pos};
return pos;
}
// mark as visiting
vis[{vals, {pos, carry}}] = {CUR_VIS, 1e9};
// jump to the right
mnAns = min(mnAns, 1 + recSolve(pos + 1, vals, carry));
// jump to the left
mnAns = min(mnAns, 1 + recSolve(pos - 1, vals, carry));
// change carried book
swap(carry, val[pos]);
mnAns = min(mnAns, recSolve(pos, vals, carry));
swap(carry, val[pos]);
vis[{vals, {pos, carry}}] = {VIS, mnAns};
return mnAns;
}
ll minimum_walk(vector<int> p, int s) {
int n = (int)p.size();
return recSolve(0, p, -1);
}
Compilation message
books.cpp: In function 'long long int recSolve(int, std::vector<int>, int)':
books.cpp:44:14: error: 'val' was not declared in this scope; did you mean 'vals'?
44 | swap(carry, val[pos]);
| ^~~
| vals
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:52:9: warning: unused variable 'n' [-Wunused-variable]
52 | int n = (int)p.size();
| ^