首页 > 其他 > 详细

UVa 871 - Counting Cells in a Blob

时间:2015-03-10 00:10:28      阅读:381      评论:0      收藏:0      [点我收藏+]

题目:一个01构成的图,求1的最大联通个数(相邻八个方向)。

分析:图论、搜索、floodfill。求解所有区域取最大即可。

说明:注意输出格式╮(╯▽╰)╭。

#include <iostream>
#include <cstdlib>
#include <cstdio>

using namespace std;

char maps[30][30],buf[30];

int dxy[8][2] = {1,-1,1,0,1,1,0,-1,0,1,-1,-1,-1,0,-1,1};
int dfs(int x, int y)
{
	if (x < 0 || y < 0) return 0;
	if (maps[x][y] != '1') return 0;
	maps[x][y] = '0';
	int sum = 1;
	for (int k = 0 ; k < 8 ; ++ k)
		sum += dfs(x+dxy[k][0], y+dxy[k][1]);
	return sum;
}

int main()
{
	int n;
	scanf("%d",&n);
	gets(buf);
	gets(buf);
	while (n --) {
		int count = 0;
		while (gets(maps[count]))
			if(!maps[count ++][0]) break;
		
		int Max = 0;
		for (int i = 0 ; i < count ; ++ i)
		for (int j = 0 ; maps[i][j] ; ++ j)
			if (maps[i][j] == '1')
				Max = max(Max, dfs(i, j));
		
		printf("%d\n",Max);
		if (n) printf("\n");
	}
	
	return 0;
}


UVa 871 - Counting Cells in a Blob

原文:http://blog.csdn.net/mobius_strip/article/details/44164639

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!