-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathQueen'sAttackII.java
More file actions
118 lines (102 loc) · 4.5 KB
/
Queen'sAttackII.java
File metadata and controls
118 lines (102 loc) · 4.5 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
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
108
109
110
111
112
113
114
115
116
117
118
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
int rQueen = in.nextInt();
int cQueen = in.nextInt();
//Row Column coordinates of the closes object in each direction
int rRObstacle = -1;
int cRObstacle = -1;
int rBRObstacle = -1;
int cBRObstacle = -1;
int rBObstacle = -1;
int cBObstacle = -1;
int rBLObstacle = -1;
int cBLObstacle = -1;
int rLObstacle = -1;
int cLObstacle = -1;
int rTLObstacle = -1;
int cTLObstacle = -1;
int rTObstacle = -1;
int cTObstacle = -1;
int rTRObstacle = -1;
int cTRObstacle = -1;
//Total squares attacked by the queen
int reachableSquares = 0;
//Finds the closest object in each direction
for(int a0 = 0; a0 < k; a0++){
int rObstacle = in.nextInt();
int cObstacle = in.nextInt();
//Right
if((cObstacle < cRObstacle || rRObstacle == -1) && cObstacle > cQueen && rObstacle == rQueen)
{
rRObstacle = rObstacle;
cRObstacle = cObstacle;
}
//Bottom Right
if(rQueen - rObstacle == cObstacle - cQueen && cObstacle > cQueen && rObstacle < rQueen
&& ((rObstacle > rBRObstacle && cObstacle < cBRObstacle) || rBRObstacle == -1))
{
rBRObstacle = rObstacle;
cBRObstacle = cObstacle;
}
//Bottom
if((rObstacle > rBObstacle || rBObstacle == -1) && rObstacle < rQueen && cObstacle == cQueen)
{
rBObstacle = rObstacle;
cBObstacle = cObstacle;
}
//Bottom Left
if(rQueen - rObstacle == cQueen - cObstacle && cObstacle < cQueen && rObstacle < rQueen
&& ((rObstacle > rBLObstacle && cObstacle > cBLObstacle) || rBLObstacle == -1))
{
rBLObstacle = rObstacle;
cBLObstacle = cObstacle;
}
//Left
if((cObstacle > cLObstacle || rLObstacle == -1) && cObstacle < cQueen && rObstacle == rQueen)
{
rLObstacle = rObstacle;
cLObstacle = cObstacle;
}
//Top Left
if(cQueen - cObstacle == rObstacle - rQueen && cObstacle < cQueen && rObstacle > rQueen
&& ((rObstacle < rTLObstacle && cObstacle > cTLObstacle) || rTLObstacle == -1))
{
rTLObstacle = rObstacle;
cTLObstacle = cObstacle;
}
//Top
if((rObstacle < rTObstacle || rTObstacle == -1) && rObstacle > rQueen && cObstacle == cQueen)
{
rTObstacle = rObstacle;
cTObstacle = cObstacle;
}
//Top Right
if(rObstacle - rQueen == cObstacle - cQueen && cObstacle > cQueen
&& rObstacle > rQueen && ((rObstacle < rTRObstacle && cObstacle < cTRObstacle) || rTRObstacle == -1))
{
rTRObstacle = rObstacle;
cTRObstacle = cObstacle;
}
}
//Calculates the distance to the closest obstacle in each direction and adds it to reachableSquares
//R B L T
reachableSquares += (cRObstacle != -1) ? (cRObstacle - cQueen -1) : n - cQueen;
reachableSquares += (rBObstacle != -1) ? (rQueen - rBObstacle - 1) : rQueen - 1;
reachableSquares += (cLObstacle != -1) ? (cQueen - cLObstacle -1) : cQueen - 1;
reachableSquares += (rTObstacle != -1) ? (rTObstacle - rQueen - 1) : n - rQueen;
//BR BL TL TR
reachableSquares += (cBRObstacle != -1) ? (cBRObstacle - cQueen -1) : Math.min(n - cQueen, rQueen - 1);
reachableSquares += (rBLObstacle != -1) ? (cQueen - cBLObstacle - 1) : Math.min(cQueen - 1, rQueen - 1);
reachableSquares += (cTLObstacle != -1) ? (cQueen - cTLObstacle -1) : Math.min(cQueen - 1, n - rQueen);
reachableSquares += (rTRObstacle != -1) ? (cTRObstacle - cQueen - 1) : Math.min(n - cQueen, n - rQueen);
System.out.println(reachableSquares);
}
}