当前位置:网站首页>[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; }
边栏推荐
- ELS compatibility DC, transfer pictures to window
- Interview must ask | what stages does a thread go through from creation to extinction?
- Grid layout
- [C language] dynamic memory management
- RN development series < 9 > --mobx (1) Introduction
- Scala immutable map, variable map, map conversion to other data types
- Affine transformation module and conditional batch Standardization (CBN) of detailed text generated images
- 如何重置Photoshop首选项?ps重置首选项的方法
- Shift right of negative numbers
- 【独立站建设】跨境电商出海开网店,首选这个网站建设!
猜你喜欢

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

Basic configuration of static routing to achieve network wide accessibility

CDH集群集成外部Flink(改进版-与时俱进)

Summary of fire safety training materials

Find a specific number in an ordered array

iPhone13再降价,其实只是做做样子,消费者都在等iPhone14

Use unity to build a WordArt system

单元测试chapter6

【C语言】自定义类型详解(结构体+枚举+联合)

Yolov4 network details
随机推荐
博云容器云、DevOps 平台斩获可信云“技术最佳实践奖”
Redis interview question (2022)
Plane conversion (displacement, rotation, scaling)
在有序数组找具体某个数字
JS tips
Ffmpeg merge video function
strlen和sizeof的区别
MySQL下载安装 & 完美卸载
R-score reproduction R-Precision evaluation index quantitative text generation image r-score quantitative experiment whole process reproduction (R-Precision) quantitative evaluation experiment step on
Ref Hook
UUID and indexing rules
[C language] detailed explanation of user-defined types (structure + enumeration + Union)
打开编程的大门
Visualization domain svg
Basic configuration of static routing to achieve network wide accessibility
JS day 2 (variables, variable usage, naming rules, syntax extensions)
Comprehensive experiment of static routing
5. Display of component dynamic components
Solution to the third game of 2022 Hangzhou Electric Multi school league
C语言 通讯录管理系统(链表,手机号码分段存储,txt文件存取,完整源码)


