UVa 201 Squares

Source: Internet
Author: User

Test instructions

Give a diagram of how many squares of different sizes or positions are different.

Analysis:

This kind of question has the idea, the first idea is enumerates the square position, needs the double circulation, the enumeration side long one heavy circulation, the judgment is the square and needs a heavy circulation, the complexity is O (N4), for n≤9, this complexity is acceptable.

You can use the time of O (1) to determine whether it is a square, as in the preprocessing prefix and so the total complexity is optimized to O (N3).

This method is transferred from here

We can think that vertical or horizontal lines is edges between-adjecent point. After, we can take a three dimensional array (say a [n][n][2]) to store the count of horizontal (a[i][j][0]) edges and Vertical (a[i][j][1]) edges. A[i][j][0] contains number of horizontal edges at row i upto Coloumn J. and A[i][j][1] contains number of vertical edges a T Coloumn j upto Row I. Next You use a O (n^2) loop to find a square. A square of size 1 is found if there are an edge from (i,j) to (i,j+1) and (i,j+1) to (i+1,j+1) and (I,j) to (I+1,J) and (i +1,J) to (i+1,j+1) we can get the just by subtracting values calculated above.

For example, A[i][j][0] indicates that on line I, from the first column to the number of horizontal sides of column J, if A[I][J+L][0]-a[i][j][0], the description point (i, J) to (I, j+l) has a horizontal line length of L.

I was also entered into the pit, notice which number represents the row and which number represents the column after VH.

1#include <cstdio>2#include <cstring>3 4 Const intMAXN =Ten;5 BOOLg[2][MAXN][MAXN];6 inta[2][MAXN][MAXN], CNT[MAXN];7 8 intMain ()9 {Ten     //freopen ("In.txt", "R", stdin); One     intN, m, Kase =0; A      while(SCANF ("%d", &n) = =1&&N) -     { -memset (G,false,sizeof(G)); theMemset (A,0,sizeof(a)); -memset (CNT,0,sizeof(CNT)); -scanf"%d", &m); - GetChar (); +          for(intK =0; K < M; ++k) -         { +             CharC; A             intI, J; atscanf"%c%d%d", &c, &i, &j); - GetChar (); -             if(c = ='H') g[0][i][j+1] =true; -             Elseg[1][j+1][i] =true; -         } -          for(inti =1; I <= N; ++i) in              for(intj =1; J <= N; ++j) -             { toa[0][I][J] = a[0][i][j-1] + g[0][i][j]; +a[1][I][J] = a[1][i-1][J] + g[1][i][j]; -             } the  *          for(inti =1; I < n; ++i) $              for(intj =1; J < N; ++J)//enumerates the upper-left corner of a squarePanax Notoginseng                  for(intL =1; I+l<=n && j+l<=n; ++L)//enumerating the side length of a square -                     if(a[0][i][j+l]-a[0][I][J] = = L && a[0][i+l][j+l]-a[0][I+L][J] = =L the&& a[1][i+l][j]-a[1][I][J] = = L && a[1][i+l][j+l]-a[1][I][J+L] = =l) +cnt[l]++; A  the         if(Kase) printf ("\n**********************************\n\n"); +printf"problem #%d\n\n", ++Kase); -         BOOLFlag =false; $          for(inti =1; I <= N; ++i)if(Cnt[i]) $         { -printf"%d square (s) of size%d\n", Cnt[i], i); -Flag =true; the         } -         if(!flag) puts ("No completed squares can be found.");Wuyi     } the  -     return 0; Wu}
code June

UVa 201 Squares

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.