Language: C++

#include <iostream>
#include <queue>
#include <string>
#include <set>
#include <map>

using namespace std;

map<string, int> stoi;
map<int, string> itos;

int solve(string str){
  bool dp[30][40320];
  dp[0][stoi[str]] = true;
  for(int i=0;i<30;i++){
    for(int j=0;j<40320;j++){
      if(!dp[i][j]) continue;
      if(j==0) return i;
      string str = itos[j];
      for(int k=0;k<8;k++){
	string tmp;
	if(str[k] != '0') continue;
	if(k!=0 && k!=4){
	  tmp = str;
	  swap(tmp[k], tmp[k-1]);
	  dp[i+1][stoi[tmp]] = true;
	}
	if(k!=7 && k!=3){
	  v.str = u.str;
	  swap(tmp[k], tmp[k+1]);
	  dp[i+1][stoi[tmp]] = true;
	}
	v.str = u.str;
	if(k<4){
	  swap(tmp[k], tmp[k+4]);
	  dp[i+1][stoi[tmp]] = true;
	}else{
	  swap(tmp[k], tmp[k-4]);
	  dp[i+1][stoi[tmp]] = true;
	}
      }
    }
  }
  return 0;
}

/*class State{
public:
  string str;
  int cnt;
};



int bfs(string str){
  State u, v;
  u.str = str;
  u.cnt = 0;
  queue<State> Q;
  Q.push(u);
  set<string> vis;
  vis.insert(u.str);
  while(!Q.empty()){
    u = Q.front(); Q.pop();
    if(u.str == "01234567") return u.cnt;
    v.cnt = u.cnt + 1;
    for(int i=0;i<8;i++){
      if(u.str[i] != '0') continue;
      if(i!=0 && i!=4){
	v.str = u.str;
	swap(v.str[i], v.str[i-1]);
	if(vis.find(v.str)==vis.end()){
	  vis.insert(v.str);
	  Q.push(v);
	}
      }
      if(i!=7 && i!=3){
	v.str = u.str;
	swap(v.str[i], v.str[i+1]);
	if(vis.find(v.str)==vis.end()){
	  vis.insert(v.str);
	  Q.push(v);
	}
      }
      v.str = u.str;
      if(i<4){
	swap(v.str[i], v.str[i+4]);
	if(vis.find(v.str)==vis.end()){
	  vis.insert(v.str);
	  Q.push(v);
	}
      }else{
	swap(v.str[i], v.str[i-4]);
	if(vis.find(v.str)==vis.end()){
	  vis.insert(v.str);
	  Q.push(v);
	}
      }
    }
  }
  return 0;
  }*/

main(){
  string str = "01234567";
  int cnt = 0;
  do{
    itos[0] = str;
    stoi[str] = cnt++;
  }while(next_permutation(str.begin(), str.end()));
  while(cin >> str){
    char c;
    for(int i=0;i<7;i++){
      cin >> c;
      str += c;
    }
    //cout << bfs(str) << endl;
    cout << solve(str) << endl;
  }
  return 0;
}

Compiler output:
a.cc: In function 'int solve(std::string)':
a.cc:14:9: error: reference to 'stoi' is ambiguous
   14 |   dp[0][stoi[str]] = true;
      |         ^~~~
In file included from /usr/include/c++/14/string:54,
                 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/basic_string.h:4432:3: note: candidates are: 'int std::__cxx11::stoi(const std::wstring&, std::size_t*, int)'
 4432 |   stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
      |   ^~~~
/usr/include/c++/14/bits/basic_string.h:4164:3: note:                 'int std::__cxx11::stoi(const std::string&, std::size_t*, int)'
 4164 |   stoi(const string& __str, size_t* __idx = 0, int __base = 10)
      |   ^~~~
a.cc:9:18: note:                 'std::map<std::__cxx11::basic_string<char>, int> stoi'
    9 | map<string, int> stoi;
      |                  ^~~~
a.cc:26:19: error: reference to 'stoi' is ambiguous
   26 |           dp[i+1][stoi[tmp]] = true;
      |                   ^~~~
/usr/include/c++/14/bits/basic_string.h:4432:3: note: candidates are: 'int std::__cxx11::stoi(const std::wstring&, std::size_t*, int)'
 4432 |   stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
      |   ^~~~
