Biridian Forest(BFS)

题目链接:[http://codeforces.com/problemset/problem/329/B]
分析:

其它的训练师都是坏人,想和你打架,如果你是他们,你会怎么做?
肯定是在必经之路等着,可是必经之路有很多,他们没有瞬移能力,但是有一个位置你必然要到达,就是出口,所以他们只要在出口等你就好了。
也就是说,只要能在你之前或与你同时到达的训练师,都会和你发生战斗。
以终点开始dfs,将早于或与你同时到达的训练师数量全部累加起来就好了。

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

const int maxn = 1100;
const int dx[4] = {0,1,0,-1};
const int dy[4] = {1,0,-1,0};
struct point 
{
    int x,y;
    point (int a = 0, int b = 0)
    {
        x = a, y = b;
    }
    friend bool operator == (const point a,const point b)
    {
        return (a.x==b.x && a.y==b.y);
    }
};
int n,m,ans,d[maxn][maxn];
char Map[maxn][maxn];
bool vis[maxn][maxn];
point e,s;
queue <point> Q;

bool check(int x,int y)
{ 
    if (x>=0 && y>=0 && x<n && y<m)
        if (!vis[x][y] && Map[x][y] != 'T') return true;
   return false;
}

void init()
{
    for (int i=0;i<n;i++)
        for (int j=0;j<m;j++) 
        {
            d[i][j] = 1e8;
            vis[i][j] = 0;
        }
    while (!Q.empty()) Q.pop();
    return;
}

void bfs()
{
    init();
    Q.push(e);
    vis[e.x][e.y] = 1;
    d[e.x][e.y] = 0;
    while (!Q.empty())
    {
        point u = Q.front();
        if (u == s) break;
        for (int k=0;k<4;k++)
        {
            point v(u.x+dx[k],u.y+dy[k]);
            if (check(v.x,v.y))
            {
                vis[v.x][v.y] = 1;
                d[v.x][v.y] = d[u.x][u.y]+1;
                Q.push(v);
            }
        }
        Q.pop();
    }
    return;
}

int getans()
{
    int dis = d[s.x][s.y], ans = 0;
    for (int i=0;i<n;i++)
        for (int j=0;j<m;j++)
        if (d[i][j]<=dis && Map[i][j]>='0' && Map[i][j]<='9')
            ans += Map[i][j]-'0';
    return ans;
}

int main()
{
    scanf("%d %d",&n,&m);
    for (int i=0;i<n;i++) 
    {
        scanf("%s",Map[i]);
        for (int j=0;j<m;j++)
        {
            if (Map[i][j] == 'E') 
            {
                e = point(i,j);
                Map[i][j] = '0';
            }
            if (Map[i][j] == 'S') 
            {
                s = point(i,j);
                Map[i][j] = '0';
            }
        }
    }
    bfs();
    printf("%d\n",getans());
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值