Language: C++

#include <iostream>
#include <set>
#include <vector>

using namespace std;

char dx[] = {0,1,1,0,-1,-1};
char dy[] = {-1,-1,0,1,1,0};

set< vector<int> > prev;
vector< vector<int> > cur, next; 
int x[8], y[8], u[100], v[100], n, k, X, Y, ans;

void move(int step, int pos){
	if(ans != 20) return;
	if(pos >= n) {
		for(int i=0;i<n;i++)
			for(int j=i+2;j<n;j++)
				for(int k=0;k<6;k++)
					if(x[i]+dx[k]==x[j]&&y[i]+dy[k]==y[j]) return;
		vector<int> v;
		for(int i=0;i<n;i++) v.push_back(x[i]), v.push_back(y[i]);
		if(x[0]==X&&y[0]==Y) ans = step;
		if(!prev.count(v)){
			prev.insert(v);
			next.push_back(v);
		}
		return;
	}
	int px = x[pos], py = y[pos];
	for(int i=0;i<6;i++){
		int nx = x[pos]+dx[i], ny = y[pos]+dy[i];
		bool check = false, flagF = false, flagB = false;
		if(pos == 0 && step+max(abs(X-nx),abs(Y-ny)) > 19) continue;
		if(pos == 1 && step+max(abs(X-x[0]),abs(Y-y[0]))+abs(max(abs(X-nx),abs(Y-ny))-1) > 19) continue;
		for(int j=0;j<min(pos+1,n);j++){
			if(j==pos) continue;
			if(x[j]==nx&&y[j]==ny) check = true;
		}
		if(check) continue;
		if(pos>0){
			for(int k=0;k<6;k++)
				if(x[pos-1]+dx[k] == nx && y[pos-1]+dy[k] == ny) flagF = true;
			if(!flagF) continue;
		}
		if(pos<n-1){
			for(int k=0;k<6;k++)
				if(x[pos+1]+dx[k] == nx && y[pos+1]+dy[k] == ny) flagB = true;
			if(!flagB) continue;
		}
		for(int j=0;j<k&&!check;j++)
			if(u[j] == nx && v[j] == ny) check = true;
		if(check) continue;

		x[pos] = nx, y[pos] = ny;
		move(step, pos+2);
		x[pos] = px, y[pos] = py;
	}
	move(step, pos+1);
}

int main(){
	set< vector<int> >::iterator it;
	while(cin >> n, n){
		for(int i=0;i<n;i++) cin >> x[i] >> y[i];
		cin >> k;
		for(int i=0;i<k;i++) cin >> u[i] >> v[i];
		cin >> X >> Y;
		ans = 20;
		vector<int> v;
		for(int i=0;i<n;i++) v.push_back(x[i]), v.push_back(y[i]);
		prev.clear();   cur.clear();
		prev.insert(v); cur.push_back(v);
		for(int i=1;ans==20&&i<=19;i++){
			next.clear();
			for(int k=0;k<cur.size();k++){
				for(int j=0;j<n;j++) x[j] = cur[k][2*j], y[j] = cur[k][2*j+1];
				move(i, 0);
			}
			cur = next;
		}
		cout << ans << endl;
	}
}

Compiler output:
a.cc: In function 'void move(int, int)':
a.cc:24:21: error: reference to 'prev' is ambiguous
   24 |                 if(!prev.count(v)){
      |                     ^~~~
In file included from /usr/include/c++/14/string:47,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:10:20: note:                 'std::set<std::vector<int> > prev'
   10 | set< vector<int> > prev;
      |                    ^~~~
a.cc:25:25: error: reference to 'prev' is ambiguous
   25 |                         prev.insert(v);
      |                         ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:10:20: note:                 'std::set<std::vector<int> > prev'
   10 | set< vector<int> > prev;
      |                    ^~~~
a.cc:26:25: error: reference to 'next' is ambiguous
   26 |                         next.push_back(v);
      |                         ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:28: note:                 'std::vector<std::vector<int> > next'
   11 | vector< vector<int> > cur, next;
      |                            ^~~~
a.cc: In function 'int main()':
a.cc:72:17: error: reference to 'prev' is ambiguous
   72 |                 prev.clear();   cur.clear();
      |                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:10:20: note:                 'std::set<std::vector<int> > prev'
   10 | set< vector<int> > prev;
      |                    ^~~~
a.cc:73:17: error: reference to 'prev' is ambiguous
   73 |                 prev.insert(v); cur.push_back(v);
      |                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:10:20: note:                 'std::set<std::vector<int> > prev'
   10 | set< vector<int> > prev;
      |                    ^~~~
a.cc:75:25: error: reference to 'next' is ambiguous
   75 |                         next.clear();
      |                         ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:28: note:                 'std::vector<std::vector<int> > next'
   11 | vector< vector<int> > cur, next;
      |                            ^~~~
a.cc:80:31: error: reference to 'next' is ambiguous
   80 |                         cur = next;
      |                               ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:11:28: note:                 'std::vector<std::vector<int> > next'
   11 | vector< vector<int> > cur, next;
      |                            ^~~~