zoukankan      html  css  js  c++  java
  • uva201 Squares

    A children's board game consists of a square array of dots that contains lines connecting some of the pairs of adjacent dots. One part of the game requires that the players count the number of squares of certain sizes that are formed by these lines. For example, in the figure shown below, there are 3 squares-2 of size 1 and 1 of size 2. (The ``size" of a square is the number of lines segments required to form a side.)

    Your problem is to write a program that automates the process of counting all the possible squares.

    Input

    The input file represents a series of game boards. Each board consists of a description of a square array of n2 dots (where 2 <= n <= 9) and some interconnecting horizontal and vertical lines. A record for a single board with n2 dots and m interconnecting lines is formatted as follows:

     Line 1: 	n the number of dots in a single row or column of the array

    Line 2: m the number of interconnecting lines

    Each of the next m lines are of one of two types:

    H i j indicates a horizontal line in row i which connects

    the dot in column j to the one to its right in column j + 1

    or

    V i j indicates a vertical line in column i which connects

    the dot in row j to the one below in row j + 1

    Information for each line begins in column 1. The end of input is indicated by end-of-file. The first record of the sample input below represents the board of the square above.

    Output

    For each record, label the corresponding output with ``Problem #1", ``Problem #2", and so forth. Output for a record consists of the number of squares of each size on the board, from the smallest to the largest. lf no squares of any size exist, your program should print an appropriate message indicating so. Separate output for successive input records by a line of asterisks between two blank lines, like in the sample below.

    Sample Input

    4
    16
    H 1 1
    H 1 3
    H 2 1
    H 2 2
    H 2 3
    H 3 2
    H 4 2
    H 4 3
    V 1 1
    V 2 1
    V 2 2
    V 2 3
    V 3 2
    V 4 1
    V 4 2
    V 4 3
    2
    3
    H 1 1
    H 2 1
    V 2 1

    Sample Output

    Problem #1
    
    2 square (s) of size 1
    1 square (s) of size 2
    
    **********************************
    
    Problem #2
    
    No completed squares can be found.


    这是刚开始的错误代码*
    #include <iostream>
    #include <cstdio>
    #include <string.h>
    using namespace std;
    int H[9][9],V[9][9];   //H为横着的边,V为竖着的边,把边当作点来存
    
    int judging(int x,int y,int k)
    {
        for(int i=x;i<x+k;i++){
            if(V[i][y]==0)return 0;
        }
        for(int i=x;i<x+k;i++){
            if(V[i][y+k]==0)return 0;
        }
        for(int j=y;j<y+k;j++){
            if(H[x][j]==0)return 0;
        }
        for(int j=y;j<y+k;j++){
            if(H[x+k][j]==0)return 0;
        }
        return 1;
    }
    
    int judge(int k,int m)                  //边长为k的正方形
    {
        int n=0;                    //正方形的个数
        for(int i=1;i<=m-k;i++){
            for(int j=1;j<=m-k;j++){
                n+=judging(i,j,k);
                //cout<<"k  "<<k<<" i "<<i<<" j "<<j<<"  number  "<<judging(i,j,k)<<endl;
            }
        }
        return n;
    }
    
    int main()
    {
        int m,n;            //m*m个小黑点,n个点
        int cot=0;
        while(cin>>m>>n){
            char ch;
            int x,y,flag=0,t;
            memset(H,0,sizeof(H));
            memset(V,0,sizeof(V));
            for(int i=0;i<n;i++){
                cin>>ch>>x>>y;
                //scanf("%c%d%d",&ch,&x,&y);            //不知道为什么用scanf输入只能输入到H结束,输入有问题!!
                if(ch=='H') H[x][y]=1;
                else V[y][x]=1;
            }
            if(cot)printf("
    **********************************
    
    ");
            printf("Problem #%d
    
    ",++cot);
            for(int i=1;i<m;i++){
                //cout<<"i  "<<i<<" m  "<<m<<endl;
                t=judge(i,m);
                //cout<<"t  "<<t<<endl;
                if(t){
                        printf("%d square (s) of size %d
    ",t,i);
                        flag=1;
                }
            }
            if(!flag)printf("No completed squares can be found.
    ");
        }
        return 0;
    }

    仔细检查,发现有可能有9*9个黑点,而以上代码定义H[9][9]和V[9][9],且黑点是从H[1][1]和V[1][1]开始初始化的,明显开辟的数组过小导致结果错误
    因此,将9*9的数组改为10*10即可

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <string.h>
     4 using namespace std;
     5 int H[10][10],V[10][10];   //H为横着的边,V为竖着的边,把边当作点来存
     6 
     7 int judging(int x,int y,int k)
     8 {
     9     for(int i=x;i<x+k;i++){
    10         if(V[i][y]==0)return 0;
    11     }
    12     for(int i=x;i<x+k;i++){
    13         if(V[i][y+k]==0)return 0;
    14     }
    15     for(int j=y;j<y+k;j++){
    16         if(H[x][j]==0)return 0;
    17     }
    18     for(int j=y;j<y+k;j++){
    19         if(H[x+k][j]==0)return 0;
    20     }
    21     return 1;
    22 }
    23 
    24 int judge(int k,int m)                  //边长为k的正方形
    25 {
    26     int n=0;                    //正方形的个数
    27     for(int i=1;i<=m-k;i++){
    28         for(int j=1;j<=m-k;j++){
    29             n+=judging(i,j,k);
    30         }
    31     }
    32     return n;
    33 }
    34 
    35 int main()
    36 {
    37     int m,n;            //m*m个小黑点,n个点
    38     int cot=0;
    39     while(cin>>m>>n){
    40         char ch;
    41         int x,y,flag=0,t;
    42         memset(H,0,sizeof(H));
    43         memset(V,0,sizeof(V));
    44         /*for(int i=0;i<10;i++){
    45             for(int j=0;j<10;j++)cout<<H[i][j]<<" ";
    46             cout<<endl;
    47         }*/
    48         for(int i=0;i<n;i++){
    49             cin>>ch>>x>>y;
    50             if(ch=='H') H[x][y]=1;
    51             else V[y][x]=1;
    52         }
    53         if(cot)printf("
    **********************************
    
    ");
    54         printf("Problem #%d
    
    ",++cot);
    55         for(int i=1;i<m;i++){
    56             t=judge(i,m);
    57             if(t){
    58                     printf("%d square (s) of size %d
    ",t,i);
    59                     flag=1;
    60             }
    61         }
    62         if(!flag)printf("No completed squares can be found.
    ");
    63     }
    64     return 0;
    65 }
  • 相关阅读:
    【BZOJ 3732】 Network
    【BJOI 2018】 求和
    【HDU 1005】 Number Sequence
    【HDU 3652】 B-numbers
    【SCOI 2007】 降雨量
    BZOJ2186 SDOI2008沙拉公主的困惑(数论)
    #38 游戏(线段树)
    BZOJ2339 HNOI2011卡农(动态规划+组合数学)
    BZOJ3107 CQOI2013二进制A+B(动态规划)
    BZOJ3083 遥远的国度(树链剖分+线段树)
  • 原文地址:https://www.cnblogs.com/farewell-farewell/p/5405547.html
Copyright © 2011-2022 走看看