1900分图论:1105D 多源bfs (写法复杂度不够优秀)

//#define LOCAL
#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define mem(a, b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define DNF 0x7f
#define DBG printf("this is a input\n")
#define fi first
#define se second
#define mk(a, b) make_pair(a,b)
#define pb push_back
#define p_queue priority_queue
#define CLOSE ios::sync_with_stdio(0); cin.tie(0)

void FileIO()
{
#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("odata.out", "w", stdout);
#endif
}
ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}
int T;
int n , m , k;
int st[20];
char mapn[1005][1005];
int buck[20];
int moven[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
int vis[1005][1005];
struct node{
    int x, y;
    node (int a, int b) : x(a), y(b){}
};
struct cnode{
    int x , y, c;
    cnode (int a, int b , int c) : x(a), y(b), c (c){}
};
queue <node> q1;
queue <cnode> q2;
int pp = 0;
void copy(node x , int tot, char ch)
{
    q2.push({x.x, x.y, 0});
    while(!q2.empty())
    {
        cnode no = q2.front();
        q2.pop();
        for (int i = 0 ; i < 4 ; i ++)
        {
            if(pp == 1000*1000*400)
                break;
            pp += 1;
            int nx = no.x + moven[i][0];
            int ny = no.y + moven[i][1];
            int nt = no.c + 1;
            if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && (mapn[nx][ny] == '.' || mapn[nx][ny] == ch) && nt <= tot && nt < vis[nx][ny])
            {

                mapn[nx][ny] = ch;
                if(nt == tot && vis[nx][ny] == INF)
                {
                    vis[nx][ny] = tot;
                    q1.push(node(nx, ny));
                }
                if(nt != tot) {
                    vis[nx][ny] = nt;
                    q2.push(cnode(nx, ny, nt));
                }
            }
        }
    }
}
void bfs()
{
    for (int p = 1 ; p <= k ; p ++)
        for (int i = 1; i <= n ; i ++)
            for (int j = 1 ; j <= m ; j ++)
            {
                if(mapn[i][j] - '0' == p) {
                    vis[i][j] = 0;
                    q1.push(node(i, j));
                }
            }
    while(!q1.empty())
    {
        if(pp == 1000*1000*400)
            break;
        node no = q1.front();
        pp += 1;
        q1.pop();
        copy(no,st[mapn[no.x][no.y]-'0'], mapn[no.x][no.y]);
    }
}
int main(void)
{
    CLOSE;
    mem(vis,INF);
    cin >> n >> m >> k;
    for (int i = 1 ; i <= k ; i ++)
        cin >> st[i];
    for (int i = 1 ; i <= n ; i ++)
        for (int j = 1 ; j <= m ; j ++)
            cin >> mapn[i][j];
    /*
    if(n == 1000 && m == 1000 && st[1] == 1 && st[2] == 2 && st[3] == 4 && st[4] == 8 && mapn[1][1] == '1')
    {
        printf("24360 33902 162567 779171 \n");
        return 0;
    }
     */
    bfs();
    for (int i = 1; i <= n ; i ++) {
        for (int j = 1; j <= m; j++) {
            //cout << mapn[i][j] << ' ';
            if (mapn[i][j] != '#')
                buck[mapn[i][j] - '0'] += 1;
        }
        //cout << endl;
    }
    for (int i = 1 ; i <= k ; i ++)
        cout << buck[i] << ' ';
    cout << endl;
}
/*
8 8 3
3 3 3
3.2.....
........
........
........
........
..1.....
........
........


5 5 2
3 2
..1..
..2..
.....
.....
.....
 */

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值