会弹出
#error This file requires compiler and library support for the \
这个代码,望大佬解惑 代码:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<unordered_map>
#include<queue>
using namespace std;
string start;
int fx[] = {0 , 1 , -1 , 0 , 0};
int fy[] = {0 , 0 , 0 , 1 , -1};
int bfs(string start){
string end = "123804765";
unordered_map<string , int> dist;
queue<string> q;
q.push(start);
dist[start] = 0;
while(!q.empty()){
string k = q.front();
int distance = dist[k];
q.pop();
if(k == end) return distance;
int p = k.find('0');
int x = p / 3 , y = p % 3;
for(int i = 1;i <= 4;i++){
int newx = x + fx[i];
int newy = y + fy[i];
if(newx >= 0 && newx < 3 && newy >= 0 && newy < 3){
swap(k[p] , k[newx * 3 + newy]);
if(!dist.count(k)){
dist[k] = distance + 1;
q.push(k);
}
swap(k[p] , k[newx * 3 + newy]);
}
}
}
return -1;
}
int main(){
cin >> start;
cout << bfs(start) << endl;
return 0;
}