-
Notifications
You must be signed in to change notification settings - Fork 6
/
queens_attack_2.cpp
124 lines (109 loc) · 3.78 KB
/
queens_attack_2.cpp
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
119
120
121
122
123
124
#include <bits/stdc++.h>
using namespace std;
int main(){
int n,i,j,temp,ans=0;
int k;
cin >> n >> k;
int rQueen;
int cQueen;
cin >> rQueen >> cQueen;
pair <int,int> left, right,up,down, leftup,rightup,leftdown,rightdown;
//first=row second=column
left.first=right.first=rQueen;
left.second=1;
right.second=n;
up.second=down.second=cQueen;
up.first=n;
down.first=1;
temp= min(n-rQueen, cQueen-1);
leftup.first=rQueen+temp;
leftup.second=cQueen-temp;
temp= min(rQueen-1, cQueen-1);
leftdown.first=rQueen-temp;
leftdown.second=cQueen-temp;
temp= min(n-rQueen, n-cQueen);
rightup.first=rQueen+temp;
rightup.second=cQueen+temp;
temp= min(rQueen-1, n-cQueen);
rightdown.first=rQueen-temp;
rightdown.second=cQueen+temp;
/*cout<<leftup.first<<","<<leftup.second<<endl;
cout<<up.first<<","<<up.second<<endl;
cout<<rightup.first<<","<<rightup.second<<endl;
cout<<right.first<<","<<right.second<<endl;
cout<<rightdown.first<<","<<rightdown.second<<endl;
cout<<down.first<<","<<down.second<<endl;
cout<<leftdown.first<<","<<leftdown.second<<endl;
cout<<left.first<<","<<left.second<<endl;
cout<<ans<<endl;*/
for(int a0 = 0; a0 < k; a0++){
int rObstacle;
int cObstacle;
cin >> rObstacle >> cObstacle;
if(rObstacle==rQueen && cObstacle>cQueen) //obstacle is right
{
right.second=min(cObstacle-1,right.second);
}
else if(rObstacle==rQueen && cObstacle<cQueen) //obstacle is left
{
left.second=max(cObstacle+1,left.second);
}
else if(cObstacle==cQueen && rObstacle<rQueen) //obstacle is down
{
down.first=max(rObstacle+1,down.first);
}
else if(cObstacle==cQueen && rObstacle>rQueen) //obstacle is up
{
up.first=min(rObstacle-1,up.first);
}
else if(abs(rObstacle-rQueen)==abs(cObstacle-cQueen)) //obstacle is on diagonal
{
if(rObstacle<rQueen && cObstacle<cQueen)//leftdown
{
if(rObstacle+1>=leftdown.first){
leftdown.first=rObstacle+1;
leftdown.second=cObstacle+1;
}
}
else if(rObstacle<rQueen && cObstacle>cQueen)//rightdown
{
if(cObstacle-1<=rightdown.second){
rightdown.first=rObstacle+1;
rightdown.second=cObstacle-1;
}
}
else if(rObstacle>rQueen && cObstacle<cQueen)//leftup
{
if(cObstacle+1>=leftup.second){
leftup.first=rObstacle-1;
leftup.second=cObstacle+1;
}
}
else if(rObstacle>rQueen && cObstacle>cQueen)//rightup
{
if(cObstacle-1<=rightup.second){
rightup.first=rObstacle-1;
rightup.second=cObstacle-1;
}
}
}
}
ans+=up.first-rQueen;
ans+=rQueen-down.first;
ans+=cQueen-left.second;
ans+=right.second-cQueen;
ans+=leftup.first-rQueen;
ans+=rQueen-leftdown.first;
ans+=rightup.first-rQueen;
ans+=rQueen-rightdown.first;
/*cout<<leftup.first<<","<<leftup.second<<endl;
cout<<up.first<<","<<up.second<<endl;
cout<<rightup.first<<","<<rightup.second<<endl;
cout<<right.first<<","<<right.second<<endl;
cout<<rightdown.first<<","<<rightdown.second<<endl;
cout<<down.first<<","<<down.second<<endl;
cout<<leftdown.first<<","<<leftdown.second<<endl;
cout<<left.first<<","<<left.second<<endl;*/
cout<<ans<<endl;
return 0;
}