-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFireAgain.cpp
More file actions
71 lines (56 loc) · 1.4 KB
/
FireAgain.cpp
File metadata and controls
71 lines (56 loc) · 1.4 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
#include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
pair<int, int> bfs(int n, int m, vector<pair<int, int>>& lst) {
vector<vector<int>> burnTime(n, vector<int>(m, INT_MAX));
queue<pair<int, int>> q;
for (auto point : lst) {
int x = point.first - 1;
int y = point.second - 1;
q.push({x, y});
burnTime[x][y] = 0;
}
while (!q.empty()) {
auto [x, y] = q.front();
q.pop();
for (int i = 0; i < 4; ++i) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && nx < n && ny >= 0 && ny < m && burnTime[nx][ny] == INT_MAX) {
burnTime[nx][ny] = burnTime[x][y] + 1;
q.push({nx, ny});
}
}
}
int mx = 0;
pair<int, int> ans = {1, 1};
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (burnTime[i][j] > mx) {
mx = burnTime[i][j];
ans = {i + 1, j + 1};
}
}
}
return ans;
}
int main() {
ifstream inFile("input.txt");
ofstream outFile("output.txt");
if (!inFile || !outFile) {
cerr << "Error opening file!" << endl;
return 1;
}
int n, m;
inFile >> n >> m;
int k;
inFile >> k;
vector<pair<int, int>> lst(k);
for (int i = 0; i < k; ++i) {
inFile >> lst[i].first >> lst[i].second;
}
pair<int, int> result = bfs(n, m, lst);
outFile << result.first << " " << result.second << endl;
return 0;
}