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的更多相关文章

  1. UVa657 The die is cast

    // 题意:给一个图案,其中'.'表示背景,非'.'字符组成的连通块为筛子.每个筛子里又包含两种字符,其中'X'组成的连通块表示筛子上的点 // 统计每个筛子里有多少个"X"连通块 ...

  2. 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 ...

  3. UVa 657 掷骰子

    意甲冠军:有一个大图.每个像素是格孩子只可能是 . * X 三种.代表背景.玻色子.色子点. 两格子是邻近或在通信,当且仅当两个格儿子*要么X.且具有共同的边,这是上下左右四个方向,斜过,即四连块. ...

  4. uva 657

    很简单的题,就是题意不懂……! 就是判断每个'*'区域内‘X’区域块的个数 WA了好多次,就是太差了: 1.结果排序输出 2.因为是骰子所以不再1-6范围内的数字要舍弃 3.格式要求要空一行…… 4. ...

  5. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  6. 10409 - Die Game

    Problem G: Die Game Life is not easy. Sometimes it is beyond your control. Now, as contestants of AC ...

  7. words2

    餐具:coffee pot 咖啡壶coffee cup 咖啡杯paper towel 纸巾napkin 餐巾table cloth 桌布tea -pot 茶壶tea set 茶具tea tray 茶盘 ...

  8. Android解析服务器Json数据实例

    Json数据信息如下: { "movies": [ { "movie": "Avengers", "year": 201 ...

  9. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

随机推荐

  1. web系列教程之php 与mysql 动态网站 。检索 与更新。

    接着上次WEb 系列开发之php 与mysql动态网站入门. 个人觉得,学习技术就像一棵大树,主干很重要,枝叶其次.对于学习技术,我们应该分清主次关系.怎么学?为什么要学?有一个较好的分寸. 有时候觉 ...

  2. zzuli oj 1165 实数的小数部分(指针专题)

    Description 读入一个实数,输出该实数的小数部分,小数部分若多余的末尾0,请去掉.如输入111111.12345678912345678900  则输出0.12345678912345678 ...

  3. STM32下载方法

    一.JLINK下载方法 1.硬件设置 Boot0,Boot1 = 0,*(测试通过) Boot0,Boot1 = 1,0或者0,1(未测试) 2.软件设置 MDK设置 ① 选择Project -> ...

  4. 根据WSDL生成代理类方式(2)

    运行开发人员工具提示 输入命令行svcutil http://localhost:8080/Test/TestClassPort?wsdl

  5. HttpWebRequest提高效率之连接数,代理,自动跳转,gzip请求等设置问题

    先设置4个: [csharp] webrequest.ServicePoint.Expect100Continue = false; //是否使用 Nagle 不使用 提高效率 webrequest. ...

  6. AD RMS Bulk Protection Tool 批量加密解密office文档

    1.Active Directory Rights Management Services Bulk Protection Tool  http://www.microsoft.com/zh-cn/d ...

  7. 关于MATLAB中的tic toc的问题

    关于MATLAB中的tic toc的问题 其一) MATLAB实际单位时间计时函数的具体应用,在编写程序时,经常需要获知代码的执行实际时间,这就需要在程序中用到计时函数,matlab中提供了以下三种方 ...

  8. tornado异步请求非阻塞

    前言也许有同学很迷惑:tornado不是标榜异步非阻塞解决10K问题的嘛?但是我却发现不是torando不好,而是你用错了 比如最近发现一个事情:某网 前言 也许有同学很迷惑:tornado不是标榜异 ...

  9. 【POJ 1741】 Tree (树的点分治)

    Tree   Description Give a tree with n vertices,each edge has a length(positive integer less than 100 ...

  10. 【HDU 2855】 Fibonacci Check-up (矩阵乘法)

    Fibonacci Check-up Problem Description Every ALPC has his own alpc-number just like alpc12, alpc55, ...