Problem Type : Graph(DFS)
#include<cstdio>
#include<cstring>
#define MAX 100
using namespace std;
char mat[MAX][MAX];
int X[8]= {-1,-1,-1,0,0,1,1,1};
int Y[8]= {-1,0,1,-1,1,-1,0,1};
int m;
void dfs(int i,int j)
{
int vx,vy,k;
mat[i][j]='#';
for(k=0; k<8; k++)
{
vx=i+X[k];
vy=j+Y[k];
if(mat[vx][vy]==' ')
{
dfs(vx,vy);
}
}
}
int main()
{
int test,i,k,j,n,bound,cou;
scanf("%d",&test);
getchar();
while(test--)
{
m=-1;
while(gets(mat[++m]))
if(mat[m][0]=='_')
break;
cou =0;
for(i=0; i<m; i++)
{
int len = strlen(mat[i]);
for(j=0; j<len; j++)
{
if(mat[i][j]=='*')
{
dfs(i,j);
cou=1;
}
if(cou==1)
break;
}
if(cou==1)
break;
}
for(i=0; i<=m; i++)
{
printf("%s\n",mat[i]);
}
}
}
/*
Sample input :
3
XXXXXXXXXXXXXXXXXXXXX
X X X X X X
X X X X
X X X X X X
XXXXXX XXX XXXXXXXXXX
X X X X X X
X X * X
X X X X X X
XXXXXXXXXXXXXXXXXXXXX
_________
XXXXXXXXX
X X X
X * X
X X X
XXXXXXXXX
X X
X X
X X
XXXXX
_____
XXXXX
X X
X * X
X x
XXXXX
_____
*/
#include<cstdio>
#include<cstring>
#define MAX 100
using namespace std;
char mat[MAX][MAX];
int X[8]= {-1,-1,-1,0,0,1,1,1};
int Y[8]= {-1,0,1,-1,1,-1,0,1};
int m;
void dfs(int i,int j)
{
int vx,vy,k;
mat[i][j]='#';
for(k=0; k<8; k++)
{
vx=i+X[k];
vy=j+Y[k];
if(mat[vx][vy]==' ')
{
dfs(vx,vy);
}
}
}
int main()
{
int test,i,k,j,n,bound,cou;
scanf("%d",&test);
getchar();
while(test--)
{
m=-1;
while(gets(mat[++m]))
if(mat[m][0]=='_')
break;
cou =0;
for(i=0; i<m; i++)
{
int len = strlen(mat[i]);
for(j=0; j<len; j++)
{
if(mat[i][j]=='*')
{
dfs(i,j);
cou=1;
}
if(cou==1)
break;
}
if(cou==1)
break;
}
for(i=0; i<=m; i++)
{
printf("%s\n",mat[i]);
}
}
}
/*
Sample input :
3
XXXXXXXXXXXXXXXXXXXXX
X X X X X X
X X X X
X X X X X X
XXXXXX XXX XXXXXXXXXX
X X X X X X
X X * X
X X X X X X
XXXXXXXXXXXXXXXXXXXXX
_________
XXXXXXXXX
X X X
X * X
X X X
XXXXXXXXX
X X
X X
X X
XXXXX
_____
XXXXX
X X
X * X
X x
XXXXX
_____
*/
No comments:
Post a Comment