-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueensThatCanAttacktheKing.java
More file actions
54 lines (46 loc) · 1.88 KB
/
Copy pathQueensThatCanAttacktheKing.java
File metadata and controls
54 lines (46 loc) · 1.88 KB
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
package com.leetcode.year_2020.Greedy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* https://leetcode.com/problems/queens-that-can-attack-the-king/
*
* @author neeraj on 22/05/20
* Copyright (c) 2019, data-structures.
* All rights reserved.
*/
public class QueensThatCanAttacktheKing {
public static void main(String[] args) {
System.out.println(queensAttacktheKing(new int[][]{
{0, 1}, {1, 0}, {4, 0}, {0, 4}, {3, 3}, {2, 4}
}, new int[]{0, 0}));
}
public static List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) {
List<List<Integer>> queensWhichCanAttack = new ArrayList<>();
/**
* First Place all the queens
*/
boolean[][] board = new boolean[8][8];
for (int[] queen : queens) {
board[queen[0]][queen[1]] = true;
}
// Now we just have to move in 8 directions
int[][] directions = new int[][]{{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}};
for (int[] direction : directions) {
int king_x = king[0];
int king_y = king[1];
// Now we will keep moving into 1 direction until we find any queen or just hit the boundary
while (king_x + direction[0] >= 0 && king_x + direction[0] < 8
&& king_y + direction[1] >= 0 && king_y + direction[1] < 8) {
king_x += direction[0];
king_y += direction[1];
if (board[king_x][king_y] == true) {
queensWhichCanAttack.add(Arrays.asList(king_x, king_y));
break; // Because once we hit a queen we don't want to proceed with that direction
// as other queen in same direction cannot over-cross this queen.
}
}
}
return queensWhichCanAttack;
}
}