题目链接:点击打开链接
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#include<queue>
int x[5]= {0,0,1,-1};
int y[5]= {1,-1,0,0};
char q[30][30];
int w1[30][30];
struct qq
{
int i1,j1;
} ;
int panduan(int i,int j)
{
int flag=0;
qq a;
a.i1=i;
a.j1=j;
queue<qq>w;
while(!w.empty())
{
w.pop();
}
w.push(a);
w1[i][j]=1;
while(!w.empty())
{
qq a1=w.front();
w.pop();
// w1[a1.i1][a1.j1]=0;
for(int t=0; t<4; t++)
{
qq a2;
a2.i1=a1.i1+x[t];
a2.j1=a1.j1+y[t];
if(q[a1.i1+x[t]][a1.j1+y[t]]=='0')
{
flag=1;
break;
}
else if(q[a1.i1+x[t]][a1.j1+y[t]]=='*')
continue;
else if(q[a1.i1+x[t]][a1.j1+y[t]]=='.')
if(w1[a2.i1][a2.j1]==0)
{
w.push(a2);
w1[a2.i1][a2.j1]=1;
}
}
if(flag==1)
while(!w.empty())
{
w.pop();
}
}
return flag;
}
int main()
{
int a;
scanf("%d",&a);
getchar();
int r=1;
while(a--)
{
int q1,q2;
scanf("%d %d",&q1,&q2);
getchar();
memset(q,'0',sizeof(q));
for(int i=1; i<=q1; i++)
{
for(int j=1; j<=q2; j++)
q[i][j]=getchar();
getchar();
}
for(int i=1; i<=q1; i++)
for(int j=1; j<=q2; j++)
if(q[i][j]=='.')
{
memset(w1,0,sizeof(w1));
if(!panduan(i,j))
q[i][j]='*';
}
printf("Case %d:\n",r);
r++;
for(int i=1; i<=q1; i++)
{
for(int j=1; j<=q2; j++)
printf("%c",q[i][j]);
printf("\n");
}
// printf("%d\n",q[0][0]=='0');
}
return 0;
}