Squares
Time Limit: 3000MS |
|
Memory Limit: Unknown |
|
64bit IO Format: %lld &%llu |
Submit Status
Description
A children ' s board game consists of a square array of dots that contains lines connecting some of the pairs of adjacent do Ts. One part of the game requires so the players count the number of squares of certain sizes that is formed by these lines . For example, with the figure shown below, there is 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 Colu MN of the array line 2: m the number of interconnecting lines
Each of the next m lines is of one of the both types:
h I j indicates a horizontal line in row I which connects
The dot I N Column J to the one and 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, the label of the corresponding output with 'problem #1', 'problem #2', and so forth. Output for a record consists of the number of squares in the board, from the smallest to the largest. LF no squares of any size exist, your program should print a appropriate message indicating so. Separate output for successive input records by a line of asterisks between both 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 one square (s) of size 2**********************************problem #2No completed squares can be found.
The horizontal and vertical columns are stored separately, and the various edge lengths are enumerated to find the matching requirements.
#include <iostream> #include <cstring> #include <string>using namespace Std;int h[12][12];int v[12][ 12];bool judge (int len, int x, int y) {for (int i = x; i < x + len;i++) if (! H[y][i]) return false;for (int i = x; i < x + len;i++) if (! H[y + len][i]) return false;for (int j = y; J < y + len;j++) if (! V[X][J]) return false;for (int j = y; J < y + len;j++) if (! V[x + len][j]) return False;return true;} int main () {int N,l,x,y;int ans[12];char op;int cas = 1;while (cin >> N) {memset (h, 0, sizeof (h)); memset (V, 0, sizeof ( V)); memset (ans, 0, sizeof (ANS)), Cin >> L;while (l--) {cin >> op;cin >> x >> y;if (op = = ' H ') h[x][y] = 1;elsev[x][y] = 1;} for (int len = 1, len <= n-1;len++) for (int i = 1, i <= n-1; i++) for (int j = 1; J <= N-1; j + +) {if (Judge (len,i , j)) ans[len]++;} if (cas! = 1) {cout << endl;cout << "**********************************" << endl;cout << Endl;} cout << "Problem #" << cas++ << EndL << Endl;int OK = 0;for (int i = 1; i<= n-1; i++) {if (Ans[i]) {cout << ans[i] << "Square (s) of Si Ze "<< i << Endl;ok = 1;}} if (!ok) cout << "No completed squares can be found." << Endl;}
UVA-201 Squares