题目地址:点击打开链接
C++代码:
#include <iostream>
#include <string>
using namespace std;
const int maxsize =100;
string grid[maxsize];
int n;
void dfs(int i,int j)
{
if(i>=0&&i<n&&j>=0&&j<grid[i].size()&&grid[i][j]==' ')
{
grid[i][j]='#';
dfs(i-1,j);
dfs(i+1,j);
dfs(i,j-1);
dfs(i,j+1);
}
}
int main()
{
int m;
cin>>m;
cin.get();//没有这一行就WA,好忧桑!
while(m--)
{
n=0;
while(getline(cin,grid[n]))
{
if(grid[n][0]=='_')
break;
++n;
}
bool flag=true;
for(int i=0;i<n&&flag;++i)
{
for(int j=0;j<grid[i].size()&&flag;++j)
{
if(grid[i][j]=='*')
{
grid[i][j]=' ';
dfs(i,j);
flag=false;
}
}
}
for(int i=0;i<=n;++i)
cout<<grid[i]<<endl;
}
return 0;
}