Language: C++

#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<set>
using namespace std;

struct Node{
	vector<int> arr;
	int step;
};
const int tx[]={1,0,-1,0};
const int ty[]={0,1,0,-1};
vector<int> start,end;
queue<Node> sq;
set<int> pan;

void input()
{
	int tmp;
	for(int i=0;i!=3;++i)
		for(int j=0;j!=3;++j)
		{
			cin>>tmp;
			start.push_back(tmp);
		}
	for(int i=0;i!=8;++i)
		end.push_back(i+1);
	end.push_back(0);
}

Node makenod(vector<int> tmp,int step)
{
	Node res;
	res.arr=tmp;
	res.step=step;
	return res;
}

int vton(vector<int> tmp)
{
	int res=0;
	for(int i=0;i!=9;++i)
		res=res*10+tmp[i];
	return res;
}

/*
void print(vector<int> tmp)
{
	for(int i=1;i!=10;++i)
	{
		cout<<tmp[i-1]<<' ';
		if(i%3==0)	cout<<endl;
	}
	cout<<endl;
}
*/
int main()
{
	input();

	sq.push(makenod(start,0));
	pan.insert(vton(start));
	
	int ans;
	while(!sq.empty())
	{
		Node now=sq.front();	sq.pop();

		print(now.arr);

		if(now.arr==end)
		{
			ans=now.step;
			break;
		}

		int ith;
		for(int i=0;i!=9;++i)
			if(now.arr[i]==0)
			{
				ith=i;
				break;
			}
		int x=ith/3,y=ith%3;
		
		for(int i=0;i!=4;++i)
		{
			int nx=x+tx[i],ny=y+ty[i];
			if(nx>=0 && nx<3 && ny>=0 && ny<3)
			{
				swap(now.arr[nx*3+ny],now.arr[ith]);
				int num=vton(now.arr);
				if(pan.count(num)==0)
				{
					sq.push(makenod(now.arr,now.step+1));
					pan.insert(num);		
				}
				swap(now.arr[nx*3+ny],now.arr[ith]);
			}
		}
	}
	
	cout<<ans<<endl;
		
	return 0;
}

Compiler output:
a.cc: In function 'void input()':
a.cc:28:17: error: reference to 'end' is ambiguous
   28 |                 end.push_back(i+1);
      |                 ^~~
In file included from /usr/include/c++/14/string:53,
                 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:2:
/usr/include/c++/14/bits/range_access.h:116:37: note: candidates are: 'template<class _Tp> const _Tp* std::end(const valarray<_Tp>&)'
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~
/usr/include/c++/14/bits/range_access.h:115:31: note:                 'template<class _Tp> _Tp* std::end(valarray<_Tp>&)'
  115 |   template<typename _Tp> _Tp* end(valarray<_Tp>&) noexcept;
      |                               ^~~
/usr/include/c++/14/bits/range_access.h:106:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::end(_Tp (&)[_Nm])'
  106 |     end(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~
/usr/include/c++/14/bits/range_access.h:85:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(const _Container&)'
   85 |     end(const _Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/bits/range_access.h:74:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(_Container&)'
   74 |     end(_Container& __cont) -> decltype(__cont.end())
      |     ^~~
In file included from /usr/include/c++/14/bits/algorithmfwd.h:39,
                 from /usr/include/c++/14/bits/stl_algo.h:59,
                 from /usr/include/c++/14/algorithm:61,
                 from a.cc:1:
/usr/include/c++/14/initializer_list:99:5: note:                 'template<class _Tp> constexpr const _Tp* std::end(initializer_list<_Tp>)'
   99 |     end(initializer_list<_Tp> __ils) noexcept
      |     ^~~
a.cc:14:19: note:                 'std::vector<int> end'
   14 | vector<int> start,end;
      |                   ^~~
a.cc:29:9: error: reference to 'end' is ambiguous
   29 |         end.push_back(0);
      |         ^~~
/usr/include/c++/14/bits/range_access.h:116:37: note: candidates are: 'template<class _Tp> const _Tp* std::end(const valarray<_Tp>&)'
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~
/usr/include/c++/14/bits/range_access.h:115:31: note:                 'template<class _Tp> _Tp* std::end(valarray<_Tp>&)'
  115 |   template<typename _Tp> _Tp* end(valarray<_Tp>&) noexcept;
      |                               ^~~
/usr/include/c++/14/bits/range_access.h:106:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::end(_Tp (&)[_Nm])'
  106 |     end(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~
/usr/include/c++/14/bits/range_access.h:85:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(const _Container&)'
   85 |     end(const _Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/bits/range_access.h:74:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(_Container&)'
   74 |     end(_Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/initializer_list:99:5: note:                 'template<class _Tp> constexpr const _Tp* std::end(initializer_list<_Tp>)'
   99 |     end(initializer_list<_Tp> __ils) noexcept
      |     ^~~
a.cc:14:19: note:                 'std::vector<int> end'
   14 | vector<int> start,end;
      |                   ^~~
a.cc: In function 'int main()':
a.cc:71:17: error: 'print' was not declared in this scope; did you mean 'printf'?
   71 |                 print(now.arr);
      |                 ^~~~~
      |                 printf
a.cc:73:29: error: reference to 'end' is ambiguous
   73 |                 if(now.arr==end)
      |                             ^~~
/usr/include/c++/14/bits/range_access.h:116:37: note: candidates are: 'template<class _Tp> const _Tp* std::end(const valarray<_Tp>&)'
  116 |   template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
      |                                     ^~~
/usr/include/c++/14/bits/range_access.h:115:31: note:                 'template<class _Tp> _Tp* std::end(valarray<_Tp>&)'
  115 |   template<typename _Tp> _Tp* end(valarray<_Tp>&) noexcept;
      |                               ^~~
/usr/include/c++/14/bits/range_access.h:106:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::end(_Tp (&)[_Nm])'
  106 |     end(_Tp (&__arr)[_Nm]) noexcept
      |     ^~~
/usr/include/c++/14/bits/range_access.h:85:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(const _Container&)'
   85 |     end(const _Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/bits/range_access.h:74:5: note:                 'template<class _Container> constexpr decltype (__cont.end()) std::end(_Container&)'
   74 |     end(_Container& __cont) -> decltype(__cont.end())
      |     ^~~
/usr/include/c++/14/initializer_list:99:5: note:                 'template<class _Tp> constexpr const _Tp* std::end(initializer_list<_Tp>)'
   99 |     end(initializer_list<_Tp> __ils) noexcept
      |     ^~~
a.cc:14:19: note:                 'std::vector<int> end'
   14 | vector<int> start,end;
      |                   ^~~