当前位置:网站首页>【2021】腾讯秋招技术岗编程 安排超市
【2021】腾讯秋招技术岗编程 安排超市
2022-04-22 18:01:00 【sigd】

解题思路:迷宫类问题,确定有多少个包含房子的连通块。在连通块内部选定一点建设一个超市,使得距离最短。bfs确定连通块,bfsC用于计算在某点建设超市的距离。
#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) /**< 用于计算在xx,yy建超市时,其他房子到这个超市距离,注意必须搜索,不可以直接求曼哈顿距离 */
{
int i,j,x,y,qx[3000],qy[3000],f=0,r=0,v[55][55]={0},dis=0;/**< 这个v会屏蔽全局v数组 */
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;/**< 记录距离 */
qx[r]=nx,qy[r++]=ny;
}
}
}
return dis;
}
void bfs(int x,int y)
{/**< qx,qy手写队列,共用一个头尾指针 */
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]=='#') /**< 房子存起来,用于计算 */
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)/**< 需要建超市 */
cnt++;
for(i=0; i<r; i++) /**< 在上面手写队列qx,qy里面,元素值都还在 */ /**< 检查在(qx[i],qy[i])建超市的总距离,另写一个深搜或广搜做这件事 */
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);/**< 一个新连通块 */
cout<<cnt<<' '<<sum;
return 0;
}
版权声明
本文为[sigd]所创,转载请带上原文链接,感谢
https://blog.csdn.net/sigd/article/details/124336532
边栏推荐
- An idea plug-in that doesn't work, but can install X
- Examples of comprehensive application of DOM -- click plus one and minus one
- 一个没啥L用,但可以装X的IDEA插件
- 【Lane】Ultra-Fast-Lane-Detection(1)自定义数据集训练
- Durable and easy to drive are recognized. On the first anniversary of ID. crozz, the power of new products has become stronger?
- 多线程笔记 | 比对 Thread 和 Runnable
- Youqilin 22.04 lts version is officially released | ukui 3.1 opens a new experience!
- Soft test high item notes | operation and maintenance service classification
- Multithreaded notes | future interface function
- 最近学习感悟
猜你喜欢

数字化靶场的未来方向

软考高项笔记 | 信息系统安全

Durable and easy to drive are recognized. On the first anniversary of ID. crozz, the power of new products has become stronger?

Vscode + PHP debug + namespace guidelines

软考高项笔记 | 信息化的五个层次

Unable to translate SQLException with Error code ‘0‘, will now try the fallback translator

秒雲助力中電科32所發布“基於擬態應用集成框架的SaaS雲管理平臺解决方案”

golang-gin-websocket问题

Inheritance relationship of Servlet

视频播放常用的库
随机推荐
Why has Sony always dominated the smartphone sensor market
How do I completely delete files on my computer?
Notes on soft test high items | information system life cycle
软考高项笔记 | 企业应用集成
Codeforces Round #784 (Div. 4) AK题解
find lower_ bound upper_ bound
华为路由器通过MPLS 虚拟私有网实现总部与分公司连接
邮箱格式测试
Common libraries for video playback
通过反射获得方法的参数实际名称
After calling Bapi many times, what is the problem with the last one-time commit work?
Fastjson 2 来了,性能继续提升,还能再战十年
Codeforces round 784 (Div. 4) AK solution
动态规划:多重背包问题
数字化靶场的未来方向
B树[概念]
In 2022, it is said on the Internet that Apple's upcoming new models iPhone 14 pro and iPhone 14 Pro Max will be a new screen shape, not a banged screen. Do you expect the iPhone 14 with a new screen
Pikachu File Inclusion (File Inclusion Vulnerability)
Get the actual name of the parameter of the method through reflection
广东水泥数据概况