-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathQueens That Can Attack the King.java
107 lines (102 loc) · 2.52 KB
/
Queens That Can Attack the King.java
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Runtime: 2 ms (Top 56.88%) | Memory: 44 MB (Top 9.63%)
class Solution {
public List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) {
List<List<Integer>> res = new ArrayList<>();
int r=king[0];
int l=king[1];
int [][]board =new int[8][8];
int n=8;
// 8 cases;
// moving upward
// moving downward
// moving right
// moving left
// moving upper right diagonal
// moving upper left diagonal'
// moving lower left diagonal
// moving lower right diagonal
int i,j;
for(i=0;i<queens.length;i++)
{
for(j=0;j<1;j++)
{
int row=queens[i][j];
int column=queens[i][j+1];
board[row][column]=1;
}
}
// for(i=0;i<8;i++)
// {
// for(j=0;j<8;j++)
// {
// System.out.println(board[i][j]);
// }
// System.out.println();
// }
for(j=l;j<n;j++)
{
if(board[r][j]==1)
{
res.add(new ArrayList<>(Arrays.asList(r, j)));
break;
}
}
for(i=r;i<n;i++)
{
if(board[i][l]==1)
{
res.add(new ArrayList<>(Arrays.asList(i, l)));
break;
}
}
for(i=r;i>=0;i--)
{
if(board[i][l]==1)
{
res.add(new ArrayList<>(Arrays.asList(i, l)));
break;
}
}
for(j=l;j>=0;j--)
{
if(board[r][j]==1)
{
res.add(new ArrayList<>(Arrays.asList(r, j)));
break;
}
}
for(i=r,j=l;i>=0 && j>=0;j--,i--)
{
if(board[i][j]==1)
{
res.add(new ArrayList<>(Arrays.asList(i, j)));
break;
}
}
for(i=r,j=l;j<n && i>=0;j++,i--)
{
if(board[i][j]==1)
{
res.add(new ArrayList<>(Arrays.asList(i, j)));
break;
}
}
for(i=r,j=l;i<n && j<n;j++,i++)
{
if(board[i][j]==1)
{
res.add(new ArrayList<>(Arrays.asList(i, j)));
break;
}
}
for(i=r,j=l;j>=0 && i<n;j--,i++)
{
if(board[i][j]==1)
{
res.add(new ArrayList<>(Arrays.asList(i, j)));
break;
}
}
return res;
}
}