UVa 10946 - You want what filled?

题目:统计一个矩阵中所有的连通块的个数,按递减输出。

分析:搜索,floodfill。直接利用dfs求解即可。

说明:数组开小了╮(╯▽╰)╭。

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>

using namespace std;

char maps[55][55];

typedef struct tnode
{
	char ch;
	int  count;
}type;
type data[2000];

int dfs(int x, int y, int n, int m, char c)
{
	if (x < 0 || x >= n || y < 0 || y >= m)
		return 0;
	if (maps[x][y] != c) return 0;
	maps[x][y] = '.';
	int sum = 1;
	sum += dfs(x+1, y, n, m, c);
	sum += dfs(x-1, y, n, m, c);
	sum += dfs(x, y+1, n, m, c);
	sum += dfs(x, y-1, n, m, c);
	return sum;
}

int cmp(type a, type b)
{
	if (a.count != b.count)
		return a.count > b.count;
	return a.ch < b.ch;
}

int main()
{
	int n,m,temp,cas = 1;
	while (~scanf("%d%d",&n,&m) && m) {
		for (int i = 0 ; i < n ; ++ i)
			scanf("%s",maps[i]);

		int count = 0;
		for (int i = 0 ; i < n ; ++ i)
		for (int j = 0 ; j < m ; ++ j)
			if (maps[i][j] != '.') {
				data[count].ch = maps[i][j];
				data[count].count = dfs(i, j, n, m, maps[i][j]);
				count ++;
			}

		sort(data, data+count, cmp);

		printf("Problem %d:\n",cas ++);
		for (int i = 0 ; i < count ; ++ i)
			printf("%c %d\n",data[i].ch,data[i].count);
	}
	return 0;
}
时间: 2025-01-07 00:56:26

UVa 10946 - You want what filled?的相关文章

UVA 532 Dungeon Master

题目如下: Dungeon Master  You are trapped in a 3D dungeon and need to find the quickest way out!The dungeon is composedof unit cubes which may or may not be filled with rock. It takes one minuteto move one unit north,south, east, west, up or down. You ca

UVA 297 Quadtrees(四叉树建树、合并与遍历)

<span style="font-size: 18pt; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">K - </span><span style="color: blue; font-size: 18pt; font-family: Arial, Helvetica, sans-serif; background-color

uva 10603 Fill (BFS)

uva 10603 Fill There are three jugs with a volume of a, b and c liters. (a, b, and c are positive integers not greater than 200). The first and the second jug are initially empty, while the third is completely filled with water. It is allowed to pour

UVA - 11774 Doom&#39;s Day (规律)

We all know about the legend oftower of Hanoi. It is said that the world will end after finishing the puzzle.What we don't know is another legend about when the world will end which is verifiedby the scientists. It is all about a 3^n * 3^m grid.Initi

UVA The Sultan&#39;s Successors

题目如下: The Sultan's Successors  The Sultan of Nubia has no children, so she has decided that thecountry will be split into up to k separate parts on her death andeach part will be inherited by whoever performs best at some test. Itis possible for any

UVA FILL(BFS + 优先队列)

Problem D FILL There are three jugs with a volume of a, b and c liters. (a, b, and c are positive integers not greater than 200). The first and the second jug are initially empty, while the third is completely filled with water. It is allowed to pour

Unix ls UVA 400

说说:这道题在开始的时候理解有偏差.其实这道题把unix里的ls命令简化了,这道题的输出格式其实是输出若干列的文件名,且每列占据的字符数是最长的文件名的长度加二,当然最后一列除外,最后一列就是最长的文件名的长度.其实只要将行数从一开始不断的测试,直到输出每行的字符数不超过六十即可.但是我开始理解成要求输出的行数最少,且每列的长度为该列中的最长的文件名的长度加二.这样的话就变得复杂了许多.因为你在计算最少的行数的时候就要统计此种情况下的每列的最长字符数,从而判断每行总的字符数是否会超过六十.原本打

uva 297 - Quadtrees

 Quadtrees  A quadtree is a representation format used to encode images. The fundamental idea behind the quadtree is that any image can be split into four quadrants. Each quadrant may again be split in four sub quadrants, etc. In the quadtree, the im

uva 11520 Fill the Square(枚举)

uva 11520 Fill the Square In this problem, you have to draw a square using uppercase English Alphabets. To be more precise, you will be given a square grid with some empty blocks and others already filled for you with some letters to make your task e