uva657

题意:在图上找出有多少个色子,还有点数是多少,按大小排序

思路:我们首先先找到色子的面,然后在广搜它的点数 。 。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

char map[60][60];
int vis[60][60];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int w,h,dieNum,dotNum;
struct Node
{
	int x,y;
};
Node que[100000];

void bfs(int x,int y)
{
	int front = 0,rear = 1 ;
	que[0].x = x ;
	que[0].y = y ;
	while (front < rear)
	{
		Node t = que[front++];
		for (int i = 0 ; i < 4 ; i++)
		{
			int dx = t.x + dir[i][0];
			int dy = t.y + dir[i][1];
			if (map[dx][dy] == '*' || map[dx][dy] == '.')
				continue ;
			if (dx >= 0 && dx < h && dy >= 0 && dy < w && map[dx][dy]=='X')
			{
				map[dx][dy] = '@';
				Node temp ;
				temp.x = dx ,temp.y = dy ;
				que[rear++] = temp;
			}
		}
	}
}

void dfs(int x,int y)
{
	for (int i = 0 ; i < 4 ; i++)
	{
		int dx = x + dir[i][0];
		int dy = y + dir[i][1];
		if (map[dx][dy] == 'X')
		{
			dotNum++;
			map[dx][dy] = '@';
			bfs(dx,dy);
		}
		if (map[dx][dy] == '.')
			continue ;
		if (dx >= 0 && dx < h && dy >= 0 && dy < w && !vis[dx][dy] && (map[dx][dy] == '*' || map[dx][dy] == 'X' || map[dx][dy] == '@'))
		{
			vis[dx][dy] = dieNum ;
			dfs(dx,dy);
		}
	}
}
int main()
{
	int cas = 1;
	while (scanf("%d %d%*c",&w,&h)!= EOF && w && h)
	{
		memset(map,0,sizeof(map));
		memset(vis,0,sizeof(vis));

		for (int i = 0 ; i < h ; i++)
			gets(map[i]);

		dieNum = 1;
		vector<int>result ;
		result.clear();
		for (int i = 0 ; i < h ; i++)
			for (int j = 0 ; j < w ; j++)
			{
				if (map[i][j] == '*' && !vis[i][j])
				{
					vis[i][j] = dieNum; 
					dotNum = 0 ;
					dfs(i,j);
					result.push_back(dotNum);
					dieNum++;
				}
			}
		printf("Throw %d\n",cas++);
		sort(result.begin(),result.begin()+result.size());
		if (result[0] != 0)
		{
			printf("%d",result[0]);
			for (int i = 1 ; i < result.size() ; i++)
				printf(" %d",result[i]);
		}
        printf("\n\n");
	}
	return 0 ;
}


你可能感兴趣的:(uva657)