简单的dfs应用
#include <stdio.h>
#include <string.h>
char arr[35][90];
char buffer[100];
int total_row;
void dfs(int i, int j)
{
if( !(i>=1 && i<=total_row) )
return;
if( !(j>=1 && j<=strlen(arr[i]+1)) )
return;
if(arr[i][j]=='X' || arr[i][j]=='#')
return;
arr[i][j] = '#';
dfs(i-1, j);
dfs(i+1, j);
dfs(i, j-1);
dfs(i, j+1);
}
//len是矩阵的行数,m和n是×的行数和列数
void func(int len, int m, int n)
{
total_row = len;
dfs(m, n);
for(int i=1; i<=len+1; i++)
printf("%s\n", arr[i]+1);
}
int main(void)
{
int N, i, j;
int count, _count;
int m, n;
char *p;
//freopen("input.dat", "r", stdin);
scanf("%d", &N);
getchar();
for(i=1; i<=N; i++)
{
count = 1;
while(1)
{
gets(buffer);
if(p=strstr(buffer, "*"))
{
m = count;
n = p-buffer+1;
//printf("m=%d n=%d\n", m, n);
}
strcpy(arr[count++]+1, buffer);
_count = 0;
for(j=0; j<strlen(buffer); j++)
if(buffer[j] == '_')
_count ++;
if(_count == strlen(buffer))
break;
}
func(count-2, m, n);
}
return 0;
}