当前位置:网站首页>J - Borg maze (minimum spanning tree +bfs)
J - Borg maze (minimum spanning tree +bfs)
2022-06-30 15:01:00 【Rabbit doesn't like radish】

The question :
A For aliens , from S set out , Reach every A It's about , Assimilate them , After assimilation A( Can be split into many ) Can assimilate other A, How many steps does it take to assimilate all aliens ( Only up, down, left and right ),
Ideas :
use dfs Take every one. A or S The distance to other aliens is found , Then there is the problem of the shortest path
#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int N=505;
int MAX=0x3f3f3f3f;
int n,m,k;
char b[N][N],s1[N];
int map[N][N];
int biao[N][N];
int pre[N*N];
int dx[5]={
1,0,0,-1};// Four directions of traversal
int dy[5]={
0,1,-1,0};
struct node{
int u,v,w;
}a[N*(N-1)/2];
struct haha{
int x,y;
int step;// They count
}now,next;
int cmp(node x,node y)
{
return x.w<y.w;
}
int find(int x)
{
if(pre[x]==x)
return x;
else
return pre[x]=find(pre[x]);
}
int judge(int u,int v)
{
int fx=find(u);
int fy=find(v);
if(fx!=fy)
{
pre[fy]=fx;
return 1;
}
return 0;
}
void bfs(int x,int y,int ans)
{
memset(biao,0,sizeof(biao));
queue<haha>Q;
now.x=x;
now.y=y;
now.step=0;
biao[x][y]=1;
Q.push(now);// Join the queue
while(!Q.empty())
{
now=Q.front();
Q.pop();
for(int i=0;i<4;i++)
{
next.x=now.x+dx[i];
next.y=now.y+dy[i];// I feel like my brain is not working well ,now.y, It was written now.x, Speechless
next.step=now.step+1;
int x1,y1;
x1=next.x,y1=next.y;
if(x1>=0&&y1>=0&&x1<n&&y1<m&&biao[x1][y1]==0&&map[x1][y1]!=-1)
{
biao[x1][y1]=1;
Q.push(next);
if(map[x1][y1]>0)
{
a[k].u=ans;
a[k].v=map[x1][y1];
a[k++].w=next.step;
}
}
}
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
int num=1;
scanf("%d%d",&m,&n);
memset(map,0,sizeof(map));
gets(b[0]);// Clear spaces
//getchar();// Out-of-service , because m,n There may be a lot of spaces after
for(int i=0;i<n;i++)
{
gets(b[i]);// The input string with spaces cannot be used scanf,
}
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(b[i][j]==' ')
map[i][j]=0;
if(b[i][j]=='#')
map[i][j]=-1;
if(b[i][j]=='A'||b[i][j]=='S')
{
map[i][j]=num;
num++;// Statistics A,S The number of
}
}
}
k=0;// Used to calculate the number of paths
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(map[i][j]>0)
bfs(i,j,map[i][j]);
}
}
for(int i=0;i<=num;i++)//kruskal Algorithm
pre[i]=i;
sort(a,a+k,cmp);
int sum=0;
for(int i=0;i<k;i++)
{
if(judge(a[i].u,a[i].v))
{
sum+=a[i].w;
}
}
//cout<<sum<<endl;
printf("%d\n",sum);
}
return 0;
}
边栏推荐
- Win10 backup backup shows that creating a shared protection point on the shadow failed
- Solve the problem that codeblocks20.03 on win11 cannot run for the first time
- 1019 general palindromic number (20 points)
- Database connection to company database denied
- 1133: output family and friends string
- 文本匹配——【NAACL 2022】GPL
- Double pointer circular linked list
- [untitled]
- CCF image rotation (Full Score code + problem solving idea) 201503-01
- CCF date calculation (Full Score code + skill summary) February 2, 2015
猜你喜欢

CCF call auction (full mark code + problem solving ideas + skill summary) 201412 - 3
![[matlab] 2D drawing summary](/img/de/6bb5385f440a2997dbf9cbb9a756eb.jpg)
[matlab] 2D drawing summary

CCF date calculation (Full Score code + skill summary) February 2, 2015

Matlab judge palindrome number (only numbers)

CCF image rotation (Full Score code + problem solving idea) 201503-01

How to use Alibaba Vector Icon

How to get palindrome number in MATLAB (using fliplr function)

Not satisfied with markdown native code block style? Try this beautify code screenshot tool~~

Solve the problem that codeblocks20.03 on win11 cannot run for the first time

Shangpinhui knowledge points of large e-commerce projects
随机推荐
高精度CNC加工中心为什么会出现误差?这4个原因你要注意!
Analysis on the problems of irregular step hole on horizontal machining center
Win10 backup backup shows that creating a shared protection point on the shadow failed
机械工程师面试的几个问题,你能答上来几个?
1133: output family and friends string
PS tip: the video frame to Layer command cannot be completed because dynamiclink is not available
ES6 notes
分布式--OpenResty+lua+Redis
Why do high precision CNC machining centers have errors? You should pay attention to these four reasons!
PS dynamic drawing
The difference between settimeout() and setinterval()
Sorting by character frequency
Using member variables and member functions of a class
立式加工中心调试的步骤
Invalid argument during startup: Failed to open the . conf file: redis-window
Determine the number of digits of an integer in MATLAB (one line of code)
V3 02——What‘s new in Chrome extensions
Sum of squares of two pointers
Implement a long-click list pop-up box on apiccloud
Bucket sorting (C language)