#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int N = 105;
int p[N][N], d[N][N];
int n, m;
queue<pair<int,int>> que;
int bfs(){
memset(d, -1, sizeof d);
d[0][0] = 0;
int a[4] = {-1, 0, 1, 0}, b[4] = {0, -1, 0, 1};
while (!que.empty()) {
auto t = que.front();
que.pop();
for (int i = 0; i < 4; i++){
int x = t.first + a[i], y = t.second + b[i];
if (x >= 0 && x < n && y >= 0 && y < m && d[x][y] == -1&& p[x][y] == 0){
que.push({x, y});
d[x][y] = d[t.first][t.second] + 1;
}
}
}
return d[n-1][m-1];
}
int main(){
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i ++) for (int j = 0; j < m; j ++) scanf("%d", &p[i][j]);
que.push(make_pair(0,0));
cout << bfs();
}
八数码
经典问题,拼图问题。重点是在对公共变量修改后要返回状态。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <unordered_set>
using namespace std;
queue<pair<pair<int, int>, string>> que;
unordered_set<string> seen;
string ggg = "12345678x";
int bfs(){
int cx[4] = {-1, 0, 1, 0}, cy[4] = {0, 1, 0, -1};
while (que.size()){
auto a = que.front();
que.pop();
int x = a.first.first % 3, y = a.first.first / 3;
int res = a.first.second;
string s = a.second;
seen.insert(s);
if (s == ggg) return res;
for (int i = 0; i < 4; i ++){
int dx = x + cx[i], dy = y + cy[i];
if (dx >= 0 && dx < 3 && dy >= 0 && dy < 3){
int b = dy * 3 + dx;
swap(s[a.first.first], s[b]);
if (seen.find(s) == seen.end()){
res ++;
que.push({{b, res}, s});
res --;
}
swap(s[a.first.first], s[b]);
}
}
}
return -1;
}
int main(){
string s;
getline(cin, s);
string c;
for (int i = 0; i < s.size(); i ++){
if (s[i] != ' ') c.push_back(s[i]);
}
que.push({{c.find('x'), 0}, c});
cout << bfs();
}