-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboj.1261.cc
81 lines (65 loc) · 1.74 KB
/
boj.1261.cc
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
72
73
74
75
76
77
78
79
80
81
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll INF = 1e8;
ll n, m;
char mat[101][101] = {{0}};
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m;
for(int i = 0; i<n; ++i)
{
cin.getline(mat[i], 101);
for(int l = 0; l<m; ++l)
{
mat[i][l] -= '0';
}
}
vector<vector<int>> cost(n, vector<int>(m, INF));
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
cost[0][0] = 0;
pq.push(make_pair(0, 0));
while(!pq.empty())
{
int c = pq.top().first, i = pq.top().second/100, l = pq.top().second%100, _i, _l;
pq.pop();
if(i-1>=0)
{
_i = i-1, _l = l;
if(cost[i][l] + mat[_i][_l] < cost[_i][_l])
{
pq.push(make_pair(cost[_i][_l] = cost[i][l] + mat[_i][_l], _i*100+_l));
}
}
if(i+1<n)
{
_i = i+1, _l = l;
if(cost[i][l] + mat[_i][_l] < cost[_i][_l])
{
pq.push(make_pair(cost[_i][_l] = cost[i][l] + mat[_i][_l], _i*100+_l));
}
}
if(l-1>=0)
{
_i = i, _l = l-1;
if(cost[i][l] + mat[_i][_l] < cost[_i][_l])
{
pq.push(make_pair(cost[_i][_l] = cost[i][l] + mat[_i][_l], _i*100+_l));
}
}
if(l+1<m)
{
_i = i, _l = l+1;
if(cost[i][l] + mat[_i][_l] < cost[_i][_l])
{
pq.push(make_pair(cost[_i][_l] = cost[i][l] + mat[_i][_l], _i*100+_l));
}
}
}
cout<<cost[--n][--m]<<"\n";
return 0;
}