bfs问题
同类型的有845八数码、3156卡牌换位
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <unordered_map>
using namespace std;
string start,endss;
int len;
int bfs(){
queue<string> Q;
Q.push(start);
unordered_map <string,int>dist;
dist[start] = 0;
while(!Q.empty()){
auto t = Q.front();
Q.pop();
int distance = dist[t];
if(t == endss){
return distance;
}
int dx[6] = {1,-1,2,-2,3,-3};
//开始移动;
int pos = t.find('*');
for(int i = 0;i < 6;i++){
int nx = pos + dx[i];
if(nx >= 0 && nx < len){
swap(t[pos],t[nx]);
if(dist.count(t) == 0){
dist[t] = distance + 1;
Q.push(t);
}
swap(t[pos],t[nx]);
}
}
}
return -1;
}
int main(){
getline(cin,start);
getline(cin,endss);
len = start.length();
cout << bfs() << endl;
return 0;
}
unordered_map [HTML_REMOVED]dist;请问这个表达有没有通俗一点的解释,我查半天没太明白它的意思😔
以string为下标的数组的值