让你将星号改变成点,使得联通的点都是矩形,尽量将更少的星号进行改变。
思路:
1、介个题思路走对了,毛线,一直莫名re/TLE在92,实在是改不动代码了,参考了巨巨的代码~
代码参考自:http://blog.csdn.net/kalilili/article/details/44724351
2、首先通过观察可知,对应一个星号如果周围有三个点,并且这三个点是呈拐弯形式出现的点,那么这个星号一定是需要被改变成点的。e.g:
* | . |
. | . |
3、那么我们将一些星号改变成点之后,会影响其他的星号也能变成点,那么此时我们Dfs处理即可。
Ac代码:
#include<stdio.h>
#include<string.h>
using namespace std;
char a[2010][2012];
int fx[5]={0,1,0,-1,0};
int fy[5]={1,0,-1,0,1};
int n,m;
void Dfs(int x,int y)
{
if(a[x][y]=='*')
{
for(int i=0;i<4;i++)
{
int x1=x+fx[i];
int y1=y+fy[i];
int x2=x+fx[i+1];
int y2=y+fy[i+1];
int x3,y3;
if(fx[i]!=0)x3=x+fx[i];
else x3=x+fx[i+1];
if(fy[i]!=0)y3=y+fy[i];
else y3=y+fy[i+1];
if(a[x1][y1]=='.'&&a[x2][y2]=='.'&&a[x3][y3]=='.')
{
a[x][y]='.';
for(int xx=x-1;xx<=x+1;xx++)
{
for(int yy=y-1;yy<=y+1;yy++)
{
if(xx>=0&&xx<n&&yy>=0&&yy<m)
Dfs(xx,yy);
}
}
return ;
}
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=0;i<n;i++)
{
scanf("%s",a[i]);
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(a[i][j]=='*')
Dfs(i,j);
}
}
for(int i=0;i<n;i++)
{
printf("%s\n",a[i]);
}
}
}