您的位置:首页 > 其它

Uva221矩形个数判断

2016-04-03 18:37 323 查看
这题我本来想的是如何储存的,在储存中寻找解决方案,结果在一番思考毫无思路以后,在google中发现了方案很简单。

存数组,在读取时判断。

#include <cstdio>
#include <iostream>
#include <cstring>

using namespace std;
int H[10][10], V[10][10];

int main() {
//freopen("cin", "r", stdin);
//freopen("cout", "w", stdout);
int n, m, T = 0;
while (~scanf("%d %d", &n, &m)) {
getchar();
memset(H, 0, sizeof(H));
memset(V, 0, sizeof(V));
for (int i = 0; i < m; i++) {
int a, b;
char c;
scanf("%c%d%d", &c, &a, &b);
getchar();
if (c == 'H')
H[a][b] = 1;
else
V[b][a] = 1;
}
if (T++) printf("\n**********************************\n\n");
printf("Problem #%d\n\n", T);

int sum = 0;
for (int l = 1; l <= n; l++) {
int count = 0, flag = 0;
for (int i = 1; i + l <= n; i++)
for (int j = 1; j + l <= n; j++) {
flag = 1;
for (int h = j; h < j + l; h++)
if (!H[i][h] || !H[i + l][h]) flag = 0;
for (int v = i; v < i + l; v++)
if (!V[v][j] || !V[v][j + l]) flag = 0;
count += flag;
}
sum += count;
if (count) printf("%d square (s) of size %d\n", count, l);
}
if (!sum) printf("No completed squares can be found.\n");
}
return 0;
}


这里的判断方法也很巧妙,采用的是数组循环,加l进行正方形的长度递增。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva 算法