POJ1481 The Die Is Cast (DFS & BFS 结合)

The Die Is Cast
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 780 Accepted: 330

Description

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 a i and a i+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.

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

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
题目不难 就是手残  GOOD!!!

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>

using namespace std;

struct node{
    int x,y;
}q[10000];

int vis[100][100],px[3000],n,m,s,e,num=0,map[100][100];
int mv[4][2] = {{0,1},{1,0},{0,-1},{-1,0}}; //上下左右4个方向
char a[100];

int cmp(const void *a,const void *b)
{
    return *(int *)a - *(int *)b;
}

void DFS_BFS(int x,int y)
{
    node t,f,h;

    if(map[x][y]==0)
        return;

    if(map[x][y]==2) //BFS操作,进行判断X是否相连
       {
           s = 0,e = 0;
           f.x = x;  f.y = y;

           vis[x][y] = 1;


            // printf("%d ",num);

             q[e++] = f;
             while(s < e)
             {
                 f = q[s++];
                 for(int k = 0;k<4;k++)
                 {
                     h.x = f.x + mv[k][0];
                     h.y = f.y + mv[k][1];
                     if(vis[h.x][h.y]==0 && map[h.x][h.y]== 2 &&h.x>=0&&h.x<m&&h.y>=0&&h.y<n)
                     {
                         vis[h.x][h.y] = 1;
                         q[e++] = h;
                     }
                  }

             }
              ++num;
        }
    for(int i =0;i<4;i++)//DFS操作,为了含*的几个区域
    {
        t.x = x + mv[i][0];
        t.y = y + mv[i][1];
        if(0<=t.x && t.x<m && 0<=t.y && t.y<n&&vis[t.x][t.y]==0)
        {
            vis[t.x][t.y] = 1;
            DFS_BFS(t.x,t.y);
        }
    }
    return ;
}

int l = 0;

void Search()//找含*的位置,进行DFS_BFS
{

    for(int i = 0;i<m;i++)
    {
        for(int j = 0;j<n;j++)
        {

            if((map[i][j]==1 || map[i][j]==2 )&&vis[i][j]==0)
            {
                vis[i][j] = 1;
                num = 0;
                DFS_BFS(i,j);
               if(num)//删掉就WA,我日 不知为什么
                px[l++] = num;
            }
        }
    }
}

int main()
{
    int C = 0;
    while(cin>>n>>m)
    {
        if(n==0||m==0)
            break;

          l = 0;

        memset(vis,0,sizeof(vis));
        memset(px,0,sizeof(px));
        for(int i = 0;i<m;i++)
        {
            cin>>a;
            for(int j = 0;j<n;j++)
               {
                   if(a[j]=='.')
                    {
                        //map数组中 1代表*  2代表X  0代表.
                        vis[i][j] = 1;
                        map[i][j] = 0;
                    }
                    else if(a[j]=='*')
                        map[i][j] = 1;
                    else if(a[j]=='X')
                        map[i][j] = 2;
               }
        }
        Search();

       qsort(px,l,sizeof(px[0]),cmp);

        cout<<"Throw "<<++C<<endl;
        for(int i = 0;i<l-1;i++)
        {
           // cout<<"i= "<<i;
            cout<<px[i]<<" ";
        }

        cout<<px[l-1]<<endl<<endl;

    }
    return 0;
}

测试数据如下:

