Answer the question
In order to leave comments, you need to log in
How to find out if a number is next to another specific number in a matrix?
There is a 16x16 matrix. Completely filled with zeros. Further I in the center of a matrix, I add unit. After that, I generate a random index of the matrix. And how do you know if this index is next to 1 in a dense one?
For example:
0 0 0 0 0
0 0 0 0 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 1 1 0
0 0 0 0 0
0 0 0 0 0
Answer the question
In order to leave comments, you need to log in
1. У матрицы 16x16 центр находится между ячейками, туда невозможно записать число.
2. Определитесь, что значит "рядом". Если это только четыре соседних клетки, то значит |i1-i2|+|j1-j2| = 1, если допустимы диагонали, то |i1-i2|+|j1-j2| > 0 && |i1-i2| <= 1 && |j1-j2| <= 1
x - столбец
y - строка
Вы ставите 1 не в сферическом центре, а в ячейке с индексом 3; 3
Далее генерируете рандомный индекс, например, 4; 3
Если вы знаете оба индекса, в чем сложность просто их сравнить?
Навскидку, если |x2 - x1| < 2 и |y2 - y1| < 2, то индекс установлен вплотную.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question