每个2*2的格子中如果有一个*的话,则必需要删掉这个*。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<iostream>
using namespace std;
char g[2005][2005];
struct NODE
{
int x,y;
};
queue<NODE>q;
int n,m;
bool check(int x,int y)
{
int i,j,ct=0;
if(x<1 || x>=n || y<1 || y>=m) return 0;
for(i=x;i<x+2;i++){
for(j=y;j<y+2;j++){
if(g[i][j]=='.') ct++;
}
}
if(ct==3) return 1;
else return 0;
}
bool solve(int x,int y)
{
if(x<1 || x>n || y<1 ||y>m) return 0;
return check(x-1,y-1) || check(x-1,y) || check(x,y-1) || check(x,y) ;
}
bool vis[2005][2005];
int ans=0;
int dx[]={0,0,1,-1,1,-1,1,-1};
int dy[]={1,-1,0,0,-1,1,1,-1};
void bfs()
{
NODE nw,nxt;
while(!q.empty()){
nw=q.front(); q.pop();
ans++;
int i;
for(i=0;i<8;i++){
nxt.x=nw.x+dx[i];
nxt.y=nw.y+dy[i];
if(nxt.x<1 || nxt.x>n || nxt.y<1 || nxt.y>m) continue;
if(vis[nxt.x][nxt.y]) continue;
if(g[nxt.x][nxt.y]=='.'){
q.push(nxt);
vis[nxt.x][nxt.y]=1;
}
if(solve(nxt.x,nxt.y) && g[nxt.x][nxt.y]=='*'){
q.push(nxt);
vis[nxt.x][nxt.y]=1;
g[nxt.x][nxt.y]='.';
}
}
}
}
int main()
{
int i,j;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++){
scanf("%s",g[i]+1);
}
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
if(g[i][j]=='*'){
if(solve(i,j))
{
NODE tmp;
tmp.x=i; tmp.y=j;
q.push(tmp);
g[i][j]='.';
vis[tmp.x][tmp.y]=1;
}
}
}
}
bfs();
//cout<<ans<<endl;
for(i=1;i<=n;i++){
puts(g[i]+1);
}
return 0;
}