-
Notifications
You must be signed in to change notification settings - Fork 2
/
biridian.cpp
67 lines (59 loc) · 1.54 KB
/
biridian.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <cstdio>
#include <queue>
#include <utility>
#include <algorithm>
#include <iostream>
#include <string>
#define A first
#define B second
using namespace std;
typedef pair<int,int> pii;
typedef pair<pii,int> piii;
string in;
int num[1010][1010];
bool vis[1010][1010];
int R,C;
int Ex,Ey,Sx,Sy;
int ans = 0;
int dist[1010][1010];
queue<piii> q;
int main(){
cin >> R >> C;
for(int y = 0;y<R;y++) for(int x = 0;x<C;x++) dist[y][x] = 99999;
for(int x = 0;x<R;x++){
cin >> in;
for(int a = 0;a<C;a++){
if(in.at(a)=='E'){
Ey = x;
Ex = a;
}
else if(in.at(a)=='S'){
Sy = x;
Sx = a;
}
else if(in.at(a)=='T') vis[x][a] = true;
else num[x][a] = in.at(a)-48;
}
}
q.push(piii(pii(Ey,Ex),0));
while(!q.empty()){
piii cur = q.front();
q.pop();
if(cur.A.A<0||cur.A.A>=R||cur.A.B<0||cur.A.B>=C) continue;
if(vis[cur.A.A][cur.A.B]) continue;
vis[cur.A.A][cur.A.B] = true;
dist[cur.A.A][cur.A.B] = cur.B;
q.push(piii(pii(cur.A.A+1,cur.A.B),cur.B+1));
q.push(piii(pii(cur.A.A-1,cur.A.B),cur.B+1));
q.push(piii(pii(cur.A.A,cur.A.B+1),cur.B+1));
q.push(piii(pii(cur.A.A,cur.A.B-1),cur.B+1));
}
for(int y = 0;y<R;y++){
for(int x = 0;x<C;x++){
if(dist[y][x]<=dist[Sy][Sx]) ans+=num[y][x];
//cout << dist[y][x] << " ";
}
// cout << endl;
}
cout << ans << endl;
}