Codeforces Round #533 (Div. 2) D. Kilani and the Game(BFS)

题意:

n x m 的网格,p 个玩家轮流BFS,给出每个玩家每次能遍历的最远距离和网格初始状态(可遍历点、障碍点、每个玩家的遍历起点(可多个)),问每个玩家最多能遍历多少点。

Tips:

以所有玩家无法再进行遍历而不是已遍历所有点为终止条件。

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

const int M=1100;

int n,m,p,max_dep[M],ans[M];

char mp[M][M];
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};

struct P{
    int x,y,dep;
    P(int a,int b,int c){x=a,y=b,dep=c;}//构造函数
};

queue<P> q[M];

bool not_all_empty(){
    for(int i=1;i<=p;i++)
        if(q[i].size()) return true;
    return false;
}

void bfs(int i){
    queue<P> next;

    while(!q[i].empty()){
        P t=q[i].front();
        q[i].pop();

        for(int j=0;j<4;j++){
            int x=t.x+dir[j][0];
            int y=t.y+dir[j][1];
            int dep=t.dep+1;

            if(mp[x][y]=='.'){
                mp[x][y]=i+'0';
                if(dep<max_dep[i]) q[i].emplace(x,y,dep);
                else next.emplace(x,y,0);
                ++ans[i];
            }
        }
    }

    while(!next.empty()){
        P t=next.front();
        next.pop();
        q[i].push(t);
    }
}

int main()
{
    cin>>n>>m>>p;
    for(int i=1;i<=p;i++) cin>>max_dep[i];
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++) cin>>mp[i][j];

    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            if(isdigit(mp[i][j])){
                int k=mp[i][j]-'0';
                q[k].emplace(i,j,0);
                ++ans[k];
            }

    while(not_all_empty())
        for(int i=1;i<=p;i++)
            if(q[i].size()) bfs(i);

    for(int i=1;i<=p;i++)
        cout<<ans[i]<<' ';

    return 0;
}

 

posted @ 2020-03-16 17:50  Kanoon  阅读(118)  评论(0编辑  收藏  举报