多源BFS
矩阵距离(多源BFS)
#include <cstring>
#include <iostream>
#include <algorithm>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 1010, M = N * N;
int n, m;
char g[N][N];
PII q[M];
int dist[N][N];
void bfs()
{
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int hh = 0, tt = -1;
memset(dist, -1, sizeof dist);
for (int i = 0; i < n; i ++ )
for (int j = 0; j < m; j ++ )
if (g[i][j] == '1')
{
dist[i][j] = 0;
q[++ tt ] = {i, j};
}
while (hh <= tt)
{
PII t = q[hh ++ ];
for (int i = 0; i < 4; i ++ )
{
int a = t.x + dx[i], b = t.y + dy[i];
if (a < 0 || a >= n || b < 0 || b >= m) continue;
if (dist[a][b] != -1) continue;
q[ ++ tt] = {a, b};
dist[a][b] = dist[t.x][t.y] + 1;
}
}
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i ++ )
scanf("%s", g[i]);
bfs();
for (int i = 0; i < n; i ++ )
{
for (int j = 0; j < m; j ++ )
{
cout << dist[i][j] << ' ';
}
puts("");
}
return 0;
}
魔板(最小步数)
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <queue>
using namespace std;
unordered_map<string, int> dist;
unordered_map<string, pair<char, string>> pre;
string start, ed, res;
string move0(string t)
{
for (int i = 0; i < 4; i ++ )
{
swap(t[i], t[7 - i]);
}
return t;
}
string move1(string t)
{
for (int i = 0; i < 3; i ++ ) swap(t[3], t[i]);
for (int i = 4; i < 7; i ++ ) swap(t[i], t[i + 1]);
return t;
}
string move2(string t)
{
swap(t[1], t[2]), swap(t[5], t[6]), swap(t[1], t[5]);
return t;
}
void bfs()
{
queue<string> q;
q.push(start);
dist[start] = 0;
while (q.size())
{
string t = q.front();
q.pop();
if (t == ed) return ;
string m[3];
m[0] = move0(t);
m[1] = move1(t);
m[2] = move2(t);
for (int i = 0; i < 3; i ++ )
{
if (dist.count(m[i]) == 0)
{
dist[m[i]] = dist[t] + 1;
pre[m[i]] = {'A' + i, t};
q.push(m[i]);
}
}
}
}
int main()
{
for (int i = 0; i < 8; i ++ )
start += char(i + '1');
for (int i = 0; i < 8; i ++ )
{
int x;
cin >> x;
ed += char('0' + x);
}
bfs();
cout << dist[ed] << endl;
if (dist[ed])
{
while (ed != start)
{
res += pre[ed].first;
ed = pre[ed].second;
}
reverse(res.begin(), res.end());
cout << res << endl;
}
return 0;
}
电路维修
#include <iostream>
#include <cstring>
#include <algorithm>
#include <deque>
using namespace std;
const int N = 510;
#define x first
#define y second
typedef pair<int, int> PII;
int n, m;
char g[N][N];
int dist[N][N];
bool st[N][N];
int bfs()
{
memset(dist, 0x3f, sizeof dist);
memset(st, 0, sizeof st);
dist[0][0] = 0;
deque<PII> q;
q.push_back({0, 0});
char cs[] = "\\/\\/";
int dx[4] = {-1, -1, 1, 1}, dy[4] = {-1, 1, 1, -1};
int ix[4] = {-1, -1, 0, 0}, iy[4] = {-1, 0, 0, -1};
while (q.size())
{
PII t = q.front();
q.pop_front();
if (st[t.x][t.y]) continue;
st[t.x][t.y] = true;
for (int i = 0; i < 4; i ++ )
{
int a = t.x + dx[i], b = t.y + dy[i];
if (a < 0 || a > n || b < 0 || b > m) continue;
int ca = t.x + ix[i], cb = t.y + iy[i];
int d = dist[t.x][t.y] + (g[ca][cb] != cs[i]);
if (d < dist[a][b])
{
dist[a][b] = d;
if (g[ca][cb] != cs[i]) q.push_back({a, b});
else q.push_front({a, b});
}
}
}
return dist[n][m];
}
int main()
{
int T;
scanf("%d", &T);
while (T -- )
{
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i ++ ) scanf("%s", g[i]);
int t = bfs();
if (t == 0x3f3f3f3f) puts("NO SOLUTION");
else printf("%d\n", t);
}
return 0;
}
字符串变换
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <unordered_map>
using namespace std;
const int N = 25;
int n;
string a[N], b[N];
int extend(queue<string>& q, unordered_map<string, int>& da, unordered_map<string, int> &db, string a[], string b[])
{
string t = q.front();
q.pop();
for (int i = 0; i < t.size(); i ++ )
{
for (int j = 0; j < n; j ++ )
{
if (t.substr(i, a[j].size()) == a[j])
{
string state = t.substr(0, i) + b[j] + t.substr(i + a[j].size());
if (db.count(state)) return da[t] + 1 + db[state];
if (da.count(state)) continue;
da[state] = da[t] + 1;
q.push(state);
}
}
}
return 11;
}
int bfs(string A, string B)
{
if (A == B) return 0;
queue<string> qa, qb;
unordered_map<string, int> da, db;
da[A] = 0, db[B] = 0;
qa.push(A);
qb.push(B);
while (qa.size() && qb.size())
{
int t;
if (qa.size() < qb.size()) t = extend(qa, da, db, a, b);
else t = extend(qb, db, da, b, a);
if (t <= 10) return t;
}
return 11;
}
int main()
{
string A, B;
cin >> A >> B;
while (cin >> a[n] >> b[n]) n ++ ;
int step = bfs(A, B);
if (step > 10) puts("NO ANSWER!");
else printf("%d\n", step);
return 0;
}
A*算法
八数码
八数码有解的充要条件是逆序对的数量是偶数,每一次移动会同时改变两个数的顺序
估价函数:当前状态中每个数的位置与目标位置的曼哈顿距离只和
基础BFS版
在这里插入代码片
A*算法
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <unordered_map>
using namespace std;
#define x first
#define y second
typedef pair<int, string> PIS;
int f(string str)
{
int res = 0;
for (int i = 0; i < str.size(); i ++ )
{
if (str[i] != 'x')
{
int t = str[i] - '1';
res += abs(i / 3 - t / 3) + abs(i % 3 - t % 3);
}
}
return res;
}
string bfs(string start)
{
string end = "12345678x";
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
char op[] = "urdl";
unordered_map<string, int> dist;
unordered_map<string, pair<string, char>> prev;
priority_queue<PIS, vector<PIS>, greater<PIS>> heap;
heap.push({f(start), start});
dist[start] = 0;
while (heap.size())
{
PIS t = heap.top();
heap.pop();
string state = t.y;
// cout << state << endl;
if (state == end) break;
int d = dist[state];
int p = state.find('x');
int x = p / 3, y = p % 3;
string source = state;
for (int i = 0; i < 4; i ++ )
{
int a = x + dx[i], b = y + dy[i];
if (a >= 0 && a < 3 && b >= 0 && b < 3)
{
swap(state[x * 3 + y], state[a * 3 + b]);
if (dist.count(state) == 0 || dist[state] > d + 1)
{
dist[state] = d + 1;
prev[state] = {source, op[i]};
heap.push({dist[state] + f(state), state});
}
swap(state[x * 3 + y], state[a * 3 + b]);
}
}
}
string res;
while (end != start)
{
res += prev[end].y;
end = prev[end].x;
}
reverse(res.begin(), res.end());
return res;
}
int main()
{
string c, str, seq;
while (cin >> c)
{
str += c;
if (c != "x") seq += c;
}
int cnt = 0;
for (int i = 0; i < 8; i ++ )
for (int j = i; j < 8; j ++ )
if (seq[i] > seq[j])
cnt ++ ;
if (cnt & 1) puts("unsolvable");
else cout << bfs(str) << endl;
return 0;
}