-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbishopsAttack.js
80 lines (68 loc) · 2.29 KB
/
bishopsAttack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Good morning! Here's your coding interview problem for today.
// This problem was asked by Google.
// On our special chessboard,
// two bishops attack each other if they share the same diagonal.
// This includes bishops that have another bishop located between them,
// i.e. bishops can attack through pieces.
// You are given N bishops, represented as (row, column)
// tuples on a M by M chessboard.
// Write a function to count the number of pairs of bishops that attack each other.
// The ordering of the pair doesn't matter: (1, 2) is considered the same as (2, 1).
// For example, given M = 5 and the list of bishops:
// (0, 0)
// (1, 2)
// (2, 2)
// (4, 0)
// The board would look like this:
// [b 0 0 0 0]
// [0 0 b 0 0]
// [0 0 b 0 0]
// [0 0 0 0 0]
// [b 0 0 0 0]
const board = [
[1, 0, 0, 0, 1],
[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 1, 0],
[1, 0, 0, 0, 0]
];
function searchAttackers(row, col, matrix, parent, attackers, direction) {
const inBoardes =
row >= 0 && col >= 0 && row < matrix.length && col < matrix[row].length;
// return if the position is out of boarders
if (!inBoardes) {
return;
}
const child = `${row}${col}`;
// if we have found a bishop, and its not the bishop we start with
// push the couple into our results array
if (matrix[row][col] === 1 && child !== parent) {
attackers.push([parent, child]);
}
// try it on every position in the right diagonal of the board
if (direction === 1) {
searchAttackers(row + 1, col - 1, matrix, parent, attackers, 1);
}
// try it on every position in the left diagonal of the board
if (direction === 0) {
searchAttackers(row + 1, col + 1, matrix, parent, attackers, 0);
}
}
function explorBoard(matrix) {
const attackers = [];
for (let row = 0; row < matrix.length; row++) {
const currentRow = matrix[row];
for (let col = 0; col < currentRow.length; col++) {
const current = currentRow[col];
const parent = `${row}${col}`;
// if we have found bishop parent on our board,
// start eexploring all the possible positions to attack
if (currentRow[col] === 1) {
searchAttackers(row, col, matrix, parent, attackers, 1);
searchAttackers(row, col, matrix, parent, attackers, 0);
}
}
}
return attackers;
}
console.log(explorBoard(board));