标签:
题目:给你一个二维的图片,上面有很多个独立的区域被用‘X‘的边界分隔开,有一些子区域中有填充的字符,
如果发现填充字符,就把这个子区域用这种字符填满(不会出现一个子区域中存在多余一种填充字符)。
分析:图论、搜索,floodfill。如果发现填充字符,直接利用递归填充上下左右方向的格子即可。
说明:本周刷的题好少╮(╯▽╰)╭。
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
char maps[31][81];
int smap[31][81];
void dfs(int x, int y, int n, char c)
{
if (smap[x][y] || maps[x][y] == 'X') return;
maps[x][y] = c;
smap[x][y] = 1;
dfs(x-1, y, n, c);
dfs(x+1, y, n, c);
dfs(x, y-1, n, c);
dfs(x, y+1, n, c);
}
int main()
{
int count = 0;
while (gets(maps[count])) {
while (maps[count][0] != '_')
gets(maps[++ count]);
memset(smap, 0, sizeof(smap));
for (int i = 0; i < count; ++ i)
for (int j = 0; maps[i][j]; ++ j)
if (maps[i][j] != ' ' && maps[i][j] != 'X')
dfs(i, j, count, maps[i][j]);
for (int i = 0; i <= count; ++ i)
puts(maps[i]);
count = 0;
}
return 0;
}标签:
原文地址:http://blog.csdn.net/mobius_strip/article/details/45361299