Codeforces Round #297 (Div. 2) D題. Arthur and Walls(BFS),
題目地址:Arthur and Walls
這題有一個腦洞,對於當前的點(i,j)並且此點為”*”來說,若存在包含它的2*2正方形中除了它自己外,另外三個點都是”.”,那麼這個點就必須要變成”.”。由於去掉這個點之後會對周圍的8個點造成影響,所以可以用BFS去搜。WA第12組的應該是只考慮了會影響到周圍的4個點了。
代碼如下:
#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace std;#define LL __int64#define pi acos(-1.0)#pragma comment(linker, "/STACK:1024000000")const int mod=1e9+7;const int INF=0x3f3f3f3f;const double eqs=1e-9;const int MAXN=40000+10;char mp[2015][2015];int n, m;int jx[]={0,0,1,-1,1,-1,1,-1};int jy[]={1,-1,0,0,1,-1,-1,1};bool check(int x, int y){ if(x<0||x>n||y<0||y>m) return false; if(mp[x][y]=='.') return true; return false;}bool Judge(int x, int y){ if(mp[x][y]!='*') return false; if(check(x+1,y)&&check(x+1,y+1)&&check(x,y+1)) return true; if(check(x+1,y)&&check(x+1,y-1)&&check(x,y-1)) return true; if(check(x-1,y)&&check(x-1,y+1)&&check(x,y+1)) return true; if(check(x,y-1)&&check(x-1,y-1)&&check(x-1,y)) return true; return false;}queue<pair<int,int> >q;void bfs(){ int i, x, y, a, b; while(!q.empty()){ x=q.front().first;y=q.front().second; q.pop(); if(mp[x][y]=='.') continue ; mp[x][y]='.'; for(i=0;i<8;i++){ a=x+jx[i]; b=y+jy[i]; if(a>=0&&a<n&&b>=0&&b<m&&Judge(a,b)){ q.push(make_pair(a,b)); } } }}int main(){ int i, j; while(scanf("%d%d",&n,&m)!=EOF){ for(i=0;i<n;i++){ scanf("%s",mp[i]); } for(i=0;i<n;i++){ for(j=0;j<m;j++){ if(Judge(i,j)) q.push(make_pair(i,j)); } } bfs(); for(i=0;i<n;i++){ printf("%s\n",mp[i]); } } return 0;}