-
Notifications
You must be signed in to change notification settings - Fork 0
/
P3709.cpp
79 lines (64 loc) · 1.37 KB
/
P3709.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
#include <iostream>
#include <cstring>
using namespace std;
int graph[52][52] = {};
int n, r;
void find(int sx, int sy) {
int nx = sx, ny = sy;
int dx = 0, dy = 0;
if (sx == 0) {
dx = 1;
dy = 0;
} else if (sx == n + 1) {
dx = -1;
dy = 0;
} else if (sy == 0) {
dx = 0;
dy = 1;
} else if (sy == n + 1) {
dx = 0;
dy = -1;
}
while (true) {
nx += dx;
ny += dy;
if (nx < 1 || nx > n || ny < 1 || ny > n) {
cout << ny << " " << nx << "\n";
return;
}
if (!graph[ny][nx]) continue;
if (dx == 0 && dy == 1) {
dx = -1;
dy = 0;
} else if (dx == 0 && dy == -1) {
dx = 1;
dy = 0;
} else if (dx == 1 && dy == 0) {
dx = 0;
dy = 1;
} else if (dx == -1 && dy == 0) {
dx = 0;
dy = -1;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int T;
cin >> T;
while (T--) {
memset(graph, 0, sizeof(graph));
cin >> n >> r;
for (int i = 0; i < r; i++) {
int x, y;
cin >> x >> y;
graph[x][y] = 1;
}
int sx, sy;
cin >> sx >> sy;
find(sy, sx);
}
return 0;
}