当前位置:网站首页>[2021] Tencent autumn recruitment technology post programming arrangement supermarket
[2021] Tencent autumn recruitment technology post programming arrangement supermarket
2022-04-22 18:03:00 【sigd】

Their thinking : Maze problems , Determine how many connected blocks contain the house . Select a point inside the connecting block to build a supermarket , Make the distance shortest .bfs Determine connected block ,bfsC Used to calculate the distance to build a supermarket at a certain point .
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int n,v[55][55],cnt,sum;
int dx[4]= {0,0,1,-1},dy[4]= {1,-1,0,0};
char a[55][55];
int bfsC(int xx,int yy) /**< Used to calculate in xx,yy When building a supermarket , The distance from other houses to this supermarket , Note that you must search , You can't find the Manhattan distance directly */
{
int i,j,x,y,qx[3000],qy[3000],f=0,r=0,v[55][55]={0},dis=0;/**< This v Will mask the global v Array */
qx[r]=xx,qy[r++]=yy;
v[xx][yy]=1;
while(f<r)
{
x=qx[f],y=qy[f++];
if(a[x][y]=='#')
dis+=v[x][y]-1;;
for(i=0; i<4; i++)
{
int nx=x+dx[i],ny=y+dy[i];
if(nx>=1&&nx<=n&&ny>=1&&ny<=n&&a[nx][ny]!='*'&&v[nx][ny]==0)
{
v[nx][ny]=v[x][y]+1;/**< Record the distance */
qx[r]=nx,qy[r++]=ny;
}
}
}
return dis;
}
void bfs(int x,int y)
{/**< qx,qy Handwriting queue , Share a head and tail pointer */
int i,j,qx[3000],qy[3000],f=0,r=0,minDis=9999999;
int house[2505][2],num=0;
qx[r]=x,qy[r++]=y;
v[x][y]=1;
while(f<r)
{
x=qx[f],y=qy[f++];
if(a[x][y]=='#') /**< Save the house , Used to calculate */
house[++num][0]=x,house[num][1]=y;
for(i=0; i<4; i++)
{
int nx=x+dx[i],ny=y+dy[i];
if(nx>=1&&nx<=n&&ny>=1&&ny<=n&&a[nx][ny]!='*'&&v[nx][ny]==0)
{
v[nx][ny]=1;
qx[r]=nx,qy[r++]=ny;
}
}
}
if(num>0)/**< Need to build a supermarket */
cnt++;
for(i=0; i<r; i++) /**< Write a line on it qx,qy Inside , The element values are still */ /**< Check on (qx[i],qy[i]) The total distance to build a supermarket , Write another deep search or wide search to do this */
minDis=min(minDis,bfsC(qx[i],qy[i]));
sum+=minDis;
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0);
int i,j;
cin>>n;
for(i=1; i<=n; i++)
cin>>a[i]+1;
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
if(a[i][j]!='*'&&v[i][j]==0)
bfs(i,j);/**< A new connected block */
cout<<cnt<<' '<<sum;
return 0;
}
版权声明
本文为[sigd]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221800580869.html
边栏推荐
猜你喜欢

Soft test high item notes | basis of project evaluation

华为路由器通过MPLS 虚拟私有网实现总部与分公司连接

【Lane】Ultra-Fast-Lane-Detection(2)自定义模型测试

Soft test high item notes | project schedule management

Soft test advanced item notes | soft skills

Design the test paper storage scheme of ten million students management system

多线程笔记 | 比对 Thread 和 Runnable

还弄不懂相对路径和绝对路径,这篇文章带你简单剖析

设计千万级学生管理系统的考试试卷存储方案

秒雲助力中電科32所發布“基於擬態應用集成框架的SaaS雲管理平臺解决方案”
随机推荐
接口测试 Mock 实战(二) | 结合 jq 完成批量化的手工 Mock
SAP ABAP FOR ALL ENTRIES 的用法
Deleted items can be recovered even after the outlook Deleted Items folder is empty
Fastjson 2 来了,性能继续提升,还能再战十年
Soft test high item notes | demand classification
软考高项笔记 | 立项管理内容
Multithreaded notes | future interface function
"Programmer's life extension guide": follow the code farmer and live 20 more years!
【Lane】Ultra-Fast-Lane-Detection(1)自定义数据集训练
How to open a file in RM format?
How do I completely delete files on my computer?
This API hub is powerful. It contains open APIs such as nailing enterprise wechat, and can be debugged directly!
广东水泥数据概况
Design the test paper storage scheme of ten million students management system
电脑硬件都有哪些?
软考高项笔记 | 项目管理知识体系构成
Guiyitong appointment registration
秒雲助力中電科32所發布“基於擬態應用集成框架的SaaS雲管理平臺解决方案”
Use of ES6 generator function
软考高项笔记 | 收集需求的工具与技术