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("\n**********************************\n\n");
        printf("Problem #%d\n\n",++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\n",t,i);
                    flag=1;
            }
        }
        if(!flag)printf("No completed squares can be found.\n");
    }
    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("\n**********************************\n\n");
54         printf("Problem #%d\n\n",++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\n",t,i);
59                     flag=1;
60             }
61         }
62         if(!flag)printf("No completed squares can be found.\n");
63     }
64     return 0;
65 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值