【Codeforces 329B】Biridian Forest

【链接】 我是链接,点我呀:)
【题意】

题意

【题解】

找到出口到每个点的最短距离。 设你到出口的最短距离为temp 那么如果某个人到终点的距离<=temp,则他们肯定能遇到你 因为他们可以在终点等你。。 但是如果某个人到终点的距离>temp,那么他们肯定不可能在某个时刻和你遇到 因为如果可以在某个时刻与你遇到的话,那他可以接下来跟着你走,那么他到终点的距离肯定是和你到终点的距离是一样的。 而它到终点的距离又大于你到终点的距离 产生了矛盾,所以不可能。

【代码】

#include <bits/stdc++.h>
using namespace std;

const int N = 1000;

int r,c;
int a[N+10][N+10],x0,y0;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
string s[N+10];
queue<pair<int,int> > dl;
int dis[N+10][N+10];

bool bfs(int X,int Y){
    dl.push({X,Y});
    dis[X][Y] = 1;
    while (!dl.empty()){
        int x = dl.front().first,y = dl.front().second;
        dl.pop();
        for (int i = 0;i < 4;i++){
            int tx = x + dx[i];
            int ty = y + dy[i];
            if (tx>=1 && tx<=r && ty>=1 && ty<=c){
                if (a[tx][ty]==0 && dis[tx][ty]==0){
                    dis[tx][ty] = dis[x][y] + 1;
                    dl.push({tx,ty});
                }
            }
        }
    }
}

int main(){
    ios::sync_with_stdio(0),cin.tie(0);
    cin >> r >> c;
    int x1,y1;
    for (int i = 1;i <= r;i++){
        cin >> s[i];
        for (int j = 0;j < c;j++)
            if (s[i][j]=='E'){
                x0 = i;y0 = j + 1;
            }else if (s[i][j]=='T'){
                a[i][j+1] = 1;
            }else if (s[i][j]=='S'){
                x1 = i;y1 = j+1;
            }
    }
    bfs(x0,y0);
    int temp = dis[x1][y1];
    int ans = 0;
    for (int i = 1;i <= r;i++)
        for (int j = 0;j < c;j++)
            if (s[i][j]>='1' && s[i][j]<='9'){
                if (dis[i][j+1]>0 && dis[i][j+1]<=temp){
                    ans+=(s[i][j]-'0');
                }
            }
    cout<<ans<<endl;
    return 0;
}
posted @ 2019-04-10 15:53  AWCXV  阅读(207)  评论(0编辑  收藏  举报