- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在写一个单词搜索谜题,我想知道对角线和反对角线定位标语的最佳方法是什么 - 因为现在我已经实现了水平和垂直标语(每个“类型”的标语都是派生的来自父抽象标语),这是搜索将垂直放置的单词的起始索引的代码 - 如果您对此实现有任何提示,请也给我您的反馈:)
public List<Coordinate> findPossiblePositionsForWord(Character[][] grid) {
List<Coordinate> result = new ArrayList<>();
int consecutiveEmptyFields;
final int wordLength = getWord().length();
for (int column = 0; column < grid[0].length; column++) {
consecutiveEmptyFields = 0;
for (int row = grid.length - 1; row >= 0; row--) {
if (grid[row][column] == null) {
consecutiveEmptyFields++;
if (consecutiveEmptyFields >= wordLength) {
result.add(new Coordinate(row, column));
}
} else {
consecutiveEmptyFields = 0;
}
}
}
return result;
}
我能想到的最简单的方法是使用两个 for 循环:从 0 到 x <= word.length 和从 1 到 y <= word.length - 但也许有更好的方法:特别是使用 Java。
进一步说明:对于像这样的标语:"HOKUSPOKUS", "THIS", "GOOGLE", "STRESS", "TEST", "LENGTHY", "PARTLY"我会得到这样的结果(例如 - 它是随机的):
*****************************************
Catchprase successfully added!
Catchprase successfully added!
Catchprase successfully added!
Catchprase successfully added!
Catchprase successfully added!
Catchprase successfully added!
Catchprase successfully added!
*****************************************
G O O G L E _ _ _ H
_ _ _ _ _ _ T _ _ O
_ _ _ _ _ _ H _ _ K
_ _ _ _ _ _ I _ P U
_ _ _ _ _ _ S _ A S
L E N G T H Y _ R P
S T R E S S _ _ T O
_ _ _ _ _ _ _ _ L K
_ _ _ _ _ _ _ _ Y U
T E S T _ _ _ _ _ S
Process finished with exit code 0
现在我也想对角添加标语
最佳答案
我建议不要对每个方向进行特殊封装,而是返回一个方向+起点。这是处理向前、向后、向上、向下以及任一方向的两个对角线的基本思想。
public List<Pair<Coordinate, Coordinate>> findPossiblePositionsForWord(Character[][] grid) {
List<Pair<Coordinate, Coordinate>> result = new ArrayList<>();
final int wordLength = getWord().length();
for (int dirColumn : {-1, 0, 1}) {
for (int dirRow : {-1, 0, 1}) {
if (dirColumn == 0 && dirRow == 0) {
continue;
}
for (
int column = max(0, -dirColumn * wordLength);
column < min(grid.length, grid.length + dirColumn*wordLength);
column++
) {
for (
int row = max(0, -dirRow * wordLength);
row < min(grid.length, grid.length + dirRow*wordLength);
row++
) {
// Test for whether the word can go here...
if (wordCanGoHere) {
result.add(
new Pair(
new Coordinate(row, column),
new Coordinate(dirRow, dirColumn)
)
);
}
}
}
}
}
return result;
}
关于java - 如何最好地迭代二维数组的对角线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73920047/
我有一个 6x6 数组,并且希望始终获取接下来的四个值。举个例子: 0----- -1---- --2--- ---3-- ----4- 所以我想得到所有对角线的 (0+1+2+3) 和 (1+2+3
我想遍历一个已转换为一维的二维方阵。 问题是我想遍历它,就像我在对角条中遍历原始 2D 一样。 该数组是对角数组,我最初使用一维的 malloc 创建它,以避免分配太多内存。 数组的大小: int T
“给定一个 2D 字符数组和一个字符串。 查找特定字符串是否出现在矩阵的对角线上。 private static boolean diagonalContains(char[][] grid,Stri
我有一个由 nxn 矩阵组成的 njxnj 矩阵。我想提取 nxn 矩阵的对角 j block 。即我想提取对角线(对于 n = 2,j = 4): 最有效的方法是什么? 最佳答案 要为元素建立索引,
这是一个 self 回答的问题。给定两个数据框, x 0 1 0 1 2 1 3 4 y 0 1 2 0 5 6 7 1 8 9 X 2 Y Z 0 x 和
我试图让我的程序打印出不等于幻方规则的行、列或对角线,例如,如果矩阵是 1 9 5 2 4 3 6 8 7 第 1 行 [2, 4, 3] 不起作用 第 2 行 [6, 8, 7] 不起作用 第 0
所以我有一个像这样的 4x4 矩阵 |0 1 2 3 -+------- 0|0 1 3 6 1|2 4 7 a 2|5 8 b d 3|9 c e f 并且我是按照其中的十六进制字符指定的顺序遍历
什么是获取正方形DataFrame的对角线的有效方法。我希望结果是一个 Series 和一个 MultiIndex 有两个级别,第一个是 DataFrame 的索引,第二个级别是DataFrame 的
问题:我正在尝试在 SwiftUI 中以矩形呈现对角线线性渐变。 我实现了一个标准的多点线性渐变,它在呈现为正方形时效果很好,但是当我将框架更改为矩形时,它有一些奇怪的行为,看起来更水平,或者有一些奇
我目前正在尝试找到一种在 C# for Excel 中使用 VSTO 的方法,以使用 C# 代码在单元格中绘制对角线。但我在网上找不到任何人甚至试图这样做。 有谁知道如何实现这一目标? 谢谢 (对不起
我需要删除图像中的线条,这最终是一个表格。我找到了一种删除水平线和垂直线的方法: convert 1.jpg -type Grayscale -negate -define morphology:co
我有一个如下所示的矩阵: ` matrix = [ ['P', 'o', 'P', 'o', 'P'], ['m', 'i', 'c', 's', 'r'], ['g', 'a', 'T', 'A',
如何在Python中按如下方式堆叠矩阵,使得父矩阵的元素在子矩阵的相同 block 对角点处形成 block 对角线。例子:我有四个矩阵 AA,AB,BA,BB 我想制作如附图所示的矩阵。 最佳答案
我在做一些统计。 我有数据框: tag a b c d e f a 5 2 3 2 0 1 b 2 4 3 2 0 1
我最近做了一个 Connect4 游戏,当我的 Connect4 向右对角线连接时,我的 Connect4 没有赢得游戏。并且它只适用于某些组合,当它连接到左边的对角线时。坐标:- 左上角:(0,0)
在 numpy 中有什么方法可以获取对角数组的引用吗?我希望我的数组对角线除以某个因子谢谢 最佳答案 如果 X 是你的数组,c 是因子, X[np.diag_indices_from(X)] /= c
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 6 年前。 Improve this ques
我有一个形状为 (m*n, m*n) 的张量,我想提取一个大小为 (n, m*n) 的张量,其中包含对角线上大小为 n*n 的 m 个块。例如: >>> a tensor([[1, 2, 0, 0],
我目前正在使用 matplotlib/pyplot 绘制 3d 对象,如下所示: fig = plt.figure().gca(projection='3d') plot = fig.plot_sur
好的,让我们考虑一个 64 位的数字,它的位组成一个 8x8 的表。 例如 0 1 1 0 1 0 1 0 0 1 1 0 1 0 1 1 0 1 1 1 1 0 1 0 0 1 1 0 1 0 1
我是一名优秀的程序员,十分优秀!