/usr/include/c++/14/bits/basic_string.h:4164:3: note:                 'int std::__cxx11::stoi(const std::string&, std::size_t*, int)'
 4164 |   stoi(const string& __str, size_t* __idx = 0, int __base = 10)
      |   ^~~~
a.cc:9:18: note:                 'std::map<std::__cxx11::basic_string<char>, int> stoi'
    9 | map<string, int> stoi;
      |                  ^~~~
a.cc:29:11: error: 'v' was not declared in this scope
   29 |           v.str = u.str;
      |           ^
a.cc:29:19: error: 'u' was not declared in this scope
   29 |           v.str = u.str;
      |                   ^
a.cc:31:19: error: reference to 'stoi' is ambiguous
   31 |           dp[i+1][stoi[tmp]] = true;
      |                   ^~~~
/usr/include/c++/14/bits/basic_string.h:4432:3: note: candidates are: 'int std::__cxx11::stoi(const std::wstring&, std::size_t*, int)'
 4432 |   stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
      |   ^~~~
/usr/include/c++/14/bits/basic_string.h:4164:3: note:                 'int std::__cxx11::stoi(const std::string&, std::size_t*, int)'
 4164 |   stoi(const string& __str, size_t* __idx = 0, int __base = 10)
      |   ^~~~
a.cc:9:18: note:                 'std::map<std::__cxx11::basic_string<char>, int> stoi'
    9 | map<string, int> stoi;
      |                  ^~~~
a.cc:33:9: error: 'v' was not declared in this scope
   33 |         v.str = u.str;
      |         ^
a.cc:33:17: error: 'u' was not declared in this scope
   33 |         v.str = u.str;
      |                 ^
a.cc:36:19: error: reference to 'stoi' is ambiguous
   36 |           dp[i+1][stoi[tmp]] = true;
      |                   ^~~~
/usr/include/c++/14/bits/basic_string.h:4432:3: note: candidates are: 'int std::__cxx11::stoi(const std::wstring&, std::size_t*, int)'
 4432 |   stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
      |   ^~~~
/usr/include/c++/14/bits/basic_string.h:4164:3: note:                 'int std::__cxx11::stoi(const std::string&, std::size_t*, int)'
 4164 |   stoi(const string& __str, size_t* __idx = 0, int __base = 10)
      |   ^~~~
a.cc:9:18: note:                 'std::map<std::__cxx11::basic_string<char>, int> stoi'
    9 | map<string, int> stoi;
      |                  ^~~~
a.cc:39:19: error: reference to 'stoi' is ambiguous
   39 |           dp[i+1][stoi[tmp]] = true;
      |                   ^~~~
/usr/include/c++/14/bits/basic_string.h:4432:3: note: candidates are: 'int std::__cxx11::stoi(const std::wstring&, std::size_t*, int)'
 4432 |   stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
      |   ^~~~
/usr/include/c++/14/bits/basic_string.h:4164:3: note:                 'int std::__cxx11::stoi(const std::string&, std::size_t*, int)'
 4164 |   stoi(const string& __str, size_t* __idx = 0, int __base = 10)
      |   ^~~~
a.cc:9:18: note:                 'std::map<std::__cxx11::basic_string<char>, int> stoi'
    9 | map<string, int> stoi;
      |                  ^~~~
a.cc: At global scope:
a.cc:104:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  104 | main(){
      | ^~~~
a.cc: In function 'int main()':
a.cc:109:5: error: reference to 'stoi' is ambiguous
  109 |     stoi[str] = cnt++;
      |     ^~~~
/usr/include/c++/14/bits/basic_string.h:4432:3: note: candidates are: 'int std::__cxx11::stoi(const std::wstring&, std::size_t*, int)'
 4432 |   stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
      |   ^~~~
/usr/include/c++/14/bits/basic_string.h:4164:3: note:                 'int std::__cxx11::stoi(const std::string&, std::size_t*, int)'
 4164 |   stoi(const string& __str, size_t* __idx = 0, int __base = 10)
      |   ^~~~
a.cc:9:18: note:                 'std::map<std::__cxx11::basic_string<char>, int> stoi'
    9 | map<string, int> stoi;
      |                  ^~~~
a.cc:110:10: error: 'next_permutation' was not declared in this scope
  110 |   }while(next_permutation(str.begin(), str.end()));
      |          ^~~~~~~~~~~~~~~~