## Sunday, May 29, 2016

### Count the squares

https://www.codechef.com/problems/D6
The problem is: given a set of N points in the plane, how many squares are there such that all their corners belong to this set?
The first line contains t, the number of test cases (about 10). Then t test cases follow.
Each test case has the following form:
• The first line contains an integer N, the number of points in the given set (4 ≤ N ≤ 500).
• Then N lines follow, each line contains two integers X, Y describing coordinates of a point (-50 ≤ X, Y ≤ 50).

### Output

For each test case, print in a single line the number of squares that have vertices belong to the given set.

### Example

```Input:
1
7
0 0
0 1
1 0
1 1
1 2
2 1
2 2

Output:
3

Output details:
The three squares are:
(0 0), (0 1), (1 1), (1 0)
(1 1), (1 2), (2 2), (2 1)
(0 1), (1 0), (2 1), (1 2)```

https://www.codechef.com/status/D6
https://www.codechef.com/viewsolution/10228421
1. public static void main(String[] args) throws IOException {
2. Locale.setDefault(Locale.US);
3. PrintWriter out = new PrintWriter(System.out,true);
4. Point c = new Point();
5. Point d = new Point();
7. for (int i=0; i<n; i++) {
9. List<Point> points = new ArrayList<Point>(k);
10. Set<Point> set = new HashSet<Point>(2*k);
11. for (int j=0; j<k; j++) {
13. int index = s.indexOf(' ');
14. int x = Integer.parseInt(s.substring(0,index));
15. int y = Integer.parseInt(s.substring(index+1));
16. Point p = new Point(x,y);
19. }
20. int count = 0;
21. for (int x=0; x<k; x++) {
22. Point a = points.get(x);
23. for (int y=x+1; y<k; y++) {
24. Point b = points.get(y);
25. int dx = b.x-a.x;
26. int dy = b.y-a.y;
27. c.x = b.x-dy;
28. c.y = b.y+dx;
29. d.x = c.x-dx;
30. d.y = c.y-dy;
31. if (set.contains(c) && set.contains(d)) {
32. count++;
33. }
34. c.x = b.x+dy;
35. c.y = b.y-dx;
36. d.x = c.x-dx;
37. d.y = c.y-dy;
38. if (set.contains(c) && set.contains(d)) {
39. count++;
40. }
41. }
42. }
43. out.println(count/4);
44. }
45. out.close();
46. }