【做练习】The Die Is Cast (搜索)

本文介绍了一种用于识别骰子点数的算法,该算法处理包含多个骰子的图像。首先,通过DFS搜索极大互相连通的非背景像素集合以识别骰子,然后对找到的骰子点像素进行另一轮DFS以计算每个骰子的点数。题目描述了输入图像的格式,并要求按升序输出所有骰子的点数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

大意: 每个case给你一个 h × w h \times w h×w的点阵,表示一幅包含了若干骰子的顶部图像。每个点,也就是像素,有三种可能:

  1. 背景像素
  2. 骰子像素(但不是骰子面上的点)
  3. 骰子点像素

我们认为,一个极大的互相连通非背景像素集合是同一个骰子。此处像素(p1, p2)的互相连通是指:p1和p2之间存在一条路径,p1->…->p2使得两两相邻(斜对角相邻不算),且路径上也都是非背景像素。
同理,一个极大的互相连通的骰子点像素属于同一个骰子点。

现在,你需要计算所给点阵中出现的所有骰子的各自点数,并按照从小到大的方式输出。

描述

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.

输入

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.

输出

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.

样例输入

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

样例输出

Throw 1
1 2 2 4

分析

无向连通图可以直接搜索,而有向连通图使用Tarjan算法。这里是无向连通图,所以是一道憨憨搜索题。稍微不同一点就是,你需要两层不同的dfs。第一个dfs搜连通的非背景点,而当它发现了一个新的骰子点上的像素时,调用第二层dfs。第二层dfs负责搜连通的骰子点像素。


代码

#include
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值