Kilani and the Game CodeForces - 1105D (bfs)

沙茶bfs打了2小时... queue入队量太大了, 放函数里直接T了, 改成全局46ms

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head

const int N= 1e3+10;
int n, m, p;
struct _ {
	int x,y,v;
};
const int dx[]={1,-1,0,0};
const int dy[]={0,0,1,-1};
queue<_> q[20], Q;
int a[20], vis[N][N], ans[20];
char s[N][N];

int expand(int id) {
	while (!q[id].empty()) { 
		_ t = q[id].front();q[id].pop();
		t.v = a[id];
		Q.push(t);
	}
	int ok = 0;
	while (!Q.empty()) {
		_ t = Q.front();Q.pop();
		if (!t.v) {
			q[id].push(t);
			continue;
		}
		REP(i,0,3) {
			int x=t.x+dx[i],y=t.y+dy[i],v=t.v-1;
			if (x<=0||y<=0||x>n||y>m||vis[x][y]||s[x][y]=='#') continue;
			ok = 1;
			vis[x][y] = id;
			Q.push({x,y,v});
		}
	}
	return ok;
}

int main() {
	scanf("%d%d%d", &n, &m, &p);
	REP(i,1,p) scanf("%d", a+i);
	REP(i,1,n) scanf("%s", s[i]+1);
	REP(i,1,n) REP(j,1,m) {
		if (isdigit(s[i][j])) {
			int ii = s[i][j]-'0';
			vis[i][j]=ii;
			q[ii].push({i,j,a[i]});
		}
	}
	while (1) {
		int ok = 0;
		REP(i,1,p) ok |= expand(i);
		if (!ok) break;
	}
	REP(i,1,n) REP(j,1,m) ++ans[vis[i][j]];
	REP(i,1,p) printf("%d ",ans[i]);hr;
}

猜你喜欢

转载自www.cnblogs.com/uid001/p/10503861.html