Language: C++

// ------ Includes ------ //

#include <set>
#include <queue>
#include <vector>
#include <utility>
#include <iostream>

using namespace std;


// ------ Directions ------ //

const int dx[4] = { 0, 1, 0, -1 };
const int dy[4] = { 1, 0, -1, 0 };


// ------ BFS ------ //

int BFS(vector<vector<int> > p)
{
	// ------ Initialize ------ //

	vector<vector<int> > lastboard = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0 } };

	set<vector<vector<int> >, int> D;

	queue<pair<vector<vector<int> >, int> > que;

	D.insert(p); que.push(make_pair(p, 0));


	// ------ Search ------ //

	while (!que.empty())
	{
		pair<vector<vector<int> >, int> p1 = que.front(); que.pop();


		// ------ 0 Searching ------ //

		int tx = -1, ty = -1;

		for (int i = 0; i < p1.first.size(); i++)
		{
			for (int j = 0; j < p1.first[i].size(); j++)
			{
				if (p1.first[i][j] == 0)
				{
					tx = j; ty = i;
				}
			}
		}


		// ------ Moving ------ //

		for (int dir = 0; dir < 4; dir++)
		{
			if (0 <= tx + dx[dir] && tx + dx[dir] < p1.first[0].size() && 0 <= ty + dy[dir] && ty + dy[dir] < p1.first.size())
			{
				vector<vector<int> > p2 = p1.first;

				swap(p2[ty][tx], p2[ty + dy[dir]][tx + dx[dir]]);

				if (D.find(p2) == D.end())
				{
					if (p2 == lastboard)
					{
						return p1.second;
					}

					D.insert(p2);

					que.push(make_pair(p2, p1.second + 1));
				}
			}
		}
	}

	return -1;
}

int main()
{
	vector<vector<int> > board(3, vector<int>(3, -1));;

	for (int i = 0; i < board.size(); i++)
	{
		for (int j = 0; j < board[i].size(); j++)
		{
			cin >> board[i][j];
		}
	}

	cout << BFS(board) << endl;

	return 0;
}

Compiler output:
In file included from /usr/include/c++/14/set:62,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_tree.h: In instantiation of 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::find(const _Key&) [with _Key = std::vector<std::vector<int> >; _Val = std::vector<std::vector<int> >; _KeyOfValue = std::_Identity<std::vector<std::vector<int> > >; _Compare = int; _Alloc = std::allocator<std::vector<std::vector<int> > >; iterator = std::_Rb_tree<std::vector<std::vector<int> >, std::vector<std::vector<int> >, std::_Identity<std::vector<std::vector<int> > >, int, std::allocator<std::vector<std::vector<int> > > >::iterator]':
/usr/include/c++/14/bits/stl_set.h:797:25:   required from 'std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::find(const key_type&) [with _Key = std::vector<std::vector<int> >; _Compare = int; _Alloc = std::allocator<std::vector<std::vector<int> > >; iterator = std::_Rb_tree<std::vector<std::vector<int> >, std::vector<std::vector<int> >, std::_Identity<std::vector<std::vector<int> > >, int, std::allocator<std::vector<std::vector<int> > > >::const_iterator; key_type = std::vector<std::vector<int> >]'
  797 |       { return _M_t.find(__x); }
      |                ~~~~~~~~~^~~~~
a.cc:66:15:   required from here
   66 |                                 if (D.find(p2) == D.end())
      |                                     ~~~~~~^~~~
/usr/include/c++/14/bits/stl_tree.h:2532:40: error: expression cannot be used as a function
 2532 |               || _M_impl._M_key_compare(__k,
      |                  ~~~~~~~~~~~~~~~~~~~~~~^~~~~
 2533 |                                         _S_key(__j._M_node))) ? end() : __j;
      |                                         ~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h: In instantiation of 'std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_unique_pos(const key_type&) [with _Key = std::vector<std::vector<int> >; _Val = std::vector<std::vector<int> >; _KeyOfValue = std::_Identity<std::vector<std::vector<int> > >; _Compare = int; _Alloc = std::allocator<std::vector<std::vector<int> > >; key_type = std::vector<std::vector<int> >]':
/usr/include/c++/14/bits/stl_tree.h:2169:4:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = const std::vector<std::vector<int> >&; _Key = std::vector<std::vector<int> >; _Val = std::vector<std::vector<int> >; _KeyOfValue = std::_Identity<std::vector<std::vector<int> > >; _Compare = int; _Alloc = std::allocator<std::vector<std::vector<int> > >]'
 2169 |         = _M_get_insert_unique_pos(_KeyOfValue()(__v));
      |           ^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:514:25:   required from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = std::vector<std::vector<int> >; _Compare = int; _Alloc = std::allocator<std::vector<std::vector<int> > >; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree<std::vector<std::vector<int> >, std::vector<std::vector<int> >, std::_Identity<std::vector<std::vector<int> > >, int, std::allocator<std::vector<std::vector<int> > > >::const_iterator; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other = std::allocator<std::vector<std::vector<int> > >; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key> = __gnu_cxx::__alloc_traits<std::allocator<std::vector<std::vector<int> > >, std::vector<std::vector<int> > >::rebind<std::vector<std::vector<int> > >; typename _Alloc::value_type = std::vector<std::vector<int> >; value_type = std::vector<std::vector<int> >]'
  514 |           _M_t._M_insert_unique(__x);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~
