当前位置:网站首页>[day_060423] no two
[day_060423] no two
2022-07-26 06:12:00 【On the Bank of Anhe Bridge】
No two
Title source
Cattle from : No two
Title Description
Er Huo Xiaoyi has one W*H Grid box , The row number of the grid is 0H-1, The column number of the grid is 0W-1. At most one cake can be placed in each grid , The Euclidean distance between any two cakes cannot be equal to 2.
For two lattice coordinates (x1,y1),(x2,y2) Euclidean distance of :
( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) ) The arithmetic square root of
Xiao Yi wants to know how many cakes can be put in the grid box at most .
Input description
Each array contains the length and width of the grid W,H, Split with spaces .(1 ≤ W、H ≤ 1000)
Output description
Output a maximum number of cakes
Example 1
Input
3 2
Output
4
Thought analysis
Solution 1
- Coordinate relation method
- Euclidean distance cannot be 2, Explain that the distance between abscissa or ordinate cannot be 2 and 0
- Use a two-dimensional array to simulate the mesh ,0 It means that the cake can be placed ,1 Indicates that cannot be placed
- The initial value of the array is 0, Start from the lower left corner and traverse , Find out where you can't put the cake , Notice the bounds of the array
Solution 2
- Looking for a regular
- This method is more complicated , It is necessary to determine the rule of the number of cakes that can be placed in each row according to the conditions , The placement pattern is shown in the figure , When the width and height are different , The rules are also different , It needs to be judged

Code display
Solution 1
#include<iostream>
using namespace std;
int main()
{
int w, h = 0;
int count = 0;
cin >> w >> h;
int a[1000][1000] = {
0 };
// Traversing a two-dimensional array , Simulate coordinate plane
for (int i = 0; i < w; i++)
{
for (int j = 0; j < h; j++)
{
//0 The table placed the cake ,1 It means that there is no
if (a[i][j] == 0)
{
count++;
if (i < w - 2)
{
a[i + 2][j] = 1;
}
if (j < h - 2)
{
a[i][j + 2] = 1;
}
}
}
}
cout << count;
return 0;
}
Solution 2
#include<iostream>
using namespace std;
// This function is used to count the number of cakes in each row
int Count(int len)
{
int count = len / 4 * 2;
if (len % 4 >= 2)
{
// If module 4 Greater than or equal to 2, Then in addition to the previous division , It can also be placed 2 individual
count += 2;
}
else if (len % 4 == 1)
{
// model 4 by 1, Except for the divisible part , You can put another
count += 1;
}
return count;
}
int main()
{
int W = 0;
int H = 0;
while (cin >> W >> H)
{
if (W * H < 4)
{
// There is only one square
if (W * H == 1)
{
cout << 1;
continue;
}
// The square is 1*3 perhaps 1*2
else
{
cout << 2;
continue;
}
}
int sum = 0;
// Go through... From the first line
for (int i = 0; i < H; i++)
{
//H Represents the vertical height of the square ,W Represents square width , Traverse the grid from the first layer , Count the number of cakes that can be placed in each row
// According to the law , Two cakes in a row need two empty positions , So four in a group
// Because the square is rectangular , So there are two lines in every four lines , The other two lines are put less
// It can be modeled according to the line number 4 The size of determines whether the current line is put more or less , Many direct calls Count function , Use less Count(W-2)
if (i % 4 < 2)
{
sum += Count(W);
}
else {
sum += Count(W - 2);// Equivalent to two unprecedented positions , Put the cake from the third , therefore -2
}
}
cout << sum;
}
return 0;
}
summary
- If the array is not initialized after definition, its element value is a random value , Partial initialization after definition , Then the uninitialized part is 0
边栏推荐
- Leetcode:934. The shortest Bridge
- Leetcode 347. top k high frequency elements
- 机械制造企业如何借助ERP系统,做好生产管理?
- How can programmers improve mental internal friction?
- 【(SV && UVM) 笔试面试遇到的知识点】~ phase机制
- ament_ Cmake generates the ros2 library and links it
- MySQL multi table query introduction classic case
- Taobao pinduoduo Tiktok 1688 Suning taote jd.com and other keyword search commodity API interfaces (keyword search commodity API interface, keyword search commodity list interface, classification ID s
- L. Link with Level Editor I dp
- Realize channel routing based on policy mode
猜你喜欢

Is the transaction in mysql45 isolated or not?

Solutions to the failure of copy and paste shortcut keys

Mysql45 talks about transaction isolation: why can't I see it after you change it?

Database SQL language practice

Blurring of unity pixel painting

Recursive processing - subproblem
![[MySQL from introduction to proficiency] [advanced chapter] (VI) storage engine of MySQL tables, comparison between InnoDB and MyISAM](/img/19/ca3a5710ead3c5b9a222a8ae4ecb37.png)
[MySQL from introduction to proficiency] [advanced chapter] (VI) storage engine of MySQL tables, comparison between InnoDB and MyISAM

TPS Motion(CVPR2022)视频生成论文解读

Flex layout

Implementation of PHP multitask second timer
随机推荐
“子问题的递归处理”——判断两棵树是不是相同的树——以及 另一颗树的子树
WebAPI整理
英语句式参考纯享版 - 状语从句
Read five articles in the evening | Economic Daily: don't treat digital collections as wealth making products
Redis sentinel cluster setup
Optical quantum milestone: 3854 variable problems solved in 6 minutes
Intelligent fire protection application based on fire GIS system
H. Take the elevator greedy
Kingbasees SQL language reference manual of Jincang database (8. Function (10))
Oc/swift Technology Download File (breakpoint continuation AFN download file alamofire Download File native download) (source code)
卸载手机自带APP的操作步骤
Widget is everything, widget introduction
L. Link with Level Editor I dp
【Day_02 0419】倒置字符串
Using dynamic libraries in VS
Kingbasees SQL language reference manual of Jincang database (7. Conditional expression)
【Day_05 0422】统计回文
Webapi collation
【Day_04 0421】进制转换
对接微信支付(二)统一下单API