题目简单分析
题目的详细内容可以在这个网站上看到,下面简单说明一下题目要求。
[题意]
题目的大概意思就是根据给出的点和边来数不同大小的正方形的个数。示意图如下:
图中的点阵行列数n为4,若干条边将其连接起来。题目要求我们分别给出边长为1、2、3的正方形的个数。我们可以看出,个数应该分别为2、1、0。
[输入输出]
输入输出样例如下:前两行分别是点阵的边长n和总共的边数,后面依次给出各条边(H 3 2 表示以点(3.2)为起点,长度为1的横向边;V 3 2表示以点(2.3)为起点,长度为1的竖向边。)。按照格式输出各个尺寸的正方形的个数即可。
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.
[分析]
这里笔者直接暴力的逐点对各个尺寸的正方形进行统计,然后输出结果。VS2017工程和其他代码在github。
代码
代码思路比较简单,没什么好说的。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define maxn 10
int HL[maxn][maxn - 1];//横线/0行列不用
int VL[maxn][maxn - 1];//竖线//0行列不用
int n;//方阵的边长
int nLine;//边的个数
int cntSquares(int length);
int main() {
int cnt = 1;
int temp=0,squareCnt=0;
freopen("data.txt", "r", stdin);
while (~scanf("%d %d", &n, &nLine)) {
memset(HL, 0, sizeof(HL));
memset(VL, 0, sizeof(VL));
squareCnt = 0;
for (int k = 0; k < nLine; ++k) {
char ch, i, j;
scanf("%s %d %d", &ch, &i, &j);
if (ch == 'H') HL[i][j] = 1;
else VL[i][j] = 1;
}
if (cnt > 1) printf("\n**********************************\n\n");
printf("Problem #%d\n\n", cnt);
for (int i = 1; i < n; ++i) {
temp = cntSquares(i); squareCnt += temp;
if (temp)printf("%d square (s) of size %d\n", temp, i);
}
if (!squareCnt)printf("No completed squares can be found.\n");
cnt++;
}
fclose(stdin);
return 0;
}
int cntSquares(int length) {
int cnt = 0;
for (int i = 1; i <= n - length; ++i)
for (int j = 1; j <= n - length; ++j) {
int k;
for (k = 0; k < length; ++k) {
if (!HL[i][j + k] || !HL[i + length][j + k]) break;
if (!VL[j][i + k] || !VL[j + length][i + k]) break;
}
if (k == length) cnt++;
}
return cnt;
}