Squares
来源:互联网 发布:word2010表格数据缺失 编辑:程序博客网 时间:2024/04/30 05:18
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 ofn2 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 arrayLine 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
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.
就是问正方形有几个,直接暴力搜过去的……
#include<cstdio>#include<queue>#include<cstring>#include<iostream>#include<string>#include<algorithm>using namespace std;int dp[20], dp1[20][20], dp2[20][20];int main(){int i, j, m, n, ans, t, x, y, k, p, q, leap;char c;p = 1;while (scanf("%d", &n) != EOF){cin >> m;for (i = 0; i <= n;i++)for (j = 0; j <= n; j++){dp1[i][j] = dp2[i][j] = 0;}memset(dp, 0, sizeof(dp));getchar();for (i = 1; i <= m; i++){cin >> c >> x >> y;getchar();if (c == 'H'){dp1[x][y] = 1;}else if (c == 'V'){dp2[y][x] = 1;}}for (k = 1; k <= n; k++){for (i = 1; i <= n - k; i++){for (j = 1; j <= n - k; j++){leap = 0;for (q = 0; q < k; q++){if (dp1[i][j + q] == 0)leap = 1;if (dp1[i + k][j + q] == 0)leap = 1;if (dp2[i + q][j] == 0)leap = 1;if (dp2[i + q][j + k] == 0)leap = 1;}if (leap == 0)dp[k]++;}}}if (p != 1)cout << endl << "**********************************" << endl << endl;cout << "Problem #" << p << endl << endl;p++;leap = 0;for (k = 1; k <= n; k++){if (dp[k]){cout << dp[k] << " square (s) of size " << k << endl;leap = 1;}}if (leap == 0)cout << "No completed squares can be found." << endl;}return 0;}
- Squares
- Squares
- Squares
- Squares
- Squares
- Squares
- Squares
- Squares
- Squares
- Least Squares
- Palindromic Squares
- hdu1264Counting Squares
- POJ2002 Squares
- 【STL】Squares
- palindromic squares
- Palindromic Squares
- Palindromic Squares
- Palindromic Squares
- NoSQL架构实践(二)——以NoSQL为主
- c#根据公式进行自动计算的实现
- (转)struts2 Action获取表单数据
- NoSQL架构实践(三)——以NoSQL为缓存
- java8 新特性之-----Lambda
- Squares
- Power of Two
- sqlit3的使用
- [Cocoa]_[初级]_[NSThread的使用]
- JS笔记-熟悉DOM元素中的各种属性
- android判断EditText输入的数字、中文还是字母方法
- 项目风险管理之风险识别
- C++对二进制文件的读写操作[c++学习网站]
- Java学习笔记(七) Enumertation接口的理解和使用和StringTokenizer的使用