Language: C++

#include<queue>
using namespace std;
int vx[]={ 1, 1, 0, 0,-1,-1};
int vy[]={ 0,-1, 1,-1, 1, 0};
struct P{
	int x,y;
};
int n,k;
struct Snake{
	int step;
	P body[8];
	bool operator<(const Snake&b)const{
		for(int i=0;i<n;i++){
			if(body[i].x!=b.body[i].x)return body[i].x<b.body[i].x;
			if(body[i].y!=b.body[i].y)return body[i].y<b.body[i].y;
		}
		return false;
	}
	P& operator[](int i){
		return body[i];
	}
};
queue<Snake> que;
int X,Y;
int ans;
P rock[100];
Snake body;
set<Snake> s;
int distance(){
	int dis, dx=X-body[0].x, dy=Y-body[0].y;
	if(dx*dy<0)dis= abs(dx)<abs(dy)? abs(dy):abs(dx);
	else dis= abs(dx)+abs(dy);
	return dis;
}
inline bool overlap(P &a,P &b){
	return a.x==b.x && a.y==b.y;
}
bool neibor(P &a,P &b){
	int dx=a.x-b.x;
	int dy=a.y-b.y;
	return abs(dx)+abs(dy)<=1
		|| abs(dx)==1 && -dx==dy;
}
bool is_dead(int no){
	for(int i=0;i<no-1;i++){
		if(neibor(body[no], body[i])){
				return true;
		}
	}
	return false;
}
bool on_rock(int no){
	for(int i=0;i<k;i++){
		if(rock[i].x==body[no].x && rock[i].y==body[no].y)
			return true;
	}
	return false;
}
void search(int step);
bool move(int no){
	if(no==1||no==2)if(ans-body.step <= distance())return false;
	for(int i=0;i<6;i++){
		body[no].x+=vx[i];
		body[no].y+=vy[i];
		if(!is_dead(no) && !on_rock(no) 
			&& (no==0  ||neibor(body[no-1],body[no]) && !overlap(body[no-1],body[no]))
			&& (n<=no+1||neibor(body[no],body[no+1]) && !overlap(body[no],body[no+1])) ){
			if(no+2<n){if(move(no+2))return true;}
			else {
				if(X==body[0].x && Y==body[0].y)return true;
				body.step++;
				if(s.find(body)==s.end()){
					s.insert(body);
					que.push(body);
				}
				body.step--;
			}
		}
		body[no].x-=vx[i];
		body[no].y-=vy[i];
	}
	if(!is_dead(no) ){
		if(no+1<n){if(move(no+1))return true;}
		else {
			if(X==body[0].x && Y==body[0].y)return true;
			body.step++;
			if(s.find(body)==s.end()){
				s.insert(body);
				que.push(body);
			}
			body.step--;
		}
	}
	return false;
}
main(){
	while(cin>>n,n){
		s.clear();
		while(!que.empty())que.pop();
		body.step=0;
		for(int i=0;i<n;i++){
			cin>>body[i].x>>body[i].y;
			
		}
		cin>>k;
		for(int i=0;i<k;i++){
			cin>>rock[i].x>>rock[i].y;	
		}
		cin>>X>>Y;
		ans=20;
		que.push(body);
		while(!que.empty()){
			body=que.front();
			que.pop();
			if(ans-body.step <= distance())continue;
			if(body.step==20-1)break;
			if(move(0)){
				ans= body.step+1;
				break;
			}
		}
		cout<<ans<<endl;
	}
}

Compiler output:
a.cc:28:1: error: 'set' does not name a type
   28 | set<Snake> s;
      | ^~~
a.cc: In function 'int distance()':
a.cc:31:25: error: 'abs' was not declared in this scope; did you mean 'ans'?
   31 |         if(dx*dy<0)dis= abs(dx)<abs(dy)? abs(dy):abs(dx);
      |                         ^~~
      |                         ans
a.cc:32:19: error: 'abs' was not declared in this scope; did you mean 'ans'?
   32 |         else dis= abs(dx)+abs(dy);
      |                   ^~~
      |                   ans
a.cc: In function 'bool neibor(P&, P&)':
a.cc:41:16: error: 'abs' was not declared in this scope; did you mean 'ans'?
   41 |         return abs(dx)+abs(dy)<=1
      |                ^~~
      |                ans
a.cc: In function 'bool move(int)':
a.cc:72:36: error: 's' was not declared in this scope
   72 |                                 if(s.find(body)==s.end()){
      |                                    ^
a.cc:87:28: error: 's' was not declared in this scope
   87 |                         if(s.find(body)==s.end()){
      |                            ^
a.cc: At global scope:
a.cc:96:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   96 | main(){
      | ^~~~
a.cc: In function 'int main()':
a.cc:97:15: error: 'cin' was not declared in this scope
   97 |         while(cin>>n,n){
      |               ^~~
a.cc:2:1: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
    1 | #include<queue>
  +++ |+#include <iostream>
    2 | using namespace std;
a.cc:98:17: error: 's' was not declared in this scope
   98 |                 s.clear();
      |                 ^
a.cc:122:17: error: 'cout' was not declared in this scope
  122 |                 cout<<ans<<endl;
      |                 ^~~~
a.cc:122:17: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:122:28: error: 'endl' was not declared in this scope
  122 |                 cout<<ans<<endl;
      |                            ^~~~
a.cc:2:1: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
    1 | #include<queue>
  +++ |+#include <ostream>
    2 | using namespace std;