Uva201 Squares, uva201squares

Source: Internet
Author: User

Uva201 Squares, uva201squares

 

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 arrayN2Dots (where2 <= n <= 9) And some interconnecting horizontal and vertical lines. A record for a single boardN2Dots andMInterconnecting lines is formatted as follows:

 

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

Line 2:MThe number of interconnecting lines

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

H I jIndicates a horizontal line in rowIWhich connects

The dot in columnJTo the one to its right in columnJ+ 1

Or

V I jIndicates a vertical line in columnIWhich connects

The dot in rowJTo the one below in rowJ+ 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''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 shocould 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

 

416H 1 1H 1 3H 2 1H 2 2H 2 3H 3 2H 4 2H 4 3V 1 1V 2 1V 2 2V 2 3V 3 2V 4 1V 4 2V 4 323H 1 1H 2 1V 2 1

 

Sample Output

 

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

There are many output requirements...
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; 25} 26 cout <"Problem #" <Flag1 + 1 <endl; 27 Flag1 ++; 28 for (int I = 1; I <a; I ++) 29 if (Judge (I, a) 30 {31 cout <Judge (I,) <"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 Indium Ut_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) 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}View Code

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.