1.

/**
* Angewandte Mathematik, SS11
* Problem: 201 - Squares
* http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=4&page=show_problem&problem=137
* @author Sebastian Stumpf
* @author Benjamin Vogt
* @version 1.0, 2011-05-02 21:51:44
*
* Method : Ad-Hoc
* Status : Accepted
* Runtime: 1.200
*/

import java.io.*;
import java.util.*;

public class Main
{
public static void main(String[] args) throws Exception
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tokenizer;
int numberOfProblem = 0;
do
{
int n = Integer.parseInt(reader.readLine());
int m = Integer.parseInt(reader.readLine());
boolean [][] horizontal = new boolean[n][n-1];
boolean [][] vertical = new boolean[n][n-1];
boolean found = false;
if(++numberOfProblem > 1)
System.out.println("\n**********************************\n");
System.out.println("Problem #"+numberOfProblem+"\n");

for(int i = 0; i < m; ++i)
{
// this is the ridiculous reason for getting compilation error,
// i didnt have an try catch here, cause if you believe the description
// of the problem, you should not have a exception causing input in here!
// but it seems as if you mustnt believe the description...
// after testing and testing i know that here is a ArrayIndexOutOfBounds Exception thrown
// that indeed means, there has to be a corrupt input with numbers greater than n
try
{
tokenizer = new StringTokenizer(reader.readLine());
if(tokenizer.nextToken().equals("H"))
{
horizontal[Integer.valueOf(tokenizer.nextToken())-1]
[Integer.valueOf(tokenizer.nextToken())-1] = true;
}
else
{
vertical[Integer.valueOf(tokenizer.nextToken())-1]
[Integer.valueOf(tokenizer.nextToken())-1] = true;
}
}catch(ArrayIndexOutOfBoundsException aioobe){}
}
// counts squares of a size
for(int size = 1; size <= n; ++size)
{
int count = 0;
// gives an element of horizontal[i][j] and vertical[j][i]
for(int i = 0; i < n-size; ++i)
{
for(int j = 0; j < n-size; ++j)
{
boolean square = true;
// checks for a size, if all the lines, needed to produce a square of that size are drawn
// example: square of size 2 from the knot k(1,3)
// h[1][3+0] && h[1+2][3+0] && v[3][1+0] && v[3+2][1+0] &&
// h[1][3+1] && h[1+2][3+1] && v[3][1+1] && v[3+2][1+1] ==> square exists
for(int k = 0; k < size; ++k)
{
square = square && horizontal[i][j+k] && horizontal[i+size][j+k] && vertical[j][i+k] && vertical[j+size][i+k];
}
count += square? 1: 0;
}
}
if(count > 0)
{
System.out.println(count+" square (s) of size "+size);
found = true;
}
}
if(!found)
{
System.out.println("No completed squares can be found.");
}
}
while(reader.ready());
}
}

-------------------------------------------------------------------------------------------------------


1.
/**
* FWP: Ausgewaehlte Probleme aus dem ACM (SS10)
*
* Method: ad hoc
* Problem: 201 - Squares
* Accepted: 1.932
* @author Evgeni Pavlidis
*
*/
import java.util.*;
import java.io.*;

public class Main {

// horizontal/vertical constants
private static int H = 0;
private static int V = 1;

// saves the horizontal/vertical connections between:
// x y and x+1 y if horizontal
// x y and x y + 1 if vertical
private static boolean[][][] square;
private static int n;

private static boolean check(int y, int x, int size)
{
// check horizontal
for(int s = 0; s < size; s++)
if(!square[H][y][x+s] || !square[H][y+size][x+s])
return false;

// check vertical
for(int s = 0; s < size; s++)
if(!square[V][x][y+s] || !square[V][x+size][y+s])
return false;

return true;
}

public static void main(String...args)
{
Scanner sc = new Scanner(System.in);
int problemNumber = 1;
boolean found;

while(sc.hasNextInt())
{
if(problemNumber > 1)
System.out.println("\n**********************************\n");
System.out.println("Problem #" + problemNumber++ + "\n");

n = sc.nextInt();
square = new boolean[2][n+1][n+1];
int inputs = sc.nextInt();
String orientation;
int counter, a, b;


for(int i = 0; i < inputs; i++)
{
orientation = sc.next();
a = sc.nextInt();
b = sc.nextInt();
if(orientation.equals("H"))
square[H][a][b] = true;
else
square[V][a][b] = true;
}


found = false;
for(int size = 1; size <= n; size++)
{
counter = 0;

for(int y = 1; y <= n-size; y++)
for(int x = 1; x <= n-size; x++)
if(check(y,x,size))
counter++;

if(counter > 0)
{
found = true;
System.out.printf("%d square (s) of size %d\n", counter, size);
}

}

if(!found)
System.out.println("No completed squares can be found.");
}
}
}