forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconquestcampaign.cpp
49 lines (39 loc) · 1.02 KB
/
conquestcampaign.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
#include <bits/stdc++.h>
using namespace std;
int inf = 1 << 29;
vector<int> dx = {-1,1,0,0};
vector<int> dy = {0,0,1,-1};
bool inrange(int x, int y, int mx, int my) {
return x >= 0 && y >= 0 && x < mx && y < my;
}
int main() {
int r, c, n;
cin >> r >> c >> n;
vector<vector<int>> v(r,vector<int>(c,inf));
queue<pair<int,int>> q;
for(int i = 0 ; i < n; i++) {
int n1, n2;
cin >> n1 >> n2;
n1--;
n2--;
q.push({n1,n2});
v[n1][n2] = 1;
}
int hi = 1;
while(!q.empty()) {
int currx = q.front().first;
int curry = q.front().second;
q.pop();
for(int i = 0; i < 4; i++) {
int nextx = currx + dx[i];
int nexty = curry + dy[i];
if(!inrange(nextx,nexty,r,c)) continue;
if(v[nextx][nexty] == inf) {
v[nextx][nexty] = v[currx][curry] + 1;
hi = v[nextx][nexty];
q.push({nextx,nexty});
}
}
}
cout << hi << endl;
}