当前位置:网站首页>【搜索】—— 多源BFS + 最小步数模型
【搜索】—— 多源BFS + 最小步数模型
2022-07-27 04:39:00 【玄澈_】

AcWing 173. 矩阵距离 (多源BFS)
![]()
输入样例:
3 4 0001 0011 0110输出样例:
3 2 1 0 2 1 0 0 1 0 0 1
在多源最短路的问题中,如果我们采用每个点的单源最短路,很大概率会超时
所以我们需要用到一个 “超级源点” 思想

建立一个虚拟源点,将所有的起点和这个虚拟源点连一条边为 0 的边,然后展开单源最短路

#include <cstring> #include <algorithm> #include <iostream> using namespace std; #define x first #define y second const int N = 1010, M = N * N; typedef pair<int,int> PII; int n, m; char g[N][N]; PII q[M]; int dist[N][N]; void bfs() { memset(dist, -1, sizeof dist); int hh = 0, tt = -1; 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}; } int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; while(hh <= tt) { auto 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; dist[a][b] = dist[t.x][t.y] + 1; q[++ tt] = {a, b}; } } } int main() { cin >> 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 ++ ) printf("%d ", dist[i][j]); cout << endl; } return 0; }
AcWing 1107. 魔板(最小步数模型)
输入样例:
2 6 8 4 5 7 3 1输出样例:
7 BCABCCB
这是将整个棋盘看做是一个状态,通过不同的操作来实现状态间的转移
存放一个状态,我们使用的是哈希法
<unordered_map>
#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;
unordered_map<string, int> dist;
void set(string state)
{
for (int i = 0; i < 4; i ++ ) g[0][i] = state[i];
for (int i = 7, j = 0; j < 4; i --, j ++ ) g[1][j] = state[i];
}
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 state)
{
set(state);
for (int i = 0; i < 4; i ++ ) swap(g[0][i], g[1][i]);
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 state)
{
set(state);
int v = g[0][1];
g[0][1] = g[1][1];
g[1][1] = g[1][2];
g[1][2] = g[0][2];
g[0][2] = v;
return get();
}
int bfs(string start, string end)
{
if (start == end) return 0;
queue<string> q;
q.push(start);
dist[start] = 0;
while (!q.empty())
{
auto 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]))
{
dist[m[i]] = dist[t] + 1;
pre[m[i]] = {'A' + i, t};
q.push(m[i]);
if (m[i] == end) return dist[end];
}
}
return -1;
}
int main()
{
int x;
string start, end;
for (int i = 0; i < 8; i ++ )
{
cin >> x;
end += char(x + '0');
}
for (int i = 1; i <= 8; i ++ ) start += char('0' + i);
int step = bfs(start, end);
cout << step << endl;
string res;
while (end != start)
{
res += pre[end].first;
end = pre[end].second;
}
reverse(res.begin(), res.end());
if (step > 0) cout << res << endl;
return 0;
}
AcWing 175. 电路维修 (双端队列广搜)
输入样例:
1 3 5 \\/\\ \\/// /\\\\输出样例:
1
【搜索】—— 双端队列广搜_玄澈_的博客-CSDN博客_双端队列搜索
#include<iostream> #include<deque> #include<cstring> #include<algorithm> #define x first #define y second using namespace std; typedef pair<int, int> PII; const int N = 510; int n,m; char g[N][N]; int dist[N][N];; deque<PII> q; int bfs() { memset(dist,0x3f,sizeof dist); q.push_front({0,0}); dist[0][0]=0; int dx[]={-1,-1,1,1},dy[]={-1,1,1,-1}; int ix[]={-1,-1,0,0},iy[]={-1,0,0,-1}; char s[]="\\/\\/"; while(q.size()) { PII t=q.front(); q.pop_front(); for(int i=0;i<4;i++) { int a=t.x+dx[i],b=t.y+dy[i]; int aa=t.x+ix[i],bb=t.y+iy[i]; if(a<0||a>n||b<0||b>m) continue; int d = dist[t.x][t.y]+(g[aa][bb]!=s[i]); if (d < dist[a][b]) { dist[a][b] = d; if (g[aa][bb] != s[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]); if((n+m)%2==1) { puts("NO SOLUTION"); continue; } cout<<bfs()<<endl; } return 0; }
边栏推荐
- 单元测试chapter6
- Chapter 6: cloud database
- 关于gorm的BeforeDelete钩子方法不生效的问题
- 「Photoshop2021入门教程」“拉平”带有透视感的图像
- TCP three handshakes and four disconnects
- Final Cut Pro中文教程 (1) 基础认识Final Cut Pro
- VSCode开启Pull Request更新代码分支可视化新篇章
- Cloudcompare & PCL matching point median (or standard deviation) distance suppression
- grid布局
- Bo Yun container cloud and Devops platform won the trusted cloud "technology best practice Award"
猜你喜欢
![[C language] detailed explanation of user-defined types (structure + enumeration + Union)](/img/d9/b10371159c63c126b5ff98bac0971a.png)
[C language] detailed explanation of user-defined types (structure + enumeration + Union)

Final Cut Pro中文教程 (2) 素材窗口的认识

Basic configuration of static routing to achieve network wide accessibility

Yolov4 network details

redux三大核心

TCP three handshakes and four disconnects

IP 14th day notes

.htaccess学习

Dino paper accuracy, and analyze the variant of its model structure & Detr

Solution to the third game of 2022 Hangzhou Electric Multi school league
随机推荐
Ref Hook
grid布局
「Photoshop2021入门教程」对齐与分布制作波点图案
【AtCoder Beginner Contest 260 (A·B·C)】
HCIA static routing basic simulation experiment
RN development series < 9 > --mobx (1) Introduction
Pinia入门到精通,Pinia使用全流程,包含state,actions,getters,以及如何解构,进行响应,actions使用的多种方法
Plane conversion (displacement, rotation, scaling)
Maximum value, minimum value, bubble sort in the array
2019强网杯upload
Shift right of negative numbers
Redis interview question (2022)
[C language] detailed explanation of user-defined types (structure + enumeration + Union)
Safety fourth after class exercise
双向重发布实验
[dynamic planning hundred questions strengthening plan] 11~20 (continuously updating)
Session&Cookie&token
【C语言】动态内存管理
0 dynamic programming medium leetcode467. The only substring in the surrounding string
结构型模式-适配器模式


