总结一下:该题目要你去写一个判断图里面有几个正方形 然后按长度顺序输出这些正方形的数量,那么用一个二维数组来存储这个图形是很简单的,关键是如何去判断呢判断这个咳咳 我们就用最原始的方法去慢慢一个一个点来判断,题目告诉我们正方形的长度,那我们就从(1,1)这个点开始从长度1到n来推测这个正方形是否是成立的嘻嘻,就完事了
#include <iostream>
#include<string>
#include<algorithm>
using namespace std;
int n;
int m;
bool matrix1[10][10];
bool matrix2[10][10];
int ans[10];
int judge(int x, int y, int length)//判断长度为length的正方形是否成立点在(x,y)
{
for (int i = x;i < x + length;i++)//向下
{
if (!matrix2[i][y])return 0;
}
for (int i = x;i < x + length;i++)//向右到最右再向下
{
if (!matrix2[i][y + length])return 0;
}
for (int i = y;i < y + length;i++)向右
{
if (!matrix1[x][i])return 0;
}
for (int i = y;i < y + length;i++)向下到最下后向右
{
if (!matrix1[x + length][i])return 0;
}
return 1;
}
int main()
{
int problem = 0;
while (cin >> n >> m)
{
if (problem) puts("\n**********************************\n");
memset(matrix1, 0, sizeof(matrix1));
memset(matrix2, 0, sizeof(matrix2));
memset(ans, 0, sizeof(ans));
char a;
while (m--)
{
cin >> a;int x, y;
if (a == 'H')
{
cin >> x >> y;
matrix1[x][y] = true;
}
if (a == 'V')
{
cin >> x >> y;
matrix2[y][x] = true;
}
}
printf("Problem #%d\n\n", ++problem);
int cnt = 0;
for (int length = 1;length <= n;length++)
{
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= n;j++)
{
if (judge(i, j, length))
{
cnt++;
ans[length]++;
}
}
}
}
/*int i, j;
for (i = 1;i <= n;++i) {
for (j = 1;j <= n;++j) printf("%d ", matrix1[i][j]);
putchar('\n');
}
for (i = 1;i <= n;++i) {
for (j = 1;j <= n;++j) printf("%d ", matrix2[i][j]);
putchar('\n');
}*/
if (!cnt) {
cout << "No completed squares can be found." << endl;
}
else
{
for (int i = 1;i <= n;i++)
{
if (ans[i])
{
printf("%d square (s) of size %d\n", ans[i], i);
}
}
}
}
return 0;
}