Arthur and Walls CodeForces - 525D (bfs)
大意: 给定格点图, 每个'.'的连通块会扩散为矩形, 求最后图案.
一开始想得是直接并查集合并然后差分, 但实际上是不对的, 这个数据就可以hack掉.
3 3
**.
.**
...
正解是bfs, 一个点被扩散当且仅当它所在的某个2*2块中只有它为'*'.
#include <iostream> #include <iostream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #include <map> #include <queue> #include <string> #include <string.h> #include <bitset> #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' #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;}) 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;} inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;} //head #ifdef ONLINE_JUDGE const int N = 1e6+10; #else const int N = 111; #endif int n, m; char s[N][N]; int dx[]={-1,-1,-1,0,1,1,1,0}; int dy[]={-1,0,1,1,1,0,-1,-1}; queue<pii> q; int chk(int x, int y) { if (x<1||x>n||y<1||y>m||s[x][y]=='.') return 0; if (s[x-1][y]=='.'&&s[x-1][y-1]=='.'&&s[x][y-1]=='.') return 1; if (s[x-1][y]=='.'&&s[x-1][y+1]=='.'&&s[x][y+1]=='.') return 1; if (s[x][y-1]=='.'&&s[x+1][y-1]=='.'&&s[x+1][y]=='.') return 1; if (s[x][y+1]=='.'&&s[x+1][y]=='.'&&s[x+1][y+1]=='.') return 1; return 0; } int main() { scanf("%d%d", &n, &m); REP(i,1,n) scanf("%s", s[i]+1); REP(i,1,n) REP(j,1,m) if (chk(i,j)) q.push(pii(i,j)); while (q.size()) { auto t = q.front(); q.pop(); if (!chk(t.x,t.y)) continue; s[t.x][t.y] = '.'; REP(i,0,7) { int x=t.x+dx[i],y=t.y+dy[i]; if (chk(x,y)) q.push(pii(x,y)); } } REP(i,1,n) puts(s[i]+1); }