-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharya_grid_1.cpp
53 lines (39 loc) · 1.08 KB
/
arya_grid_1.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
#include<bits/stdc++.h>
using namespace std;
struct Cell {
int x, y;
};
int countReachableCells(int N, int M, int X0, int Y0, int L) {
set<Cell> visited;
deque<Cell> q;
Cell start = {X0, Y0};
q.push_back(start);
visited.insert(start);
vector<pair<int, int>> directions = {{0, L}, {0, -L}, {L, 0}, {-L, 0}};
while (!q.empty()) {
Cell current = q.front();
q.pop_front();
for (auto& dir : directions) {
int newX = current.x + dir.first;
int newY = current.y + dir.second;
if (newX >= 1 && newX <= N && newY >= 1 && newY <= M) {
Cell newCell = {newX, newY};
if (visited.find(newCell) == visited.end()) {
visited.insert(newCell);
q.push_back(newCell);
}
}
}
}
return visited.size();
}
int main() {
int T;
cin >> T;
while (T--) {
int N, M, X0, Y0, L;
cin >> N >> M >> X0 >> Y0 >> L;
cout << countReachableCells(N, M, X0, Y0, L) << endl;
}
return 0;
}