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:

 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.

输出的要求比较多啊。。。

 1 #include<iostream>
 2 #include<algorithm>
 3
 4 using namespace std;
 5 int A[10][10],D[10][10];
 6
 7 void Initializer(int);
 8 void Input_Dates(int);
 9 int Judge(int,int);
10 int JudgePoint(int,int,int);
11 int main()
12 {
13     int a,b;
14     int Flag;
15     int Flag1=0;
16     while(cin >> a >> b)
17     {
18         Flag=1;
19         Initializer(a);
20         Input_Dates(b);
21         if(Flag1)
22         {
23             cout << endl << "**********************************"
24                  << endl << endl;
25         }
26         cout << "Problem #" << Flag1+1 << endl << endl;
27         Flag1++;
28         for(int i=1;i<a;i++)
29             if(Judge(i,a))
30             {
31                 cout << Judge(i,a) <<" square (s) of size " << i << endl;
32                 Flag=0;
33             }
34         if(Flag)
35             cout << "No completed squares can be found." << endl;
36     }
37     return 0;
38 }
39 void Initializer(int a)
40 {
41     for(int i=0;i<=a;i++)
42         for(int j=0;j<=a;j++)
43             D[i][j]=A[i][j]=0;
44 }
45 void Input_Dates(int n)
46 {
47     char c;
48     int a,b;
49     int Suma=0,Sumb=0;
50     for(int i=0;i<n;i++)
51     {
52         cin >> c >> a >> b;
53         if(c==‘H‘)
54             A[a][b]=1;
55         if(c==‘V‘)
56             D[b][a]=1;
57     }
58 }
59 int Judge(int n,int a)
60 {
61     int Sum=0;
62     for(int i=1;i<=a-n;i++)
63         for(int j=1;j<=a-n;j++)
64             Sum+=JudgePoint(i,j,n);
65     return Sum;
66 }
67 int JudgePoint(int x,int y,int n)
68 {
69     for(int i=0;i<n;i++)
70         if(!A[x][y+i])
71             return 0;
72     for(int i=0;i<n;i++)
73         if(!A[x+n][y+i])
74             return 0;
75     for(int i=0;i<n;i++)
76         if(!D[x+i][y])
77             return 0;
78     for(int i=0;i<n;i++)
79         if(!D[x+i][y+n])
80             return 0;
81     return 1;
82 }

时间: 2024-08-06 11:56:35

uva201 Squares的相关文章

【习题 4-2 Uva201】Squares

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 注意那个星号的数量... 然后V x y的话,是从(y,x)向(y+1,x)连线. H x y才是从(x,y)向(x,y+1)连线 枚举以(x,y)作为左上角的举行就ok了 [代码] #include <bits/stdc++.h> using namespace std; const int N = 10; int n,m; bool a[N+10][N+10][2]; int cnt[N+5]; bool check(in

USACO 1.2 Palindromic Squares

Palindromic SquaresRob Kolstad Palindromes are numbers that read the same forwards as backwards. The number 12321 is a typical palindrome. Given a number base B (2 <= B <= 20 base 10), print all the integers N (1 <= N <= 300 base 10) such that

Partial least squares regression(偏最小二乘法回归)

偏最小二乘法(PLS)是近年来发展起来的一种新的多元统计分析 http://en.wikipedia.org/wiki/Partial_least_squares_regression Partial least squares regression(偏最小二乘法回归),布布扣,bubuko.com

HDU 1264 Counting Squares(模拟)

题目链接 Problem Description Your input is a series of rectangles, one per line. Each rectangle is specified as two points(X,Y) that specify the opposite corners of a rectangle. All coordinates will be integers in the range 0 to 100. For example, the lin

Codeforces Round #337 (Div. 2) B. Vika and Squares 水题

B. Vika and Squares Vika has n jars with paints of distinct colors. All the jars are numbered from 1 to n and the i-th jar contains ai liters of paint of color i. Vika also has an infinitely long rectangular piece of paper of width 1, consisting of s

Project Euler 98:Anagramic squares 重排平方数

Anagramic squares By replacing each of the letters in the word CARE with 1, 2, 9, and 6 respectively, we form a square number: 1296 = 362. What is remarkable is that, by using the same digital substitutions, the anagram, RACE, also forms a square num

USACO 6.5 All Latin Squares

All Latin Squares A square arrangement of numbers 1 2 3 4 5 2 1 4 5 3 3 4 5 1 2 4 5 2 3 1 5 3 1 2 4 is a 5 x 5 Latin Square because each whole number from 1 to 5 appears once and only once in each row and column. Write a program that will compute the

HDU 1264 Counting Squares(线段树求面积的并)

Counting Squares Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1885    Accepted Submission(s): 946 Problem Description Your input is a series of rectangles, one per line. Each rectangle is sp

BFS解Magic Squares

Magic Squares IOI'96 Following the success of the magic cube, Mr. Rubik invented its planarversion, called magic squares. This is a sheet composed of 8 equal-sizedsquares: 1 2 3 4 8 7 6 5 In this task we consider the version where each square has a d