Count how many squares are there in the following figure.

Easy validation by Python using Bruteforce, checking every possible pair of points (lower-left, and upper right)
#!/usr/bin/env python
# https://helloacm.com
def count(pts):
pts.sort()
cnt = 0
sz = len(pts)
for x in xrange(sz):
u = pts[x]
for y in xrange(x + 1, sz):
v = pts[y]
if v[0] - u[0] == v[1] - u[1]:
cnt += 1
print u, v
return cnt
def compute():
pts = []
for x in xrange(5):
for y in xrange(5):
pts.append((x, y))
cnt1 = count(pts)
pts = []
for x in xrange(3):
for y in xrange(3):
pts.append((1.5 + x * 0.5, 0.5 + y * 0.5))
cnt2 = count(pts)
pts = []
for x in xrange(3):
for y in xrange(3):
pts.append((1.5 + x * 0.5, 2.5 + y * 0.5))
cnt3 = count(pts)
print "total = ", cnt1 + cnt2 + cnt3
if __name__ == "__main__":
compute()
The Output is:
(0, 0) (1, 1)
(0, 0) (2, 2)
(0, 0) (3, 3)
(0, 0) (4, 4)
(0, 1) (1, 2)
(0, 1) (2, 3)
(0, 1) (3, 4)
(0, 2) (1, 3)
(0, 2) (2, 4)
(0, 3) (1, 4)
(1, 0) (2, 1)
(1, 0) (3, 2)
(1, 0) (4, 3)
(1, 1) (2, 2)
(1, 1) (3, 3)
(1, 1) (4, 4)
(1, 2) (2, 3)
(1, 2) (3, 4)
(1, 3) (2, 4)
(2, 0) (3, 1)
(2, 0) (4, 2)
(2, 1) (3, 2)
(2, 1) (4, 3)
(2, 2) (3, 3)
(2, 2) (4, 4)
(2, 3) (3, 4)
(3, 0) (4, 1)
(3, 1) (4, 2)
(3, 2) (4, 3)
(3, 3) (4, 4)
(1.5, 0.5) (2.0, 1.0)
(1.5, 0.5) (2.5, 1.5)
(1.5, 1.0) (2.0, 1.5)
(2.0, 0.5) (2.5, 1.0)
(2.0, 1.0) (2.5, 1.5)
(1.5, 2.5) (2.0, 3.0)
(1.5, 2.5) (2.5, 3.5)
(1.5, 3.0) (2.0, 3.5)
(2.0, 2.5) (2.5, 3.0)
(2.0, 3.0) (2.5, 3.5)
total = 40
–EOF (The Ultimate Computing & Technology Blog) —
Last Post: Using Dictionary Object in VBScript
Next Post: Codeforces: B. New Problem