Arthur and Walls CodeForces - 525D (bfs)
大意: 给定格点图, 每个'.'的连通块会扩散为矩形, 求最后图案.
一开始想得是直接并查集合并然后差分, 但实际上是不对的, 这个数据就可以hack掉.
3 3
**.
.**
...
正解是bfs, 一个点被扩散当且仅当它所在的某个2*2块中只有它为'*'.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | #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); } |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步