简体   繁体   English

可以放置在 X x Y 网格中的长度为 3 的行数

[英]Number of lines of length 3 that can be placed in a X by Y grid

Given a grid of dimensions length X by height Y, how do I find the number of lines of length 3 that can be placed in the grid?给定一个长度为 X 高度为 Y 的网格,我如何找到可以放置在网格中的长度为 3 的行数? Accounting for horizontal, vertical and diagonal lines.考虑水平线、垂直线和对角线。

EXAMPLE 3x3 grid - 3 horizontal lines, 3 vertical lines, 2 diagonal lines = 8 possible lines.示例 3x3 网格 - 3 条水平线、3 条垂直线、2 条对角线 = 8 条可能的线。 3x4 grid - 4 horizontal lines, 6 vertical lines, 4 diagonal lines = 14 possible lines. 3x4 网格 - 4 条水平线、6 条垂直线、4 条对角线 = 14 条可能的线。

Let's assume that X and Y are both at least 3.假设XY都至少为 3。

In every row, there are X - 2 possible left-end points for a horizontal line of length three (all except the rightmost two columns).在每一行中,对于长度为 3 的水平线(除了最右边的两列),有X - 2可能的左端点。 So the overall number of possible horizontal lines is Y * (X - 2) .所以可能的水平线的总数是Y * (X - 2)

By symmetry, the overall number of possible vertical lines is X * (Y - 2) .通过对称性,可能的垂直线的总数是X * (Y - 2)

There are two types of diagonals according to their orientation.根据它们的方向,有两种类型的对角线。 Consider those that are in upper-left to lower-right direction.考虑从左上到右下方向的那些。 Similar to the argument above, for the upper-left endpoint of any such diagonal of length three, there are X - 2 possible columns and Y - 2 possible rows.与上面的论点类似,对于任何长度为 3 的对角线的左上端点,有X - 2可能的列和Y - 2可能的行。 So the overall number of such diagonals is (X - 2) * (Y - 2) .所以这样的对角线的总数是(X - 2) * (Y - 2)

By symmetry, the overall number of the other type of diagonal is the same.通过对称性,其他类型对角线的总数相同。

So the overall number of lines of length three is Y * (X - 2) + X * (Y - 2) + 2 * (X - 2) * (Y - 2) , which can be simplified to:所以长度为 3 的总行数是Y * (X - 2) + X * (Y - 2) + 2 * (X - 2) * (Y - 2) ,可以简化为:

(2 * X - 3) * (2 * Y - 3) - 1

As expected, this formula does agree with your examples.正如预期的那样,这个公式确实与您的示例一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以沿着y = 0,x = 0,y = x和y = -x以外的线在JFrame(JPanel)中移动对象? - Can I move objects in a JFrame(JPanel) along lines other than y=0, x=0, y=x and y=-x? 如果数字是十进制,如何检查某个数字是否可以表示为x ^ y? - How to check if some number can be expressed as x^y if number is a decimal? 返回任意数量的项目的最小X(可以包含Y个项目) - Return minimum X (that can contain Y items) for arbitrary number of items 如何获得 x、y、z 的每个长度和 p 的度数? - How can I get each length of x, y, z, and degree of p? 如何旋转(x,y)进行网格捕捉 - How to rotate (x, y) for grid snapping 将增量网格索引转换为(x,y)坐标 - Convert incremental grid index to (x,y) coordinates 如何在SQL Server数据库表中使用X和Y网格坐标选择数据圆? - How can I select a circle of data using X and Y grid coordinates in a SQL server database table? 返回格式为x ^ y的第i个数字,其中x和y为整数 - Return the ith number of the format x^y, where x and y are integers 找出最大的数,x对于给定的y和n,使得x ^ y &lt;= n - find greatest number, x for given y and n such that x ^ y <= n 如果沿线的 y 坐标已知,则沿 2 条线求 x 值 - Find the x values along 2 lines if the y coordinates along the lines are known
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM