习题4-2 UVA 201 Squares

思路,输入的时候,把(1,1)到(4,4)有两个坐标的点转化为1到16,(1,1)和(2,1)相连我们转化为1和5相连,然后像图那样存储,注意,这里有一个坑点,那就是输入行和列连线,两个数据含义不一样,不要想当然以为是坐标,之后就是按部就班,遍历可能的边长,再在里面遍历起点,再遍历这个起点的正方形的形状,看能不能构成正方形即可。

#include 
using namespace std;
int main()
{
	int n, m, x1, x2, count = 1;
	char c;
	while(scanf("%d%d", &n, &m) != EOF){
		int a[105][105] = {};
		bool judge = false;
		for(int i = 0; i < m; i++){
			getchar();
			scanf("%c%d%d", &c, &x1, &x2);
			int temp1 = (x1 - 1) * n + x2, temp2 = (x2 - 1) * n + x1;
			if(c == 'H') a[temp1][temp1 + 1] = a[temp1 + 1][temp1] = 1;
			else a[temp2][temp2 + n] = a[temp2 + n][temp2] = 1;
		}
		if(count > 1) printf("\n**********************************\n\n");
		printf("Problem #%d\n\n", count++);
		for(int i = 1; i <= n - 1; i++){
			int ans = 0;
			for(int j = 0; j < n - i; j++){
				for(int k = 1; k <= n - i; k++){
					int temp = j * n + k, ok = 1;
					for(int t = 0; t < i; t++){
						if(!a[temp + t][temp + t + 1]) ok = 0;
						if(!a[temp + t + n * i][temp + t + 1 + n * i]) ok = 0;
						if(!a[temp + t * n][temp + (t + 1) * n]) ok = 0;
						if(!a[temp + t * n + i][temp + (t + 1) * n + i]) ok = 0;
					}
					if(ok) ans++;
				}
			}
			if(ans) judge = true, printf("%d square (s) of size %d\n", ans, i);
		}
		if(!judge) printf("No completed squares can be found.\n");
	}
	return 0;
}

你可能感兴趣的:(#,第四章)