如何处理以 2D 数组编写的条件

How to handle conditions written in 2D array

本文关键字:数组 条件 2D 何处理 处理      更新时间:2023-10-16

考虑这个2D数组:

A B C D E
A 1 0 0 0 0
B 0 1 1 0 0
C 0 1 1 0 0
D 0 0 0 1 1
E 0 0 1 1 1

这意味着 A可以映射到 A,但 sureshot 不能映射到 B、C、D 或 E。

类似地,B可以映射到 B 和 C,但不能映射为 A、D 和 E。

此处可能意味着它可能被映射或不映射。

现在,我必须编写一个 c++ 代码来确保此映射成立,如果它不成立,则断言。

我已经像这样写了一个关于它的 if else 代码(骨架(。我正在检查 B 应该不确定射击匹配的条件。

if (checking_for_B) {
if (B is mapped with A || B is mapped with D || B is mapped with E) {
assert();
}
}

同样,我还必须为 A、B、C、D 和 E 编写 if 条件。我对这种方法不满意。如果你是我,你会写什么?

我会尝试通过将字母转换为行和列索引来解决您的问题,并检查是否允许该组合。

那么你不需要"checking_for_A"后跟"checking_for_B"等。只是像

if (my_map[row_letter_index][column_letter_index])
{
// All okay, allow
}
else
{
// Not allowed, report or abort or throw exception or anything else
}