题目大意:给一个图形,每个图形中用字母分为一块一块,如果一块中有‘*’,即该块内容都填补为‘#’。每个图形下面有下划线作为分割,输出时也要有。
解题思路:类似迷宫问题,将所有‘*’号读入队列,并转化字符。然后bfs。(不看题目坑死人。。。)
ac代码:
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
queue <int>qu;
int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
int n, le, wi, vis[1005][1005];
char a[1005][1005];
void bfs()
{
while (!qu.empty()){
int x, y, len;
x = qu.front();
qu.pop();
y = qu.front();
qu.pop();
for (int i=0; i<4; i++){
int tt, kk;
tt = x + dx[i];
kk = y + dy[i];
len = strlen(a[tt]);
if (tt >= 0 && tt < le && kk >= 0
&& kk < len && vis[tt][kk] == -1){
a[tt][kk] = '#';
qu.push(tt);
qu.push(kk);
vis[tt][kk] = 0;
}
}
}
}
int main()
{
scanf("%d", &n);
getchar();
while (n--){
for (int i=0; ;i++){
gets(a[i]);
if (a[i][0] == '_'){
le = i;
break;
}
}
memset(vis, -1, sizeof(vis));
while (!qu.empty())
qu.pop();
for (int i=0; i<le; i++){
wi = strlen(a[i]);
for (int j=0; j<wi; j++){
if (isalpha(a[i][j]))
vis[i][j] = 0;
else if (a[i][j] == '*')
a[i][j] = '#', qu.push(i), qu.push(j);
}
}
bfs();
for (int i=0; i<le; i++)
printf("%s\n", a[i]);
printf("%s\n", a[le]);
}
return 0;
}