P1379 八数码难题
C++#include <bits/stdc++.h>
using namespace std;
// 目标状态字符串:123804765
string ss = "123804765";
// 定义结构体 node,用于 BFS 中存储每个状态的信息
struct node {
int x; // 空格('0')在当前字符串中的位置索引(0~8)
int c; // 当前已经移动的步数
string s; // 当前的状态字符串
};
string s; // 初始状态字符串
queue<node> q; // BFS 所需的队列
int ans; // 存储最终答案(最小移动次数)
map<string, bool> mp; // 用于记录是否访问过某个状态,避免重复搜索
// 广度优先搜索函数
void bfs() {
mp[s] = 1; // 标记初始状态为已访问
while (q.size()) { // 队列非空时继续搜索
node f = q.front(); // 取出队首节点
q.pop();
if (f.s == ss) { // 如果当前状态等于目标状态
cout << f.c; // 输出所需步数
break;
}
string xs = f.s; // 复制当前状态字符串用于修改
// 向左移动:当空格不在最左列(索引不能是 0,3,6)
if (f.x != 0 && f.x != 3 && f.x != 6) {
swap(xs[f.x - 1], xs[f.x]); // 将空格与左边元素交换
if (mp[xs] == 0) { // 若该状态未被访问过
q.push({f.x - 1, f.c + 1, xs}); // 入队新状态
mp[xs] = 1; // 标记为已访问
}
}
xs = f.s; // 恢复原状态以便尝试其他方向
// 向右移动:当空格不在最右列(索引不能是 2,5,8)
if (f.x != 2 && f.x != 5 && f.x != 8) {
swap(xs[f.x + 1], xs[f.x]);
if (mp[xs] == 0) {
q.push({f.x + 1, f.c + 1, xs});
mp[xs] = 1;
}
}
xs = f.s;
// 向下移动:当空格不在最后一行(索引不能是 6,7,8)
if (f.x != 8 && f.x != 7 && f.x != 6) {
swap(xs[f.x + 3], xs[f.x]); // 行号+1 对应索引+3
if (mp[xs] == 0) {
q.push({f.x + 3, f.c + 1, xs});
mp[xs] = 1;
}
}
xs = f.s;
// 向上移动:当空格不在第一行(索引不能是 0,1,2)
if (f.x != 0 && f.x != 1 && f.x != 2) {
swap(xs[f.x - 3], xs[f.x]);
if (mp[xs] == 0) {
q.push({f.x - 3, f.c + 1, xs});
mp[xs] = 1;
}
}
}
return;
}
// 主函数入口
int main() {
cin >> s; // 输入初始状态字符串
// 查找初始状态中空格('0')的位置
for (int i = 0; i < s.size(); i++) {
if (s[i] == '0') {
q.push({i, 0, s}); // 将初始状态入队
break;
}
}
bfs(); // 开始广度优先搜索
return 0;
}
P2730 [USACO3.2] 魔板 Magic Squares
C++#include <bits/stdc++.h>
using namespace std;
int main() {
string ed;
char c[8];
bool b=0;
for (int i = 0; i < 8; ++i) {
cin >> c[i];
if(c[i]!=i+1+'0'){
b=1;
}
ed += c[i];
}
if(b==0){
cout<<0;
}
queue<string> q;
map<string, string> mp;
q.push("12345678");
mp["12345678"] = "";
while (!q.empty()) {
string s = q.front(); q.pop();
{
string x = s;
for (int i = 0; i < 4; ++i)
swap(x[i], x[7 - i]);
if (mp.count(x) == 0) {
mp[x] = mp[s] + 'A';
q.push(x);
if (x == ed) {
cout << mp[x].size() << endl << mp[x];
return 0;
}
}
}
{
string x = s;
x[0] = s[3]; x[1] = s[0]; x[2] = s[1]; x[3] = s[2];
x[4] = s[5]; x[5] = s[6]; x[6] = s[7]; x[7] = s[4];
if (mp.count(x) == 0) {
mp[x] = mp[s] + 'B';
q.push(x);
if (x == ed) {
cout << mp[x].size() << endl << mp[x];
return 0;
}
}
}
{
string x = s;
x[1] = s[6]; x[2] = s[1]; x[5] = s[2]; x[6] = s[5];
if (mp.count(x) == 0) {
mp[x] = mp[s] + 'C';
q.push(x);
if (x == ed) {
cout << mp[x].size() << endl << mp[x];
return 0;
}
}
}
}
return 0;
}
P1699 [USACO19OPEN] Bucket Brigade B
C++#include<bits/stdc++.h>
using namespace std;
int n = 10;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
char c[15][15];
string s[15];
bool vis[15][15];
struct node{
int x;
int y;
int step;
};
queue<node> q;
void BFS(){
while(!q.empty()){
int x = q.front().x;
int y = q.front().y;
int step = q.front().step;
q.pop();
if(c[x][y]=='B'){
cout << step;
return;
}
vis[x][y] = 1;
for (int i = 0; i < 4;i++){
int curx = x + dx[i];
int cury = y + dy[i];
if(c[curx][cury]!='R'&&vis[curx][cury]==0&&curx>=1&&cury>=1&&curx<=n&&cury<=n){
q.push(node{curx, cury, step + 1});
}
}
}
}
int main(){
for (int i = 1; i <= n;i++){
cin >> s[i];
}
for (int i = 1; i <= n;i++){
for (int j = 1; j <= n;j++){
c[i][j] = s[i][j - 1];
if(c[i][j]=='L'){
q.push(node{i, j, -1});
}
}
}
BFS();
return 0;
}
D3443 【提高】走出迷宫的最短路径
C++#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int mn = 155;
int n, m;
int g[mn][mn];
int a, b, c, d;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
struct nd {
int x;
int y;
int p[mn * mn][2];
int l;
};
bool v[mn][mn];
void e() {
queue<nd> q;
nd s;
s.x = a;
s.y = b;
s.l = 1;
s.p[0][0] = a;
s.p[0][1] = b;
v[a][b] = true;
q.push(s);
while (!q.empty()) {
nd f = q.front();
q.pop();
int x = f.x;
int y = f.y;
if (x == c && y == d) {
for (int i = 0; i < f.l; ++i) {
if (i != 0) cout << "->";
cout << "(" << f.p[i][0] + 1 << "," << f.p[i][1] + 1 << ")";
}
cout << endl;
return;
}
for (int i = 0; i < 4; ++i) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && nx < n && ny >= 0 && ny < m && g[nx][ny] == 0 && !v[nx][ny]) {
v[nx][ny] = true;
nd t;
t.x = nx;
t.y = ny;
t.l = f.l + 1;
for (int j = 0; j < f.l; ++j) {
t.p[j][0] = f.p[j][0];
t.p[j][1] = f.p[j][1];
}
t.p[f.l][0] = nx;
t.p[f.l][1] = ny;
q.push(t);
}
}
}
cout << "no way" << endl;
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
cin >> g[i][j];
cin >> a >> b;
cin >> c >> d;
a -= 1;
b -= 1;
c -= 1;
d -= 1;
e();
return 0;
}