30 15
..............................
..............................
...............*..............
...*****......****............
...*X***.....**X***...........
...*****....***X**............
...***X*.....****.............
...*****.......*..............
..............................
........***........******.....
.......**X****.....*X**X*.....
......*******......******.....
.....****X**.......*X**X*.....
........***........******.....
..............................
41 41
.........................................
.******......................*****.......
.*X**X*......................*X*X*.......
.******......................*****.......
.*X**X*......................*X*X*.......
.******......................*****.......
.........................................
.........................................
.......********..........................
.......*XX**XX*..........................
.......********..........................
.......*XX**XX*..........................
.......********..........................
.......*XX**XX*..........................
.......********..........................
.........................................
.........................................
.........................................
................**.......................
..............*****......................
.............***X***.....................
............*******......................
...........**X****.......................
...........*****.........................
............**...........................
.........................................
.........................................
..........................**.............
.........................****............
........................*X****...........
.......................****X***..........
........................******...........
.........................****............
..........................**.............
.........................................
.........................................
.........................................
.........................................
.........................................
.........................................
.........................................
20 29
....................
....................
....................
*****...............
*XX**...............
*XX**...............
*****...............
*****...............
....................
....................
....................
....................
....*****...........
....*X***...........
....*****.....***...
....***X*....**X**..
....*****...*****...
...........*XX**....
....*****...**......
....*X*X*...........
....*****...........
....*X*X*...........
....*****...........
....................
....................
....................
....................
....................
....................
29 12
.............................
.............................
........***..................
......**X***.................
.....*******.................
....****X**..................
....**X***...................
.....***.....................
.............................
.............................
.............................
.............................
48 12
................................................
................................................
................................................
......******....................................
......*X*XX*.........................**.........
......******......**................****........
......*XX*X*.....****..............**XX**.......
......******....**X****...........***X**........
...............*******.............****.........
...............****X*...............**..........
.................***............................
................................................
25 21
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.....******..............
.....*X**X*..............
.....******..............
.....*X**X*..............
.....******..............
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
.........................
43 15
...........................................
.....*.....................................
....*X*....................................
...*****...................................
..*X*X*X*..................................
...*****...................................
....*X*....................................
.....*.....................*...............
..........................***..............
.........................*X***.............
.........................***X**............
..........................****X*...........
...........................****............
............................**.............
...........................................
47 7
.....................*******...................
.******..............*X***X*...................
.*XX*X*....******....***X***.........***.......
.******....*X*XX*....*X***X*........*****......
.*XX*X*....*X*XX*....*******.......***X**......
.******....******..................*****.......
...........******...................***........
19 45
...................
...................
...................
...................
...................
...................
...................
...................
...................
...................
...................
...................
...................
...................
...................
...................
...................
.....*******.......
.....*X***X*.......
.....***X***.......
.....*X***X*.......
.....*******.......
...................
...................
...................
...................
...................
...................
...................
...................
...................
...................
...................
...................
...................
...................
...................
...................
...................
...................
...................
...................
...................
...................
...................
39 44
.......................................
.******.******.*****.*****.*****.......
.*X****.**XX**.*X*X*.**X**.*X*X*.......
.****X*.**XX**.*****.**X**.*X*X*.......
.******.******.*X*X*.*****.*****.......
...............*****...................
.......................................
.......................................
.......................................
.......................................
...***.................................
..*X****...............................
...**X***..............................
....***X**..**.........................
.....***...*X**........................
..........******.......................
.........*X***X**......................
..........**X***.......................
...........****........................
............**.........................
.......................................
.......................................
.......................................
.......................................
.......................................
.******.******.*****.*****.*****.......
.*X****.*X*XX*.*X*X*.**X**.*X*X*.......
.****X*.*X**X*.*****.**X**.*X*X*.......
.******.******.*X*X*.*****.*****.......
...............*****...................
.......................................
.......................................
.......................................
.......................................
.......................................
.......................................
.......................................
.......................................
.......................................
.******.******.*****.*****.*****.......
.*X****.**XX**.*X*X*.**X**.*X*X*.......
.****X*.**XX**.*****.**X**.*X*X*.......
.******.******.*X*X*.*****.*****.......
...............*****...................
37 35
.....................................
.....................................
..*..................................
.***.................................
**X**................................
.*****...............................
..***................................
...*.................................
.....................................
.....................................
.....................................
......................**.............
.....................****............
....................***X**...........
.....................****............
......................**.............
.....................................
.....................................
.....................................
.....................................
.....................................
.....................................
.....................................
.....................................
.....................................
.....................................
......******.........................
......*X*XX*.........................
......******......**............*****
......*XX*X*.....****...........*X*X*
......******....**X****.........*****
...............*******..........*X*X*
...............****X*...........*****
.................***.................
.....................................
3 3
***
*X*
***
4 4
****
*XX*
*XX*
****
4 4
****
*X**
**X*
****
8 4
********
*X*X*X**
**X*X*X*
********
30 20
*****.....*****.....*****..***
*X*X*.....*X***.....*XXX*..*X*
*****.....*****.....*XXX*..*X*
*X*X*.....***X*.....*XXX*..***
*****.....*****.....*****.....
.....*****.....*****.....*****
.....*XX**.....*XX**.....*X***
.....*XXX*.....*****.....*****
.....**XX*.....**XX*.....*****
.....*****.....*****.....*****
*****.....*****.....*****.....
*X***.....*X*X*.....*****.....
***X*.....*****.....**X**.....
*X***.....**X**.....*****.....
*****.....*****.....*****.....
.....*****.....*****.....*****
.....*XXX*.....*XXX*.....*X*X*
.....*****.....*X***.....***X*
.....*XXX*.....*X*X*.....*XXX*
.....*****.....*****.....*****
50 11
*****.*******.*********.***********.**************
*X*X*.*X*X*X*.*X*X*X*X*.*X*X*X*X*X*.*X*X*X*X*X*XX*
*****.*******.*********.***********.**************
..................................................
*****.*******.*********.***********.*************.
*XXX*.*X*X*X*.*X*X*XXX*.*X*X*X*X*X*.*XXX*X*X*XXX*.
*****.*******.*********.***********.*************.
..................................................
**************************************************
***XXXXXXXXXX**X*X*****XXXXXXX******XX**XXX*******
**************************************************
10 10
**********
*XXXXXXXX*
*XXXXXXXX*
*XXXXXXXX*
*XXXXXXXX*
*XXXXXXXX*
*XXXXXXXX*
*XXXXXXXX*
*XXXXXXXX*
**********
50 50
**************************************************
**************************************************
**************************************************
**************************************************
******XX*************************XXXXXXX**********
*****XXXX***********************XXXXXXXXXX********
******XX************************XXXXXXXX**********
*********************************XXXXXXX**********
**********************************XXXXXX**********
***********************************XXXXX**********
************************************XXXX**********
**************************************XX**********
**************************************XX**********
***************************************X**********
**************************************************
**************************************************
*******XXXXXX*************************************
*******XXXXXX*************************************
*******XXXXX**************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
******************************XXXXX***************
**************************XXXXXXXXXX**************
****************************XXXXXXX***************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
************XXX*************************X*********
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
50 50
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
***********************X**************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************
**************************************************


答案:




> Throw 1
> 1 2 2 4

> Throw 2
> 2 2 4 4 6



> Throw 3
> 1 2 2 4

> Throw 4
> 3

> Throw 5
> 1 2 4

> Throw 6
> 4

> Throw 7
> 3 5

> Throw 8
> 1 2 4 5

> Throw 9
> 5

> Throw 10
> 1 1 1 1 1 2 2 2 2 2 2 2 3 4 4 4 4

> Throw 11
> 1 1 2 4 4

> Throw 12
> 1

> Throw 13
> 1

> Throw 14
> 2

> Throw 15
> 6

> Throw 16
> 1 1 1 1 1 2 2 2 2 2 3 3 4

> Throw 17
> 1 2 3 3 3 4 4 5 5 6 6

> Throw 18
> 1

> Throw 19
> 6

> Throw 20
> 1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值