题解:P10578 [蓝桥杯 2024 国 A] 旋转九宫格
题意
给定一个
问最少需要几步才能将给定的状态旋转为
1 2 3
4 5 6
7 8 9
思路
考虑 bfs。
状态存一个长度为 vector
存一个二维数组 左上)、右上)、左下) 还是 右下) 的
拿右上举例,原来的位置为
1 2 3
4 5 6
7 8 9
顺时针旋转之后位置变成了
1 5 2
4 6 3
7 8 9
这时只需要在开一个 vector
AC代码
#include <bits/stdc++.h>
using namespace std;
vector<int> s, e;
map<vector<int>, int> dis;
int r[4][9] = {{3, 0, 2, 4, 1, 5, 6, 7, 8}, {0, 4, 1, 3, 5, 2, 6, 7, 8}, {0, 1, 2, 6, 3, 5, 7, 4, 8}, {0, 1, 2, 3, 7, 4, 6, 8, 5}};
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
for(int i = 1; i <= 9; i++){
s.emplace_back(i);
}
while(t--){
e.clear();
for(int i = 1; i <= 9; i++){
int x;
cin >> x;
e.emplace_back(x);
}
queue<vector<int>> q;
q.push(s);
dis[s] = 1;
while(!q.empty()){
vector<int> x = q.front();
q.pop();
for(int i = 0; i < 4; i++){
vector<int> t(9);
for(int j = 0; j < 9; j++){
t[r[i][j]] = x[j];
}
if(!dis[t]){
dis[t] = dis[x] + 1;
q.push(t);
}
}
}
cout << dis[e] - 1 << '\n';
}
return 0;
}