[英]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.假设
X
和Y
都至少为 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.