欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UVa 201- Squares

程序员文章站 2024-03-19 08:47:10
...

一开始确实没思路,如何存点和线段的问题困扰了很久,知道在网上看了前辈们的解法才开始用两个数组分别存横线和竖线。之后只要按照边数数正方形就好了。易错点是输出格式,要注意空行的输出。

题目链接:UVa 201

AC代码:

#include <iostream>
#include <cstring> 
using namespace std;
int H[10][10], V[10][10];
int main() {
	int n, m, cnt = 1, a, b;
	char ch;
	while (cin >> n) {
		cin >> m;
		memset(H, 0, sizeof(H));
		memset(V, 0, sizeof(V));
		while (m--) {
			cin >> ch >> a >> b;
			if (ch == 'H')
				H[a][b] = 1;
			else if (ch == 'V')
				V[b][a] = 1;
		}
		if (cnt > 1)
			cout <<endl<< "**********************************" << endl<<endl;
		cout << "Problem #" << cnt++ << endl<<endl;
		int sum = 0;
	for (int p = 1; p < n; p++) {  //按照边数由少至多数正方形  
		int count = 0;
		for(int i=1;i+p<=n;i++)
			for (int j = 1; j + p <= n; j++) {
				int flag = 1;
				for (int k = j; k < j + p; k++)
					if (!H[i][k] || !H[i + p][k])  //同时判断一对边 
						flag = 0;
				for (int l = i; l < i + p; l++)
					if (!V[l][j] || !V[l][j + p])
						flag = 0;
				count += flag;
			}
		sum += count;
		if (count)
			cout << count << " square (s) of size " << p << endl;
	}
	if (!sum)
		cout << "No completed squares can be found." << endl;
	}
	return 0;
}
相关标签: 笔记