zoukankan      html  css  js  c++  java
  • 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.(neglection above)

    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. 这张图片有三种像素组成,分别是 ' . '表示背景, ' * '表示骰子, ' X '骰子中的点;两个像素共用一条边表示连接,而尽管共用一角却不连接

    A set S of pixels is connected if for every pair (a,b) of pixels in S, there is a sequence $a_1, a_2, \dots, a_k$ inS such that a = a1 and b = ak , and ai and ai+1 are connected for $1 \le 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 \leŸw,h \le 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

    Miguel Revilla 
    2000-05-22
     
    /*The die is cast(掷骰子)
     *Author: xueying
     *Submit time : 2013-3-28 17:00
     *Verdict: Accept
     *Rank: 996/1630
     *Runningtime: 0.012ms
     *解题思路:深度查找中的基础上的深度查找(即两次深度查找)没有什么思路可言,花点时间想清楚题目的情形就可以了 
    */ 
    
    
    #include<stdio.h>
    #include<string.h>
    #define MAXN 60
    int dot[2510];
    
    int comp(const void *a, const void *b)
    {
        return *(int *)a - *(int *)b;
    }
    
    void scdfs(char (*alpha)[MAXN], int row, int column, int n, int m, int cnt, int flag)
    {// 为骰子中的点数做深度查找,并将找到的'X'改为'*'值 
        int i, j;
        for(j=column-1; j>=0 && alpha[row][j] == 'X'; --j)
        {
            alpha[row][j] = '*';
            scdfs(alpha, row, j, n, m, cnt, flag);
        }
            for(j=column+1; j<n && alpha[row][j] == 'X'; ++j) 
        {
            alpha[row][j] = '*';
            scdfs(alpha, row, j, n, m, cnt, flag);    
        }
        
        for(i=row-1; i>=0 && alpha[i][column] == 'X'; --i)
        {
            alpha[i][column] = '*';
            scdfs(alpha, i, column, n, m, cnt, flag);
        }
        for(i=row+1; i<m && alpha[i][column] == 'X'; ++i)
        {
            alpha[i][column] = '*';
            scdfs(alpha, i, column, n, m, cnt, flag);
        }
        return;
    }
    
    void dfs(char (*alpha)[MAXN], int row, int column, int n, int m, int cnt, int flag)
    {//为骰子(即 '*')做深度查找 
        int i, j;
        
        //共四个方向,按题目的要求:共边的像素才算是一类 
        for(j=column-1; j>=0 && (alpha[row][j] == '*' || alpha[row][j] == 'X') ; --j)
        {//左边相同的情况 
            if(alpha[row][j] == 'X')
            {//如果遇到的是'X',这时就应该转到另一深度查找 
                alpha[row][j] = '*';
                dot[cnt]++;
                scdfs(alpha, row, j, n, m, cnt, flag);
                //代码很明显可以再变简,但为了看出思路还是没有删减(毕竟我一开始这样写的) 
                alpha[row][j] = '.';
                dfs(alpha, row, j, n, m, cnt, flag);
            }
            else
            {
                alpha[row][j] = '.';
                dfs(alpha, row, j, n, m, cnt, flag);
            }
                
    
        }
        for(j=column+1; j<n && (alpha[row][j] == '*' || alpha[row][j] == 'X'); ++j) 
        {//右边相同的情况 
            if(alpha[row][j] == 'X')
            {
                alpha[row][j] = '*';
                dot[cnt]++;
                scdfs(alpha, row, j, n, m, cnt, flag);
                
                alpha[row][j] = '.';
                dfs(alpha, row, j, n, m, cnt, flag);
            }
            else
            {
                alpha[row][j] = '.';
                dfs(alpha, row, j, n, m, cnt, flag);
            }    
        }
        
        for(i=row-1; i>=0 && (alpha[i][column] == '*' || alpha[i][column] == 'X'); --i)
        {//上面相同的情况 
            if(alpha[i][column] == 'X')
            {
                alpha[i][column] == '*';
                dot[cnt]++;
                scdfs(alpha, i, column, n, m, cnt, flag);
                
                alpha[i][column] = '.';
                dfs(alpha, i, column, n, m, cnt, flag);
            }
            else 
            {
                alpha[i][column] = '.';
                dfs(alpha, i, column, n, m, cnt, flag);
            }
        }
        for(i=row+1; i<m && (alpha[i][column] == '*' || alpha[i][column] == 'X'); ++i)
        {//下面相同的情况 
            if(alpha[i][column] == 'X')
            {
                alpha[i][column] == '*';
                dot[cnt]++;
                scdfs(alpha, i, column, n, m, cnt, flag);
                
                alpha[i][column] = '.';
                dfs(alpha, i, column, n, m, cnt, flag);
            }
            else 
            {
                alpha[i][column] = '.';
                dfs(alpha, i, column, n, m, cnt, flag);
            }
        }
        return;
    }
    
    int main()
    {
    
        int m, n, cnt, i, j, flag, T = 0;
        char alpha[MAXN][MAXN];
        while(scanf("%d%d", &n, &m) != EOF && m && n)
        {
            getchar();
            memset(dot, 0, sizeof(dot));
            for(i=0; i<m; ++i)
                {
                    for(j=0; j<n; ++j)
                    scanf("%c", &alpha[i][j]);
                    getchar();
                }
                cnt = 0;
            for(i=0; i<m; ++i)
                for(j=0; j<n; ++j)
                {
                    if(alpha[i][j] != '.')
                    {
                        if(alpha[i][j] == 'X')
                        {
                            flag = 1;
                            dot[cnt]++;
                            scdfs(alpha, i, j, n, m, cnt, flag);
                            alpha[i][j] = '.';
                            dfs(alpha, i, j, n, m, cnt, flag);
                        }
                        else 
                        {
                            flag = 0;
                            alpha[i][j] = '.';
                            dfs(alpha, i, j, n, m, cnt, flag);
                        }
                        cnt++;
                    }    
                }
            qsort(dot, cnt, sizeof(int), comp);
            printf("Throw %d\n", ++T);
            for(i=0; i<cnt; ++i)
            {
                printf("%d", dot[i]);
                if(i+1 != cnt) printf(" ");
            }
            printf("\n\n");
        }
        return 0;
    }
  • 相关阅读:
    解析HTTP协议六种请求方法
    金蝶
    普元
    中间件
    [CTSC2008] 网络管理
    【Uva 10498】满意值
    【SPOJ839】最优标号
    bzoj2879 [Noi2012]美食节
    bzoj3144 [Hnoi2013]切糕
    bzoj3112 [Zjoi2013]防守战线
  • 原文地址:https://www.cnblogs.com/liaoguifa/p/2987144.html
Copyright © 2011-2022 走看看