当前位置:网站首页>牛客刷题——不要二
牛客刷题——不要二
2022-06-11 18:14:00 【HHYX.】
题目链接:不要二
题目描述
二货小易有一个W*H的网格盒子,网格的行编号为0-H-1,网格的列编号为0~W-1。每个格子至多可以放一块蛋糕,任意两块蛋糕的欧几里得距离不能等于2。
对于两个格子坐标(x1,y1),(x2,y2)的欧几里得距离为:
( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) ) 的算术平方根
小易想知道最多可以放多少块蛋糕在网格盒子里。
输入描述:
每组数组包含网格长宽W,H,用空格分割.(1 ≤ W、H ≤ 1000)
输出描述:
输出一个最多可以放的蛋糕数
题目分析
题中提到了欧几里得距离的计算公式如下:( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) ) 的算数平方根,满足题目要求的距离也就是( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) ) 等于4的情况,但是这里的值都是整数,要满足公式等于4的情况只有(x1-x2)或者(y1-y2)的绝对值等于2的时候,也就是说只有在横向或者竖向上距离为2的时候才能满足等于2的条件,注意一下题中要求的是不能等于2.也就是说在[i][j]位置有蛋糕的话[i+2][j]和[i][j+2]位置就不能有蛋糕了,同时需要防止数组越界。这里可以先将数组元素全部初始化为1,此时遍历数组当遇到[i][j]为1的时候,计数器加1,同时需要将[i+2][j]和[i][j+2]置为0,最终返回计数器的大小即可,代码实现如下:
代码实现
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int w,h,_count=0;
cin>>w>>h;
vector<vector<int>> arr;
arr.resize(w);
for(auto& e :arr)
{
e.resize(h,1);
}
for(size_t i=0;i<arr.size();i++)
{
for(size_t j=0;j<arr[i].size();j++)
{
if(arr[i][j]==1)
{
_count++;
if(i+2<w)
{
arr[i+2][j]=0;
}
if(j+2<h)
{
arr[i][j+2]=0;
}
}
}
}
cout<<_count<<endl;
}

边栏推荐
- 学习使用LSTM和IMDB评论数据进行情感分析训练
- SISO Decoder for SPC (补充章节1)
- ACL 2022: is it no longer difficult to evaluate word polysemy? A new benchmark "dibimt"
- Hwang
- SQL error injection 1
- 谈谈远程工作 | 社区征文
- [C语言]压缩字符串并添加标记字符
- [golang] leetcode - 292 Nim games (Mathematics)
- Common interview questions of network and concurrent programming
- [pat grade B question bank] complete summary
猜你喜欢
![[c language] output students' names and scores in descending order of scores with structures](/img/41/b9dba88941560b296f4d7153b7c684.png)
[c language] output students' names and scores in descending order of scores with structures

SISO Decoder for Repetition(补充章节4)

SISO decoder for a general (n,n-1) SPC code(补充章节3)

VIM common commands

SISO Decoder for a General (n, N - 1) SPC Code (Supplementary section 3)
![[C语言]用结构体把最高分的学生输出,可有多个最高分](/img/4e/836a8f717a2d9bf5f999a934ff4c91.png)
[C语言]用结构体把最高分的学生输出,可有多个最高分

SISO Decoder for SPC (补充章节1)
![[C语言]用结构体按分数高低降序输出学生的姓名和分数](/img/41/b9dba88941560b296f4d7153b7c684.png)
[C语言]用结构体按分数高低降序输出学生的姓名和分数

Surveillance des fonctions de perte avec visdom
![[not forgetting the original intention and forging ahead] 2021 Zhongchuang Suanli new year conference and anniversary celebration](/img/ae/9a0c300f2dcb03b05d737f14b0955f.jpg)
[not forgetting the original intention and forging ahead] 2021 Zhongchuang Suanli new year conference and anniversary celebration
随机推荐
LeetCode_前缀树_中等_208. 实现 Trie (前缀树)
合并多棵二叉搜索树
SA token single sign on SSO mode 2 URL redirection propagation session example
ubuntu 安装psql以及运行相关命令
[C语言]用结构体把输入的指定分数范围内的学生输出
SISO decoder for a general (n,n-1) SPC code(补充章节3)
System learning typescript (V) - joint type
Codeworks round 481 (Div. 3) [done]
Feign shares login information for request
[c language] limit the number of searches and output the maximum value found in the number of internal searches
金融银行_催收系统简介
General terms in security field
LDAP 目录服务器的现代化应用
Surveillance des fonctions de perte avec visdom
* Jetpack 笔记 LifeCycle ViewModel 与LiveData的了解
H. 264 concept
安全领域常规术语
Is it good or not to open a stock account on the flush? Is it safe?
The HashSet collection stores student objects and traverses
力扣39题组合总和