a.cc:30:10:   required from here
   30 |         D.insert(p); que.push(make_pair(p, 0));
      |         ~~~~~~~~^~~
/usr/include/c++/14/bits/stl_tree.h:2116:42: error: expression cannot be used as a function
 2116 |           __comp = _M_impl._M_key_compare(__k, _S_key(__x));
      |                    ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:2127:33: error: expression cannot be used as a function
 2127 |       if (_M_impl._M_key_compare(_S_key(__j._M_node), __k))
      |           ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h: In instantiation of 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_(_Base_ptr, _Base_ptr, _Arg&&, _NodeGen&) [with _Arg = const std::vector<std::vector<int> >&; _NodeGen = std::_Rb_tree<std::vector<std::vector<int> >, std::vector<std::vector<int> >, std::_Identity<std::vector<std::vector<int> > >, int, std::allocator<std::vector<std::vector<int> > > >::_Alloc_node; _Key = std::vector<std::vector<int> >; _Val = std::vector<std::vector<int> >; _KeyOfValue = std::_Identity<std::vector<std::vector<int> > >; _Compare = int; _Alloc = std::allocator<std::vector<std::vector<int> > >; iterator = std::_Rb_tree<std::vector<std::vector<int> >, std::vector<std::vector<int> >, std::_Identity<std::vector<std::vector<int> > >, int, std::allocator<std::vector<std::vector<int> > > >::iterator; _Base_ptr = std::_Rb_tree_node_base*]':
/usr/include/c++/14/bits/stl_tree.h:2174:26:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = const std::vector<std::vector<int> >&; _Key = std::vector<std::vector<int> >; _Val = std::vector<std::vector<int> >; _KeyOfValue = std::_Identity<std::vector<std::vector<int> > >; _Compare = int; _Alloc = std::allocator<std::vector<std::vector<int> > >]'
 2174 |           return _Res(_M_insert_(__res.first, __res.second,
      |                       ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
 2175 |                                  _GLIBCXX_FORWARD(_Arg, __v), __an),
      |                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:514:25:   required from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = std::vector<std::vector<int> >; _Compare = int; _Alloc = std::allocator<std::vector<std::vector<int> > >; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree<std::vector<std::vector<int> >, std::vector<std::vector<int> >, std::_Identity<std::vector<std::vector<int> > >, int, std::allocator<std::vector<std::vector<int> > > >::const_iterator; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other = std::allocator<std::vector<std::vector<int> > >; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key> = __gnu_cxx::__alloc_traits<std::allocator<std::vector<std::vector<int> > >, std::vector<std::vector<int> > >::rebind<std::vector<std::vector<int> > >; typename _Alloc::value_type = std::vector<std::vector<int> >; value_type = std::vector<std::vector<int> >]'
  514 |           _M_t._M_insert_unique(__x);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~
a.cc:30:10:   required from here
   30 |         D.insert(p); que.push(make_pair(p, 0));
      |         ~~~~~~~~^~~
/usr/include/c++/14/bits/stl_tree.h:1824:56: error: expression cannot be used as a function
 1824 |                               || _M_impl._M_key_compare(_KeyOfValue()(__v),
      |                                  ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
 1825 |                                                         _S_key(__p)));
      |                                                         ~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h: In instantiation of 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_lower_bound(_Link_type, _Base_ptr, const _Key&) [with _Key = std::vector<std::vector<int> >; _Val = std::vector<std::vector<int> >; _KeyOfValue = std::_Identity<std::vector<std::vector<int> > >; _Compare = int; _Alloc = std::allocator<std::vector<std::vector<int> > >; iterator = std::_Rb_tree<std::vector<std::vector<int> >, std::vector<std::vector<int> >, std::_Identity<std::vector<std::vector<int> > >, int, std::allocator<std::vector<std::vector<int> > > >::iterator; _Link_type = std::_Rb_tree_node<std::vector<std::vector<int> > >*; _Base_ptr = std::_Rb_tree_node_base*]':
/usr/include/c++/14/bits/stl_tree.h:2530:36:   required from 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::find(const _Key&) [with _Key = std::vector<std::vector<int> >; _Val = std::vector<std::vector<int> >; _KeyOfValue = std::_Identity<std::vector<std::vector<int> > >; _Compare = int; _Alloc = std::allocator<std::vector<std::vector<int> > >; iterator = std::_Rb_tree<std::vector<std::vector<int> >, std::vector<std::vector<int> >, std::_Identity<std::vector<std::vector<int> > >, int, std::allocator<std::vector<std::vector<int> > > >::iterator]'
 2530 |       iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
      |                      ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:797:25:   required from 'std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::find(const key_type&) [with _Key = std::vector<std::vector<int> >; _Compare = int; _Alloc = std::allocator<std::vector<std::vector<int> > >; iterator = std::_Rb_tree<std::vector<std::vector<int> >, std::vector<std::vector<int> >, std::_Identity<std::vector<std::vector<int> > >, int, std::allocator<std::vector<std::vector<int> > >