您的位置:首页 > 其它

UVA - 657 The die is cast(二重dfs)

2014-07-29 18:58 393 查看
The Die Is Cast

Time Limit: 1000MSMemory Limit: 10000KB64bit IO Format: %I64d & %I64u
[Submit] [Go Back] [Status]

Description

InterGames is a high-tech startup company that specializes in developing technology that allows users to play games over the Internet. A market analysis has alerted them to the fact that games of chance are pretty popular among their potential customers. Be
it Monopoly, ludo or backgammon, most of these games involve throwing dice at some stage of the game.

Of course, it would be unreasonable if players were allowed to throw their dice and then enter the result into the computer, since cheating would be way to easy. So, instead, InterGames has decided to supply their users with a camera that takes a picture of
the thrown dice, analyzes the picture and then transmits the outcome of the throw automatically.

For this they desperately need a program that, given an image containing several dice, determines the numbers of dots on the dice.

We make the following assumptions about the input images. The images contain only three different pixel values: for the background, the dice and the dots on the dice. We consider two pixels connected if they share an edge - meeting at a corner is not enough.
In the figure, pixels A and B are connected, but B and C are not.



A set S of pixels is connected if for every pair (a,b) of pixels in S, there is a sequence a1, a2, ..., ak in S such that a = a1 and b = ak , and ai and ai+1 are connected for 1 <= i < k.

We consider all maximally connected sets consisting solely of non-background pixels to be dice. `Maximally connected' means that you cannot add any other non-background pixels to the set without making it dis-connected. Likewise we consider every maximal connected
set of dot pixels to form a dot.

Input

The input consists of pictures of several dice throws. Each picture description starts with a line containing two numbers w and h, the width and height of the picture, respectively. These values satisfy 5 <= w, h <= 50.

The following h lines contain w characters each. The characters can be: "." for a background pixel, "*" for a pixel of a die, and "X" for a pixel of a die's dot.

Dice may have different sizes and not be entirely square due to optical distortion. The picture will contain at least one die, and the numbers of dots per die is between 1 and 6, inclusive.

The input is terminated by a picture starting with w = h = 0, which should not be processed.

Output

For each throw of dice, first output its number. Then output the number of dots on the dice in the picture, sorted in increasing order.

Print a blank line after each test case.

Sample Input

30 15
..............................
..............................
...............*..............
...*****......****............
...*X***.....**X***...........
...*****....***X**............
...***X*.....****.............
...*****.......*..............
..............................
........***........******.....
.......**X****.....*X**X*.....
......*******......******.....
.....****X**.......*X**X*.....
........***........******.....
..............................
0 0


Sample Output

Throw 1
1 2 2 4


题意:

给你一张二维平面图,叫你算出每个色子的点数有多少,并按照升序排序。

注意:

每个色子的面和点数,都只算上下左右连接的面,斜向不算。

输出时注意要留一个空行。

解析:

用一个vector来保存cnt的值

在新建一个标记数组visit[

先for循环一遍,如果遇到'*'就让visit[x][y] = 0,并向周围四个方向搜索,如果遇到'X'就cnt++,然后用第二个dfs搜索'X'周围是否出现'X',如果有就将visit[x][y] = 0

如果cnt > 0 就将 cnt保存在vector中。

最后排序vector,并输出。

#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
int w,h;
const int N = 60;
char grid

;
char visit

;

void dfs1(int x,int y) { //将X周围的所有X都标记为已经访问过
	if(x - 1 >= 0 && grid[x-1][y] == 'X' && visit[x-1][y] == 0) {
		visit[x-1][y] = 1;
		dfs1(x-1,y);
	}
	if(x + 1 < h && grid[x+1][y] == 'X' && visit[x+1][y] == 0) {
		visit[x+1][y] = 1;
		dfs1(x+1,y);
	}
	if(y - 1 >= 0 && grid[x][y-1] == 'X' && visit[x][y-1] == 0) {
		visit[x][y-1] = 1;
		dfs1(x,y-1);
	}
	if(y + 1 < w && grid[x][y+1] == 'X' && visit[x][y+1] == 0) {
		visit[x][y+1] = 1;
		dfs1(x,y+1);
	}
}

void dfs(int x,int y,int &cnt) {
	if(x < 0 || x >= h || y < 0 || y >= w) { //越界
		return ;
	}
	if(grid[x][y] == '*' && visit[x][y] == 0) {
		visit[x][y] = 1;
		dfs(x+1,y,cnt);
		dfs(x-1,y,cnt);
		dfs(x,y+1,cnt);
		dfs(x,y-1,cnt);
	}
	else if(grid[x][y] == 'X' && visit[x][y] == 0) {
		visit[x][y] = 1;
		cnt++;
		dfs1(x,y);
		dfs(x+1,y,cnt);
		dfs(x-1,y,cnt);
		dfs(x,y+1,cnt);
		dfs(x,y-1,cnt);
	}
}

int main() {
	int cas = 1;
	while(scanf("%d%d",&w,&h) != EOF && (w || h)) {
		vector<int> dice;
		//初始化
		memset(grid,0,sizeof(grid));
		memset(visit,0,sizeof(visit));
		for(int i = 0; i < h; i++) {
				scanf("%s",&grid[i]);
		}
		//查找
		int cnt = 0;
		for(int i = 0; i < h; i++) {
			for(int j = 0; j < w; j++) {
				dfs(i,j,cnt);
				if(cnt != 0) {
					dice.push_back(cnt);
					cnt = 0;
				}
			}
		}
		sort(dice.begin(),dice.end());
		printf("Throw %d\n",cas++);
		for(int i = 0;i < dice.size() - 1; i++) {
			printf("%d ",dice[i]);
		}
		printf("%d\n\n",dice.back());
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: