UVa 201 - Squares

201 - Squares

 * http://acm.uva.es/p/v2/201.html

Summary
Bruteforce. Since $$N \leq 9$$, we can simply iterate all the possible squares.

Explanation
We can think that vertical or horizontal lines are edges between two adjecent point. After that 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 at 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 is 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 this just by subtarcting values calculated above.

Input
4 16 H 1 1 H 1 3 H 2 1 H 2 2 H 2 3 H 3 2 H 4 2 H 4 3 V 1 1 V 2 1 V 2 2 V 2 3 V 3 2 V 4 1 V 4 2 V 4 3 2 3 H 1 1 H 2 1 V 2 1 4 16 H 1 1 H 1 3 H 2 1 H 2 2 H 2 3 H 3 2 H 4 2 H 4 3 V 1 1 V 2 1 V 2 2 V 2 3 V 3 2 V 4 1 V 4 2 V 4 3 2 3 H 1 1 H 2 1 V 2 1 4 16 H 1 1 H 1 3 H 2 1 H 2 2 H 2 3 H 3 2 H 4 2 H 4 3 V 1 1 V 2 1 V 2 2 V 2 3 V 3 2 V 4 1 V 4 2 V 4 3 2 3 H 1 1 H 2 1 V 2 1

Output
Problem #1

2 square (s) of size 1 1 square (s) of size 2



Problem #2

No completed squares can be found.



Problem #3

2 square (s) of size 1 1 square (s) of size 2



Problem #4

No completed squares can be found.



Problem #5

2 square (s) of size 1 1 square (s) of size 2



Problem #6

No completed squares can be found. CODE:::: using namespace std;
 * 1) include
 * 2) include
 * 3) include
 * 4) include
 * 5) include
 * 6) include
 * 7) include
 * 8) include
 * 9) include
 * 10) include
 * 11) include
 * 12) include
 * 13) include
 * 14) include
 * 15) include
 * 16) include
 * 17) include


 * 1) define INT_MAX 2147483647
 * 2) define INT_MIN -2147483647
 * 3) define pi acos(-1.0)
 * 4) define N 1000000
 * 5) define LL long long

char board [9 + 3] [9 + 3]; int freq [10 + 2]; int n; void reset {   for ( int i = 0; i < 12; i++ ) {       for ( int j = 0; j < 12; j++ ) board [i] [j] = '.'; }   memset (freq, 0, sizeof (freq)); }

void isSquare (int r, int c) { int next_r = r + 1; int next_c = c + 1; int size = 0; bool complete; while ( next_r <= n && next_c <= n ) { size++; complete = true;

if ( board [next_r - 1] [c] == 'H' || board [next_r - 1] [c] == '.' ) break; if ( board [r] [next_c - 1] == 'V' || board [r] [next_c - 1] == '.' ) break; for ( int i = c; i < c + size; i++ ) if ( board [next_r] [i] != 'H' && board [next_r] [i] != 'B' ) complete = false;

for ( int i = r; i < r + size; i++ ) if ( board [i] [next_c] != 'V' && board [i] [next_c] != 'B' ) complete = false; if ( complete ) freq [size]++; next_r++; next_c++; }	}	int main {	   int cases = 0; int asterisk = false; while ( scanf ("%d", &n) != EOF ) { reset ; int m;       scanf ("%d", &m); getchar ; char line_info [50]; char ch; int p, q;	       for ( int i = 0; i < m; i++ ) { gets (line_info); sscanf (line_info, "%c %d %d", &ch, &p, &q); if ( ch == 'H' ) { if ( board [p] [q] == 'V' ) board [p] [q] = 'B'; else board [p] [q] = 'H'; }	           else { if ( board [q] [p] == 'H' ) board [q] [p] = 'B'; else board [q] [p] = 'V'; }	       }	        for ( int i = 1; i < n; i++ ) { for ( int j = 1; j < n; j++ ) if ( board [i] [j] == 'B' ) isSquare (i, j); }	       if ( asterisk ) printf ("\n**********************************\n\n"); asterisk = true; printf ("Problem #%d\n\n", ++cases); if ( accumulate (freq, freq + 12, 0) == 0 ) printf ("No completed squares can be found.\n"); else { for ( int i = 1; i < 12; i++ ) if ( freq [i] ) printf ("%d square (s) of size %d\n", freq [i], i); }	   }	    return 0; }