-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneps65.cpp
More file actions
57 lines (46 loc) · 1.02 KB
/
Copy pathneps65.cpp
File metadata and controls
57 lines (46 loc) · 1.02 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
/*
* Contest : OBI 2015 - Fase 2
* Problem : Mina
* Link : https://neps.academy/br/exercise/65
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fastio ios::sync_with_stdio(0); cin.tie(0);
typedef pair<int, int> pii;
const int INF = 1e9;
int grid[101][101], dist[101][101], n;
pii pos[] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
void bfs(int i, int j) {
queue<pii> q;
q.push({i, j});
dist[i][j] = 0;
while (!q.empty()) {
int lin, col;
tie(lin, col) = q.front();
q.pop();
for (auto [v, h] : pos) {
int x = lin + v,
y = col + h;
if (x >= 0 && x < n && y >= 0 && y < n) {
if (dist[lin][col] + grid[x][y] < dist[x][y]) {
q.push({x, y});
dist[x][y] = dist[lin][col] + grid[x][y];
}
}
}
}
}
int main() {
fastio
cin >> n;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> grid[i][j];
dist[i][j] = INF;
}
}
bfs(0, 0);
cout << dist[n - 1][n - 1] << '\n';
return 0;
}