当前位置:网站首页>[search] - multi source BFS + minimum step model
[search] - multi source BFS + minimum step model
2022-07-27 04:57:00 【Xuanche_】

AcWing 173. Matrix distance ( Multi-source BFS)
![]()
sample input :
3 4 0001 0011 0110sample output :
3 2 1 0 2 1 0 0 1 0 0 1
stay Multi source shortest path In the question of , If we use the single source shortest path at each point , There is a high probability of timeout
So we need to use a “ Super source ” thought

Build a Virtual source point , Connect all the starting points and this virtual source point to an edge as 0 The edge of , And then unfold Single source shortest path

#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. Magic board ( Minimum step model )
sample input :
2 6 8 4 5 7 3 1sample output :
7 BCABCCB
This is a Regard the whole chessboard as a state , Realize the transition between states through different operations
Store a status , We use Hashifa
<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. Circuit maintenance ( Double ended queue wide search )
sample input :
1 3 5 \\/\\ \\/// /\\\\sample output :
1
#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; }
边栏推荐
- Cloudcompare & PCL match point distance suppression
- Qstring conversion char*
- strlen和sizeof的区别
- 多态的详讲
- State Hook
- Summary of fire safety training materials
- Technology sharing | gtid that needs to be configured carefully_ mode
- 0 dynamic programming medium leetcode467. The only substring in the surrounding string
- 【HCIP】重发布、重分布、重分发实验
- Shift right of negative numbers
猜你喜欢

Chapter 4 scope and life cycle of bean object

STL upper series - detailed explanation of list container

【C语言】动态内存管理

Photoshop提示暂存盘已满怎么办?ps暂存盘已满如何解决?

【搜索】DFS之连通性模型 + 搜索顺序

Final Cut Pro中文教程 (1) 基础认识Final Cut Pro

Structural mode - adapter mode
![[hcip] redistribute, redistribute and redistribute experiments](/img/53/18e1a589a85b508675c97a0108aee6.png)
[hcip] redistribute, redistribute and redistribute experiments

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

Photoshop裁剪工具隐藏技巧
随机推荐
Redis interview question (2022)
Grid layout
Knowledge about hash index and b+ tree
多态的详讲
【搜索】—— 多源BFS + 最小步数模型
[hcip] redistribute, redistribute and redistribute experiments
State Hook
5. Display of component dynamic components
Bubble sort (detailed)
【搜索】双向广搜 + A*
消防安全培训资料汇总
0动态规划中等 LeetCode467. 环绕字符串中唯一的子字符串
第4章 Bean对象的作用域以及生命周期
Open the door of programming
二叉搜索树详讲
Dynamic routing configuration
Pinia uses the whole process, including state, actions, getters, and how to deconstruct, respond, and various methods used by actions
2019强网杯upload
打开编程的大门
C language - two dimensional array, pointer


