图的dfs或是BFS , 不难 , 以下是代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int size_row = 30 + 5;
const int size_col = 80 + 5;
char s[size_row][size_col];
int d[][2] = { {0 , -1} , {-1 , 0} , {0 , 1} , {1 , 0}};
void dfs(int x , int y)
{
s[x][y] = '#';
for(int i = 0 ; i < 4 ; ++i)
{
int tx = x + d[i][0];
int ty = y + d[i][1];
if(s[tx][ty] == ' ') dfs(tx , ty);
}
}
int main()
{
int n;
scanf("%d" , &n); getchar();
for(int i = 0 ; i < n ; ++i)
{
memset(s , 0 , sizeof(s));
int total = 0;
while(fgets(s[total] , size_col , stdin) && s[total][0] != '_')
{
++total;
}
for(int j = 0 ; j < total ; ++j)
{
for(int k = 0 ; k < size_col ; ++k)
{
if(s[j][k] == '*') dfs(j , k);
}
}
for(int j = 0 ; j <= total ; ++j)
printf("%s" , s[j]);
}
return 0;
}