zoukankan      html  css  js  c++  java
  • 657

    题目大意就是读出图上筛子的点数。 一开始搞错了以为是读出每个筛子上一共有多少个联通点。 结果题目要求的是一块联通的算一个点。问一个筛子上有多少个联通的。。。(样例正好可以过) 还有就是注意一下输出格式

    题目:

    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
    

    代码:
      1     #include <iostream>
      2 #include <memory.h>
      3 #include <cstdio>
      4 #include <cstring>
      5 #include <queue>
      6 #include <algorithm>
      7 using namespace std;
      8 
      9 int G[100][100];
     10 int vis[100][100];
     11 int row,line,cse=0;
     12 typedef pair <int,int> pii;
     13 
     14 const int dx[] = {0,0,1,-1};
     15 const int dy[] = {1,-1,0,0};
     16 
     17 
     18 void print()
     19 {
     20     for(int i=0;i<row;i++)
     21     {
     22         for(int j=0;j<line;j++)
     23             cout<<vis[i][j]<<" ";
     24 
     25         cout<<endl;
     26     }
     27     cout<<endl<<endl;
     28 }
     29 
     30 
     31 inline bool check(int x,int y)
     32 {
     33     if(x>=0 && x<row && y>=0 &&y<line)return true;
     34     else
     35         return false;
     36 }
     37 int bfs(int x,int y )
     38 {
     39     queue<pii> v;
     40     v.push(make_pair(x,y));
     41     while(!v.empty())
     42     {
     43         pii now = v.front();
     44         int nx=now.first,ny=now.second;
     45 
     46         for(int i=0;i<4;i++)
     47         {
     48             int ax = nx+dx[i],ay=ny+dy[i];
     49             if(check(ax,ay)&&!vis[ax][ay] && G[ax][ay]=='X')
     50             {
     51                 v.push(make_pair(ax,ay));
     52                 vis[ax][ay]=1;
     53             }
     54 //            else if(check(ax,ay)&&!vis[ax][ay] && G[ax][ay]=='*')
     55 //            {
     56 //                vis[ax][ay]=1;
     57 //            }
     58         }
     59         vis[nx][ny]=1;
     60         v.pop();
     61     }
     62 }
     63 
     64 int ppo[100];
     65 int pa=0;
     66 int dfs_back(int x,int y)
     67 {
     68     vis[x][y]=1;
     69     for(int i=0;i<4;i++)
     70     {
     71         int ax = x + dx[i] , ay = y+dy[i];
     72         if(check(ax,ay)&& !vis[ax][ay] && G[ax][ay]=='*')
     73         {
     74             dfs_back(ax,ay);
     75         }
     76         else if(check(ax,ay)&& !vis[ax][ay] && G[ax][ay]=='X')
     77         {
     78             ppo[pa]++;
     79            // print();
     80             bfs(ax,ay);
     81             dfs_back(ax,ay);
     82         }
     83     }
     84 
     85 }
     86 
     87 int main()
     88 {
     89 
     90     freopen("in.txt","r",stdin);
     91     while(cin>>line>>row &&row!=0 && line!=0)
     92     {
     93         cout<<"Throw "<<++cse<<endl;
     94         getchar();
     95         for(int i=0;i<row;i++)
     96         {
     97             for(int j=0;j<line;j++)
     98                 G[i][j]=getchar();
     99             getchar();
    100         }
    101         for(int i=0;i<row;i++)
    102         {
    103             for(int j=0;j<line;j++)
    104             {
    105                 if(!vis[i][j] && G[i][j]=='*')
    106                 {
    107                     dfs_back(i,j);
    108                     pa++;
    109                     //print();
    110                 }
    111                 else if(G[i][j]=='.')
    112                     {vis[i][j]=1;}
    113             }
    114 
    115         }
    116         sort(ppo,ppo+pa);
    117         for(int i=0;i<pa-1;i++)
    118         {
    119             if(ppo[i])
    120             cout<<ppo[i]<<" ";
    121         }
    122         if(ppo[pa-1])
    123         cout<<ppo[pa-1];
    124         cout<<endl<<endl;
    125 
    126         pa=0;
    127         memset(vis,0,sizeof(vis));
    128         memset(G,0,sizeof(G));
    129         memset(ppo,0,sizeof(ppo));
    130     }
    131 
    132     return 0;
    133 }
  • 相关阅读:
    JAVA高级篇(二、JVM内存模型、内存管理之第二篇)
    JAVA高级篇(三、JVM编译机制、类加载机制)
    spring batch (二) 元数据表
    spring batch (一) 常见的基本的概念介绍
    ORACLE——EXTRACT() 截取日期时间的函数使用
    ORACLE——NVL()、NVL2() 函数的用法
    ORACLE删除分区
    ORACLE中关于使用between在MyBatis中取不同的区间值和取反
    ORACLE——count() 统计函数的使用
    Oracle——trunc()函数的使用
  • 原文地址:https://www.cnblogs.com/doubleshik/p/3473875.html
Copyright © 2011-2022 走看看