#include <bits/stdc++.h>
using namespace std;
#define INF 0x7ffffff
#define maxn 105
typedef pair<int, int> P;
int dp[maxn][maxn], n, m;
char maze[maxn][maxn];
int mark[maxn][maxn];
int sx, sy, ex, ey;
int vis[maxn][maxn] = {0};
int go[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
queue<P> que;
void BFS()
{
que.push(P(ex, ey));
dp[ex][ey] = 0;
while(!que.empty())
{
P a = que.front();
que.pop();
if(vis[a.first][a.second])
continue;
vis[a.first][a.second] = 1;
for(int i=0; i<4; i++)
{
int x = a.first + go[i][0];
int y = a.second + go[i][1];
if(maze[x][y] != 'T' && dp[x][y] == INF)
{
que.push(P(x, y));
dp[x][y] = dp[a.first][a.second] + 1;
}
}
}
}
int main()
{
while(~scanf("%d%d", &n, &m) && n+m)
{
memset(mark, 0, sizeof(mark));
for(int i=0; i<n; i++)
{
maze[i][0] = 'T';
maze[i][m] = 'T';
}
for(int i=0; i<m; i++)
{
maze[0][i] = 'T';
maze[n][i] = 'T';
}
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
{
cin>>maze[i][j];
if(maze[i][j] == 'S')
{
sx = i, sy = j;
}
if(maze[i][j] == 'E')
{
ex = i, ey = j;
}
if(maze[i][j]>'0' && maze[i][j]<='9')
{
mark[i][j] = 1;
}
dp[i][j] = INF;
}
BFS();
long long ans = 0;
int tmp = dp[sx][sy];
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
{
if(mark[i][j])
if(dp[i][j] <= tmp)
{
ans += maze[i][j] - '0';
}
}
cout<<ans<<endl;
}
return 0;
}
/*
TSTTTTTT.T
......T..T
.T.TT.TT.T
.T........
TT.TT.TTTT
....T....T
.TTTTTTT.T
....T.....
.TTTT.TTT.
....T...ET
5 5
S9999
TTTT9
99999
9TTTT
9999E
*/
codeforces Biridian Forest
最新推荐文章于 2021-03-04 20:43:10 发布