zoukankan      html  css  js  c++  java
  • BFS:UVa201-Squares

    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:
    这里写图片描述

    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.


    解题心得

    1. 说实话被这个题给恶心惨了,在输出上为难人就算了,还要在输入描述上坑人,H i j 和 V i j 其中i并不是都是表示行,j都是表示列,坑人的啊。
    2. 具体的做法很简单,毕竟数据这么小,直接暴搜。

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 15;
    bool maps[maxn][maxn][maxn][maxn];
    //maps[i][j][x][y]为真的时候代表点(i,j)到(x,y)之间有连线
    int ans[maxn];
    
    void get_ans(int n)
    {
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
            {
                int len = 1;
                while(i+len<=n && j+len<=n)//从每一个点直接搜索,扩展长度就可以了
                {
                    bool flag = false;
                    for(int k=0;k<len;k++)
                        if(!maps[i][j+k][i][j+k+1])
                            flag = true;
    
                    for(int k=0;k<len;k++)
                        if(!maps[i+len][j+k][i+len][j+k+1])
                            flag = true;
    
                    for(int k=0;k<len;k++)
                        if(!maps[i+k][j][i+k+1][j])
                            flag = true;
    
                    for(int k=0;k<len;k++)
                        if(!maps[i+k][j+len][i+k+1][j+len])
                            flag = true;
    
                    if(!flag)
                        ans[len]++;
                    len++;
                }
            }
    
            bool flag = false;
            for(int i=1;i<=n;i++)
                if(ans[i])
                    flag = true;
            if(!flag)
                printf("No completed squares can be found.
    ");
            else
            {
                for(int i=1;i<=n;i++)
                    if(ans[i])
                        printf("%d square (s) of size %d
    ",ans[i],i);
            }
    }
    
    int main()
    {
        int n,t;
        t = 1;
        bool flag = false;
        while(scanf("%d",&n) != EOF)
        {
            if(flag)
                printf("
    **********************************
    
    ");
            flag = true;
            printf("Problem #%d
    
    ",t++);
            memset(maps,0,sizeof(maps));
            memset(ans,0,sizeof(ans));
            int num;
            scanf("%d",&num);
            while(num--)
            {
                char ch[5];
                int s,e;
                scanf("%s%d%d",ch,&s,&e);
                if(ch[0] == 'H')
                    maps[s][e][s][e+1] = true;
                else
                    maps[e][s][e+1][s] = true;
            }
            get_ans(n);
        }
        return 0;
    }
    
  • 相关阅读:
    js动画(三)
    js动画(二)
    css内容生成器
    css选择器基本属性
    css样式图片、渐变、相关小知识
    wed网页开发面试笔试必备小知识
    html5.边框属性相关知识点
    伪类选择符
    窗口尺寸小用法
    css3选择符使用个人理解。
  • 原文地址:https://www.cnblogs.com/GoldenFingers/p/9107273.html
Copyright © 2011-2022 走看看