当前位置:网站首页>Detonate the most bombs - C language DFS recursive approach
Detonate the most bombs - C language DFS recursive approach
2022-04-22 17:06:00 【Mr Gao】
The most detonated bombs
Here's a list of bombs . A bomb Explosion range Defined as a circle centered on a bomb .
The bomb uses a subscript from 0 Start with a two-dimensional integer array bombs Express , among bombs[i] = [xi, yi, ri] .xi and yi It means the first one i A bomb X and Y coordinate ,ri Indicates the explosion range radius .
You need to choose to detonate One bomb . When the bomb was detonated , all All bombs within its explosion range will be detonated , These bombs will further detonate other bombs within their explosion range .
Here's your array bombs , Please return to detonate One Under the premise of the bomb , most The number of bombs that can detonate .

Input :bombs = [[2,1,3],[6,1,4]]
Output :2
explain :
The picture above shows 2 The location and explosion range of a bomb .
If we detonate the bomb on the left , The bomb on the right will not be affected .
But if we detonate the bomb on the right , Both bombs will explode .
So the maximum number of bombs that can detonate is max(1, 2) = 2 .
Input :bombs = [[1,1,5],[10,10,5]]
Output :1
explain :
Detonating any bomb will not detonate another bomb . So the maximum number of bombs that can detonate is 1 .
Input :bombs = [[1,2,3],[2,3,1],[3,4,2],[4,5,3],[5,6,4]]
Output :5
explain :
The best detonating bomb is a bomb 0 , because :
- bomb 0 Set off the bomb 1 and 2 . The red circle indicates the bomb 0 The explosion range .
- bomb 2 Set off the bomb 3 . The blue circle indicates the bomb 2 The explosion range .
- bomb 3 Set off the bomb 4 . A green circle indicates a bomb 3 The explosion range .
So there are 5 A bomb was detonated .
The code implementation is as follows :
int dfs(int** bombs, int bombsSize,int x,int y,int r,int *a,int index){
int i,j;
int n=0;
float pi=3.1415926;
int x1,y1;
// printf("index %d ",index);
a[index]=1;
for(i=0;i<bombsSize;i++){
if(i!=index&&a[i]==0){
x1=bombs[i][0];
y1=bombs[i][1];
long long d1=x1-x,d2=y-y1;
// printf("d1,d2 %d %d ",d1,d2);
long long d3=d1*d1+d2*d2;
double d=d3;
d=sqrt(d);
// printf(" d %f ",d);
if(d<=r){
a[i]=1;
n++;
n=n+dfs(bombs,bombsSize,x1,y1,bombs[i][2],a,i);
}
}
}
return n;
}
int maximumDetonation(int** bombs, int bombsSize, int* bombsColSize){
int a[bombsSize];
int i,j;
int rz[bombsSize];
// if(bombsSize>90) return 10;
for(j=0;j<bombsSize;j++){
for(i=0;i<bombsSize;i++){
a[i]=0;
}
rz[j]=dfs(bombs,bombsSize,bombs[j][0],bombs[j][1],bombs[j][2],a,j)+1;
}
int max=rz[0];
for(j=1;j<bombsSize;j++){
if(rz[j]>max){
max=rz[j];
}
}
return max;
}
版权声明
本文为[Mr Gao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221701358223.html
边栏推荐
猜你喜欢

Your password does not satisfy the current policy requirements
![[distributed project] certification service center, social login oauth2 0. Single sign on and distributed session solutions](/img/b5/c4fb21101a8d4b83de760d0feda296.png)
[distributed project] certification service center, social login oauth2 0. Single sign on and distributed session solutions

SDN学习之Opendaylight浅析(一)

ASP.NET Core实现JWT授权与认证(2.实战篇)

4亿女性刚需,各路资本杀伐:藏在卫生巾里的1000亿生意

There was another bug today. When mogodb was used to query data, it was found that the returned data was null

香港云服务器怎样避免勒索软件攻击?

AQS source code reading

敬語 謙譲

Leetcode problem brushing plan -- monotonic sequence
随机推荐
swagger Authorization测试
The [ANSYS Workbench] mechanical interface displays the model tree window and the details window
bisect——对列表插入新数据仍然保持有序
Sequoia China led the team and voted for two female founders
js 获取汉字首字符
写LeetCode发现的一些神奇语句
JD side: how can a child thread get the value of the parent thread ThreadLocal? I got...
A serial port data receiving mode of stm32
混合背包呀
Facing the global market, platefarm today logs in to four major global platforms such as Huobi
IDEA代码重构小技巧(VIP典藏版)
Anomaly detection of log data based on deep learning
GlobalMapper20 如何批量把dwg文件转换为dxf文件,效率神器
Idea code refactoring tips (VIP collection version)
R语言上课代码记录5
SDN学习之Opendaylight浅析(五)
Vscode most complete practical plug-in (VIP collection version)
全网征集!说说你跟宜搭之间的故事吧
Redis(16) -- Redis集群
Sqlalchemy过滤时间