判断2*2的正方形,是不是3个"."1个"*"然后暴力bfs就好。(这种处理也是挺神奇的2333%%题解)
1 #include<bits/stdc++.h> 2 #define INF 0x7fffffff 3 #define LL long long 4 #define N 100005 5 #define pill pair<int ,int > 6 using namespace std; 7 inline int ra() 8 { 9 int x=0,f=1; char ch=getchar(); 10 while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();} 11 while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} 12 return x*f; 13 } 14 const int dir_x[4][3] = {{1,0,1},{0,-1,-1},{-1,-1,0},{1,1,0}}; 15 const int dir_y[4][3] = {{0,1,1},{1,0,1},{0,-1,-1},{0,-1,-1}}; 16 char mat[2005][2005]; 17 int n,m; 18 void bfs() 19 { 20 queue<pair<int ,int > > q; 21 for (int i=0; i<n; i++) 22 for (int j=0; j<m; j++) 23 if (mat[i][j]=='.') q.push(make_pair(i,j)); 24 while (!q.empty()) 25 { 26 pair<int , int > now=q.front(); q.pop(); 27 int x=now.first,y=now.second; 28 int pos_x,pos_y,cnt; 29 for (int i=0; i<4; i++) 30 { 31 cnt=0; 32 int ok=1; 33 for (int j=0; j<3; j++) 34 { 35 int xx=x+dir_x[i][j]; 36 int yy=y+dir_y[i][j]; 37 if (xx>=0 && xx<n && yy>=0 && yy<m) 38 { 39 if (mat[xx][yy]=='*') 40 { 41 pos_x=xx; pos_y=yy; 42 cnt++; 43 } 44 } 45 else { 46 ok=0; 47 break; 48 } 49 } 50 if (ok && cnt==1) 51 { 52 mat[pos_x][pos_y]='.'; 53 q.push(make_pair(pos_x,pos_y)); 54 } 55 } 56 } 57 return; 58 } 59 60 void print() 61 { 62 for (int i=0; i<n; i++) 63 puts(mat[i]); 64 } 65 int main() 66 { 67 scanf("%d%d",&n,&m); 68 for (int i=0; i<n; i++) 69 scanf("%s",mat[i]); 70 bfs(); 71 print(); 72 return 0; 73 }