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.

    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 $a_1, a_2, dots, a_k$ in S 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

    题意: 给定一个图片。。图片上有有一些骰子的图形。。骰子点数用'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
    ", 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("
    
    ");
    	}
    	return 0;	
    }



  • 相关阅读:
    day09 小练习 斐波那契数列 文件
    day09三目运算
    day08文件操作
    Nginx 内容缓存及常见参数配置
    阿里开源分布式事务解决方案 Fescar 全解析
    为什么你学不会递归?刷题几个月,告别递归,谈谈我的经验
    JavaScript 复杂判断的更优雅写法
    Java 线程本地 ThreadLocal 的分析和总结
    总结异步编程的六种方式
    JAVA8新特性(吐血整理)
  • 原文地址:https://www.cnblogs.com/riskyer/p/3233666.html
Copyright © 2011-2022 走看看