Equilateral Triangles
Time limit2sMemory limit512 MB
Given an N by N grid of cows, count triples of cows that are pairwise equidistant under Manhattan distance, with N up to 300.
- Level
Medium6 of 10
- Topics
- Math, Hash map, Implementation
- Solved
- No attempts yet
Problem
Farmer John's pasture is an square grid with positions for all . A cell contains '*' if a single cow stands at that position, and '.' if no cow is there.
FJ believes the beauty of his pasture is directly proportional to the number of triples of cows whose positions are equidistant from each other, that is, triples that form an equilateral triangle. All cows are at integer coordinates, so no such triple can exist under Euclidean distance. FJ therefore switches to Manhattan distance. The Manhattan distance between and is .
Given the grid, count the equilateral triples.
Input
The first line contains a single integer . For each , line contains a string of length made of the characters '*' and '.'. The th character describes whether there is a cow at position .
Output
Output a single integer, the answer. The answer fits into a signed 32-bit integer.
Hint
There are three cows, and they form an equilateral triple because the Manhattan distance between each pair of cows is equal to two.