-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabirint.cpp
More file actions
67 lines (60 loc) · 1.53 KB
/
Copy pathLabirint.cpp
File metadata and controls
67 lines (60 loc) · 1.53 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
#include <fstream>
#include <queue>
using namespace std;
ifstream cin("labirint.in");
ofstream cout("labirint.out");
int const di[] = {-1, 0, 1, 0};
int const dj[] = {0, 1, 0, -1};
int const NMAX = 1005;
int n, st, dr, mid, p, mat[NMAX][NMAX];
bool matriceFill[NMAX][NMAX];
inline bool ok(int i, int j) {
if (i < 1 || j < 1 || i > n || j > n)
return false;
if (matriceFill[i][j])
return false;
return true;
}
void fill(int istart, int jstart) {
if (mat[istart][jstart] > mid) return;
queue < pair < int, int > > Q;
Q.push(make_pair(istart, jstart));
matriceFill[istart][jstart] = true;
while (!Q.empty()) {
int r = Q.front().first;
int c = Q.front().second;
Q.pop();
for (int i = 0; i < 4; i++) {
int rr = r + di[i];
int cc = c + dj[i];
if (ok(rr, cc) && mat[rr][cc] <= mid) {
Q.push(make_pair(rr, cc));
matriceFill[rr][cc] = true;
}
}
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cin >> mat[i][j];
st = 0;
dr = 100000;
while (st <= dr) {
mid = (st + dr) >> 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
matriceFill[i][j] = false;
fill(1, 1);
if (matriceFill[n][n]) {
p = mid;
dr = mid - 1;
} else
st = mid + 1;
}
cout << p << '\n';
cin.close();
cout.close();
return 0;
}