Exercise 4_2 uva201 square

This topic I chose to look at the idea solution to a problem of
unexpected surprise, is my dish
actually prompted the old topic of obvious, H and V a cross a vertical, description of a point, to be divided anyway, to which can be connected side storage,
(keep note of time to keep the V and H rows and columns consistent !!!)
and then ask how many squares are, to a wave of violence enumerate (anyway up to 64 points)
deposit when it is stored each point can be up to how long the side for the point, a right-click, respectively hc and vc storage
someone asked, that does not just two sides of it, a square has four sides, ah, this is the technology of step we have to get on the square hc [i] [j] and left vc [i] [j], the next to find the law, found under, is hc [i + z] [j ], and the right, it is vc [i] [j + z ], (z is determined at this time the square side length) like this to get
ah, yes ... my dishes

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 16;
char s[4];
int hc[MAXN][MAXN],vc[MAXN][MAXN],H[MAXN][MAXN],square[MAXN], 
V[MAXN][MAXN];
int main()
{
	int m;
	for(int t = 1;scanf("%d",&m)==1; t++)
	{
		if(t > 1) printf("\n**********************************\n\n");//小心输出 
		int n,a,b;
		scanf("%d",&n);
		memset(H,0,sizeof(H));memset(V,0,sizeof(V));
		memset(hc,0,sizeof(hc));memset(vc,0,sizeof(vc));
		memset(square,0,sizeof(square));
		//initialize 
		for (int i = 0; i < n; i++)
		{
			scanf("%s%d%d",s,&a,&b);
			if(s[0]=='H') H[a][b]++;
			else V[b][a]++;//存下横和竖 
		}//看清!!!v[b][a]保证 V与H 都是先行后列储存 
		for (int i = m; i >= 1; i--)//从后往前 
		  for (int j = m; j>=1; j--)
		  {
		  	if(H[i][j]) hc[i][j] = hc[i][j+1]+1;
		  	if(V[i][j]) vc[i][j] = vc[i+1][j]+1;
		  }//存下其该点最长的延伸 
		//一波查找猛如虎 
		for (int i = 1; i <= m; i++)
		 for (int j = 1; j <= m; j++)
		 {
		 	int maxn = min(hc[i][j],vc[i][j]);
		 	for (int z = 1;z <= maxn; z++)
		 	{
		 		if(vc[i][j+z] >= z && hc[i+z][j] >= z)
		 		{
		 			square[z]++;
		 		} //自己画个图就知道啦为什么是 j+z 与 i+z 
		 	}
		 } 
	   int ok = 0;
	   printf("Problem #%d\n\n",t);//小心输出
	   for (int i = 1; i <= m; i++)
	   {
	   		if(square[i])
	   		{
	   			ok = 1;
	   			printf("%d square (s) of size %d\n",square[i],i);
	   		}
	   }
	   if(!ok) printf("No completed squares can be found.\n");
	}
	return 0;
} 
Published 55 original articles · won praise 1 · views 2668

Guess you like

Origin blog.csdn.net/qq_37548017/article/details/99550104