多源BFS
AcWing 173. 矩阵距离
//这题的题意有点难理解,实际场景就是找到每一个点距离他最近的商店、
#include <iostream>
#include <cstring>
using namespace std;
typedef pair<int, int> PII;
#define x first
#define y second
const int N = 1e3 + 10;
int dist[N][N];
char g[N][N]; //题目给的输入是一个字符串
PII q[N * N];
int n, m;
void bfs()
{
memset(dist, -1, sizeof dist);
int hh = 0, tt = -1;
for (int i = 1; i <=n; i ++ )
for (int j = 1; j <= m; j ++ )
{
if (g[i][j] == '1')
{
q[++ tt] = {i, j};
dist[i][j] = 0;//自己到自己的最短距离为0;
}
}
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
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 < 1 || a > n || b < 1 || b > m) continue;
if (dist[a][b] != -1) continue;//相当于一个st数组判断是否入队过
q[++ tt] = {a, b};
dist[a][b] = dist[t.x][t.y] + 1;
}
}
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i ++ ) scanf("%s", g[i] + 1);
//for 里面加1代表行从1开始,g[i] + 1代表列下标从1开始
//题目给的矩阵在输入的时候没有空格,并且给的公式中A下标要求从1开始
bfs();
for (int i = 1; i <= n; i ++ )
{
for (int j = 1; j <= m; j ++ )
cout << dist[i][j] << " ";
cout << endl;
}
return 0;
}
最小步数模型
AcWing 1107. 魔板
很复杂的move写法
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <queue>
using namespace std;
char g[2][4];
unordered_map<string, pair<char, string>>pre;//存下来每个state由那个state的哪个操作转移过来的
unordered_map<string, int>dist ;//存下来转移到每个状态需要多少步
queue<string> q;
//三种变换
void set(string state)
{
for (int i = 0; i < 4; i ++ ) g[0][i] = state[i];
for (int i = 0, j = 7; i < 4; i ++, j -- ) g[1][i] = state[j];
}
string get()
{
string res;
for (int i = 0; i < 4; i ++ ) res += g[0][i];
for (int i = 3; i >= 0; i -- ) res += g[1][i];
return res;
}
string move0(string start)
{
set(start);
for (int i = 0; i < 4; i ++ ) swap(g[0][i], g[1][i]);
return get();
}
// string move1(string start)
// {
// set(start);
// for (int i = 0; i < 2; i ++ ) swap(g[i][0], g[i][3]);
// return get();
// }
string move1(string state)
{
set(state);
int v0 = g[0][3], v1 = g[1][3];
for (int i = 3; i > 0; i -- )
{
g[0][i] = g[0][i - 1];
g[1][i] = g[1][i - 1];
}
g[0][0] = v0, g[1][0] = v1;
return get();
}
string move2(string start)
{
set(start);
int t = g[0][1];
g[0][1] = g[1][1];
g[1][1] = g[1][2];
g[1][2] = g[0][2];
g[0][2] = t;
return get();
}
int bfs(string start, string end)
{
if (start == end) return 0;//剪枝优化
q.push(start);
dist[start] = 0;
while (!q.empty())
{
string t = q.front();
q.pop();
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]))
{
pre[m[i]] = {'A' + i, t};
dist[m[i]] = dist[t] + 1;
q.push(m[i]);
if (m[i] == end) return dist[m[i]];
}
}
}
return -1;
}
int main()
{
int x;
string start, end;
start = "12345678";
for (int i = 0; i < 8; i ++ )
{
cin >> x;
end += char(x + '0');
}
//找出最小步数
cout << bfs(start, end) << endl;
string change;
while(end != start)
{
change += pre[end].first;
end = pre[end].second;
}
reverse(change.begin(), change.end());
cout << change;
//找出最小步数对应的操作序列
return 0;
}
简单的move写法
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <queue>
using namespace std;
char g[2][4];
unordered_map<string, pair<char, string>>pre;//存下来每个state由那个state的哪个操作转移过来的
unordered_map<string, int>dist ;//存下来转移到每个状态需要多少步
queue<string> q;
//三种变换
string move(string state, int k)
{
string op[] = {"87654321", "41236785", "17245368"};
string res;
for (int i = 0; i < 8; i ++ )
res += state[op[k][i] - '1'];
return res;
}
int bfs(string start, string end)
{
if (start == end) return 0;//剪枝优化
q.push(start);
dist[start] = 0;
while (!q.empty())
{
string t = q.front();
q.pop();
for (int i = 0; i < 3; i ++ )
{
string s = move(t, i);
if (!dist.count(s))
{
pre[s] = {'A' + i, t};
dist[s] = dist[t] + 1;
q.push(s);
if (s == end) return dist[s];
}
}
}
return -1;
}
int main()
{
int x;
string start, end;
start = "12345678";
for (int i = 0; i < 8; i ++ )
{
cin >> x;
end += char(x + '0');
}
//找出最小步数
cout << bfs(start, end) << endl;
string change;
while(end != start)
{
change += pre[end].first;
end = pre[end].second;
}
reverse(change.begin(), change.end());
cout << change;
//找出最小步数对应的操作序列
return 0;
}
双端队列广搜
双端队列主要是解决边权只有 0 和 1 的这类问题,我们把边权为 0 的边加入到对头,把边权为 1 的边加入到队尾,同堆优化版的 Dijkstra 一样,只有在出队的时候才能知道最小值
AcWing 175. 电路维修
#include <iostream>
#include <cstring>
#include <deque>
using namespace std;
typedef pair<int, int> PII;
#define x first
#define y second
const int N = 510;
char g[N][N];
deque<PII> q;
int dist[N][N];
bool st[N][N];
int T, n, m;
int bfs()
{
int dx[4] = {-1, -1, 1, 1}, dy[4] = {-1, 1, 1, -1};//遍历每个点,注意点的横坐标和纵坐标一共有n + 1,m + 1
int ix[4] = {-1, -1, 0, 0}, iy[4] = {-1, 0, 0, -1};//遍历每个格子里面的字符,一共n * m个
char cs[5] = {"\\/\\/"};
memset(dist, 0x3f, sizeof dist);
dist[0][0] = 0;//初始化起点距离
memset(st, 0, sizeof st);
q.push_front({0, 0});
while (q.size())
{
PII t = q.front();
q.pop_front();
int x = t.x, y = t.y;
if (st[x][y]) continue;//每个点出队的时候距离起点才是最短的,它之前在队列里面的时候 就会不停的被出队的点松弛
//和disktra一样,每个点只用松弛他周围的边一次就行
//不加这句的话理论上时间复杂度会高一些,但是实测只高了1ms
//应该是虽然会重复判断,但是不会重复给dist[a][b]赋值,因为在它第一次出队的时候已经将它周围的点到起点的距离弛了一遍了
st[x][y] = true;
for (int i = 0; i < 4; i ++ )//bfs松弛这个点周围的点到起点的距离
{
int da = x + dx[i], db = y + dy[i];//可到达的点
int ia = x + ix[i], ib = y + iy[i];//与可到达的点之间的格子里面的电线状态
if (da < 0 || da > n || db < 0 || db > m) continue;
int d = dist[x][y] + (g[ia][ib] != cs[i]);//!=代表权值为1,=代表权值为0
if (dist[da][db] > d)
{
dist [da][db] = d;
//形成一个两段式的队列,前面是0后面是1,保证先遍历短的,相当于手动维护了一个(优先队列)堆,
//实际上本题就是bfs + 两段式优先队列版本的djkstra
if (g[ia][ib] != cs[i]) q.push_back({da, db});//权值为1放在队尾
else q.push_front({da, db});//权值为0放在队首
}
}
}
return dist[n][m];
}
int main()
{
cin >> T;
while (T -- )
{
cin >> n >> m;
for (int i = 0; i < n; i ++ )
cin >> g[i];
int res = bfs();
if (res == 0x3f3f3f3f) puts("NO SOLUTION");
else printf("%d\n", res);
}
return 0;
}