UVA 657 The die is cast
| The die is cast |
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 dif- ferent 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
in S such that a = a1 and b = ak, and ai and ai+1 are connected for
.
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
.
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
题意: 给定一个图片。。图片上有有一些骰子的图形。。骰子点数用'X'表示,骰子背景为'*', 其余背景为'.' , 如果X是连在一起的为1点。。
思路: 嵌套的搜索。。遇到'*' 然后去搜‘X’。。把连在的'X'转换成‘*’,点数+1,再在把连在一起的*转换成‘.',这样一个骰子就搜索出来了。。注意‘X’一定要先转换成‘*'不能直接变成'.'不然假如遇到这种情况
*X*X。。第一次搜索过去 X 变成'.'就把一个骰子分割成2个骰子了。。就会错。。
#include <stdio.h>
#include <string.h> int d[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int n, m;
int num[7];
int i, j;
int sum;
char map[55][55];
int vis[55][55];
int ju;
void jud(int x, int y)
{
if (map[x][y] == '*')
{
ju = 1;
return;
}
if (map[x][y] == '.')
return;
int i;
vis[x][y] = 1;
for (i = 0; i < 4; i ++)
{
if (vis[x + d[i][0]][y + d[i][1]] == 0)
jud(x + d[i][0], y + d[i][1]);
}
}
void bfs2(int x, int y)
{
int i;
map[x][y] = '*';
for (i = 0; i < 4; i ++)
{
if (map[x + d[i][0]][y + d[i][1]] == 'X')
{
bfs2(x + d[i][0], y + d[i][1]);
}
}
}
void bfs(int x, int y)
{
int i;
map[x][y] = '.';
for (i = 0; i < 4; i ++)
{
if (map[x + d[i][0]][y + d[i][1]] == 'X')
{
bfs2(x + d[i][0], y + d[i][1]);
sum ++;
}
if (map[x + d[i][0]][y + d[i][1]] == '*')
{
bfs(x + d[i][0], y + d[i][1]);
}
}
}
int main()
{
int tt = 1;
while (~scanf("%d%d", &m, &n) && n && m)
{
getchar();
memset(map, '.' ,sizeof(map));
memset(num, 0 ,sizeof(num));
for (i = 1; i <= n; i ++)
{
for (j = 1; j <= m; j ++)
scanf("%c", &map[i][j]);
getchar();
}
for (i = 1; i <= n; i ++)
{
for (j = 1; j <= m; j ++)
{
if (map[i][j] == '*')
{
sum = 0;
bfs(i, j);
num[sum] ++;
}
else if (map[i][j] == 'X')
{
ju = 0;
memset(vis, 0, sizeof(vis));
jud(i, j);
if (ju == 0)
{
bfs2(i, j);
num[1] ++;
}
}
}
}
int bo = 0;
int judge = 0;
printf("Throw %d\n", tt ++);
for (i = 1; i <= 6; i ++)
{
while (num[i])
{
if (bo == 0)
{
judge = 1;
printf("%d", i);
num[i] --;
bo = 1;
}
else
{
printf(" %d", i);
num[i] --;
}
}
}
if (judge == 0)
printf("0");
printf("\n\n");
}
return 0;
}
UVA 657 The die is cast的更多相关文章
- UVa657 The die is cast
// 题意:给一个图案,其中'.'表示背景,非'.'字符组成的连通块为筛子.每个筛子里又包含两种字符,其中'X'组成的连通块表示筛子上的点 // 统计每个筛子里有多少个"X"连通块 ...
- The Die Is Cast(poj 1481简单的双dfs)
http://poj.org/problem?id=1481 The Die Is Cast Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- UVa 657 掷骰子
意甲冠军:有一个大图.每个像素是格孩子只可能是 . * X 三种.代表背景.玻色子.色子点. 两格子是邻近或在通信,当且仅当两个格儿子*要么X.且具有共同的边,这是上下左右四个方向,斜过,即四连块. ...
- uva 657
很简单的题,就是题意不懂……! 就是判断每个'*'区域内‘X’区域块的个数 WA了好多次,就是太差了: 1.结果排序输出 2.因为是骰子所以不再1-6范围内的数字要舍弃 3.格式要求要空一行…… 4. ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- 10409 - Die Game
Problem G: Die Game Life is not easy. Sometimes it is beyond your control. Now, as contestants of AC ...
- words2
餐具:coffee pot 咖啡壶coffee cup 咖啡杯paper towel 纸巾napkin 餐巾table cloth 桌布tea -pot 茶壶tea set 茶具tea tray 茶盘 ...
- Android解析服务器Json数据实例
Json数据信息如下: { "movies": [ { "movie": "Avengers", "year": 201 ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
随机推荐
- javascript 返回顶部
<style> #linGoTopBtn { POSITION: fixed; TEXT-ALIGN: center; LINE-HEIGHT: 30px; WIDTH: 30px; ...
- WinForm TreeView 三种状态
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { var node = e.N ...
- HTML5 的绘图支持- canvas
Canvas HTML5新增了一个canvas元素,它是一张空画布,开发者需要通过JavaScript脚本进行绘制. 在canvas上绘图,经过如下3步 (1) 获取canvas元素对应的DOM对象. ...
- 调度思路+EurekaServer获得当前机器的instanceid
调度思路 概念 Build 一次完整的构建 整个流水线 Task(BuidStep) 流水线中的某一个步骤单元 先假设对于一个Build(流水线)而言里面所有Task是串行执行的 并且各Task之间不 ...
- node-mongodb-native的几种连接数据库的方式
h1,h2,h3,h4,h5,h6,p,blockquote { margin: 0; padding: 0;}body { font-family: "Helvetica Neue&quo ...
- C#中的反射 Assembly.Load() Assembly.LoadFrom()
一些关于C#反射的知识,估计也就最多达到使用API的程度,至于要深入了解,以现在的水平估计很难做到,所以下面此篇文章,以作为一个阶段的总结. 对于反射的总结,我想从以下几个方面展开,首先是反射程序集, ...
- skip index scan
官网对skip index scan的解释: Index skip scans improve index scans by nonprefix columns since it is often f ...
- 关于ADMM的研究(一)
关于ADMM的研究(一) 最近在研究正则化框架如何应用在大数据平台上.找到了<Distributed Optimization and Statistical Learning via the ...
- android recovery模式及ROM制作
转自android recovery模式及ROM制作 1.总述 为了方便客户日后的固件升级,本周研究了一下android的recovery模式.网上有不少这类的资料,但都比较繁杂,没有一个系统的介绍与 ...
- Web-Scale IT 我之见!
Gartner 曾在发表过的一篇文章中表示,到2017年,全球50%的企业将使用Web-Scale IT 架构.下面我们来看看 Andre Leibovici 对 Web-Scale IT 的看法: ...