zoukankan      html  css  js  c++  java
  • CodeForces

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    struct node{
        int x,y;
        ll setp;
    };
    queue<node> Q,border[10];
    const int maxn=1005;
    char a[maxn][maxn];
    int vis[maxn][maxn];
    int n,m,p;
    ll ans[10],s[10];
    int f[4][2]={
            {1,0},{0,1},{0,-1},{-1,0}
    };
    void bfs(int w)
    {
        node temp;
        int x,y;
        while(!Q.empty())
        {
            temp=Q.front();
            Q.pop();
            if(temp.setp==0)
                border[w].push(temp);
            else{
                for(int i=0;i<4;i++)
                {
                    x=temp.x+f[i][0];
                    y=temp.y+f[i][1];
                    if(x<1||x>n||y<1||y>m||vis[x][y]!=0||a[x][y]!='.') continue;
                    vis[x][y]=w;
                    Q.push(node{x,y,temp.setp-1});
                }
            }
        }
    }
    bool expand(int w)
    {
        node temp;
        while(!border[w].empty())
        {
            temp=border[w].front();
            border[w].pop();
            temp.setp=s[w];
            Q.push(temp);
        }
        bfs(w);
        return !border[w].empty();
    }
    int main()
    {
        cin>>n>>m>>p;
        for(int i=1;i<=p;i++)
            cin>>s[i];
        for(int i=1;i<=n;i++)
            cin>>a[i]+1;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(a[i][j]>='1'&&a[i][j]<='9')
                {
                    int num=a[i][j]-'0';
                    border[num].push(node{i,j,s[num]});
                    vis[i][j]=num;
                }
            }
        }
        while(1)
        {
            bool flag= false;
            for(int i=1;i<=p;i++)
                flag|=expand(i);
            if(!flag)
                break;
        }
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                ans[vis[i][j]]++;
        for(int i=1;i<=p;i++)
            cout<<ans[i]<<" ";
    }
  • 相关阅读:
    vmware 连网问题
    js控制表单非法输入时提交
    简单的jsp&servlet 购物车项目
    html引入css不显示问题
    每天学点java_反射作用
    java选择特定的值2--抽象enum
    java选择特定的值
    1 小时 SQL 极速入门(一)
    460004600146002MNCMCCIMSI
    网络中存在2台DHCP服务器问题
  • 原文地址:https://www.cnblogs.com/033000-/p/10457144.html
Copyright © 2011-2022 走看看