Problem Type : Graph(DFS)
#include<stdio.h>
#include<string.h>
#define MAX1 31
#define MAX 81
char mat[MAX1][MAX],c,contour;
int X[8]= {-1,0,0,1};
int Y[8]= {0,-1,1,0};
int color[MAX1][MAX];
void dfs(int i,int j)
{
int vx,vy,k;
if(color[i][j]==0)
{
mat[i][j]=c;
color[i][j]=1;
}
for(k=0; k<8; k++)
{
vx=i+X[k];
vy=j+Y[k];
if(mat[vx][vy]==' ')
{
dfs(vx,vy);
}
}
}
int main()
{
int i,j,k,len,m=-1;
while(gets(mat[0]))
{
if(mat[0][0]=='_')
{
break;
}
m=0;
while(gets(mat[++m]))
{
if(mat[m][0]=='_')
break;
}
int cou =0;
int cou1=0;
for(i=0; i<m; i++)
{
len=strlen(mat[i]);
for(j=0; j<len; j++)
{
if(mat[i][j]!=' ' && mat[i][j]!='_')
{
contour=mat[i][j];
cou=1;
}
if(cou=1)
break;
}
if(cou=1)
break;
}
for(i=0; i<m; i++)
{
len=strlen(mat[i]);
for(j=0; j<len; j++)
{
if(mat[i][j]!=' ' && mat[i][j]!=contour && mat[i][j]!='_' && color[i][j]==0)
{
c=mat[i][j];
dfs(i,j);
}
}
}
for(i=0; i<=m; i++)
{
printf("%s\n",mat[i]);
}
memset(color,'\0',sizeof(color));
}
return 0;
}
/*
XXXXXXXXXXXXXXXXXXXX
X X X
X # # XXXXXXXX / X
X X X
XXXXXXXXXXXXXXXXXXXX
_____________________________
XXXXXXXXXXXX XXXXXX
X # XXX XXX X X
X XX X X X X
X X X XXXXXXX XXXXXXX
X XX X
X X XXXX XXXXXXXX
XX XXXX X X / X
X X X / X
XXXXXXXXXXXXX XXXXXXXX
_____________________________
*/
#include<stdio.h>
#include<string.h>
#define MAX1 31
#define MAX 81
char mat[MAX1][MAX],c,contour;
int X[8]= {-1,0,0,1};
int Y[8]= {0,-1,1,0};
int color[MAX1][MAX];
void dfs(int i,int j)
{
int vx,vy,k;
if(color[i][j]==0)
{
mat[i][j]=c;
color[i][j]=1;
}
for(k=0; k<8; k++)
{
vx=i+X[k];
vy=j+Y[k];
if(mat[vx][vy]==' ')
{
dfs(vx,vy);
}
}
}
int main()
{
int i,j,k,len,m=-1;
while(gets(mat[0]))
{
if(mat[0][0]=='_')
{
break;
}
m=0;
while(gets(mat[++m]))
{
if(mat[m][0]=='_')
break;
}
int cou =0;
int cou1=0;
for(i=0; i<m; i++)
{
len=strlen(mat[i]);
for(j=0; j<len; j++)
{
if(mat[i][j]!=' ' && mat[i][j]!='_')
{
contour=mat[i][j];
cou=1;
}
if(cou=1)
break;
}
if(cou=1)
break;
}
for(i=0; i<m; i++)
{
len=strlen(mat[i]);
for(j=0; j<len; j++)
{
if(mat[i][j]!=' ' && mat[i][j]!=contour && mat[i][j]!='_' && color[i][j]==0)
{
c=mat[i][j];
dfs(i,j);
}
}
}
for(i=0; i<=m; i++)
{
printf("%s\n",mat[i]);
}
memset(color,'\0',sizeof(color));
}
return 0;
}
/*
XXXXXXXXXXXXXXXXXXXX
X X X
X # # XXXXXXXX / X
X X X
XXXXXXXXXXXXXXXXXXXX
_____________________________
XXXXXXXXXXXX XXXXXX
X # XXX XXX X X
X XX X X X X
X X X XXXXXXX XXXXXXX
X XX X
X X XXXX XXXXXXXX
XX XXXX X X / X
X X X / X
XXXXXXXXXXXXX XXXXXXXX
_____________________________
*/
No comments:
Post a Comment