您的位置:首页 > 其它

算法竞赛入门经典第四章习题4-2 Squares UVA - 201

2018-01-04 16:01 579 查看
习题4-2

https://vjudge.net/problem/UVA-201

#include<iostream>
#include<string>
#include<cstring>
#pragma warning(disable:4996)
using namespace std;
int main() {
#ifdef _DEBUG
freopen("in", "rb", stdin);
#endif // _DEBUG
int N, M, cnt = 0;
while (cin >> N >> M) {
int H[10][10] = {}, V[10][10] = {};
if (++cnt != 1)printf("\n**********************************\n\n");
printf("Problem #%d\n\n", cnt);
while (M--) {
string s; int a, b;
cin >> s >> a >> b;
switch (s[0])
{
case 'H':H[a][b] = 1;
break;
case 'V':V[b][a] = 1;
break;
default:
break;
}
}
bool flag = true;//是否没有正方形
for (int k = 1; k <= N-1; ++k) {//k为边长
int nn = 0;//边长为k的正方形个数
for (int i = 1; i + k <= N; ++i) {
for (int j = 1; j + k  <= N; ++j) {
bool is_z = true;
for (int x = 0; x < k; ++x)
if (!(H[i][j + x] && H[i + k][j + x] && V[i + x][j] && V[i + x][j + k])) {
is_z = false; break;
}
if (is_z) ++nn;
}
}
if (nn) {
flag = false;
printf("%d square (s) of size %d\n", nn, k);
}
}
if (flag) printf("No completed squares can be found.\n");
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: