Given the coordinates of 5 2-dimensional points, check if they form a closed T shape. Print ‘Yes’ if they form T shape and ‘No’ otherwise. **Note:** Coordinates should be distinct and integers.

There are 4-types of T shaped formations possible according to the given conditions:

**Examples:**

Input: [[7, 5], [8, 5], [6, 5], [7, 7], [7, 6]] Output: Yes Input: [[0, 0], [1, 0], [2, 0], [1, -1], [1, -2]] Output: Yes

**Approach:**

- Consider the first point in the given list as the centre (x, y) (i.e intersection of the two lines that form T-shape).
- Then check if all the points which are needed to form T-shape of which (x, y) is the centre are present in the list of given points or not.
- Check this for all the 4 possible patterns of T-shape.

Repeat this process from step-1 for all points in the list until a solution is found.

**Below is the implementation of the above approach:**

`# Python3 code to check if given 5 ` `# 2-D points form T-shape or not ` ` ` `# Import the function to initialize the ` `# dictionary with a specific value ` `from` `collections ` `import` `defaultdict ` ` ` `# This function checks if the points ` `# form T-shape pointing up ` `def` `isUpDirected(point, x, y): ` ` ` ` ` `return` `(point[(x` `-` `1` `, y)] ` `and` ` ` `point[(x, y)] ` `and` ` ` `point[(x ` `+` `1` `, y)] ` `and` ` ` `point[(x, y` `-` `1` `)] ` `and` ` ` `point[(x, y` `-` `2` `)]) ` ` ` `# This function checks if the points ` `# form T-shape pointing down ` `def` `isDownDirected(point, x, y): ` ` ` ` ` `return` `(point[(x` `-` `1` `, y)] ` `and` ` ` `point[(x, y)] ` `and` ` ` `point[(x ` `+` `1` `, y)] ` `and` ` ` `point[(x, y ` `+` `1` `)] ` `and` ` ` `point[(x, y ` `+` `2` `)]) ` ` ` `# This function checks if the points ` `# form T-shape pointing left ` `def` `isLeftDirected(point, x, y): ` ` ` ` ` `return` `(point[(x, y ` `+` `1` `)] ` `and` ` ` `point[(x, y)] ` `and` ` ` `point[(x, y` `-` `1` `)] ` `and` ` ` `point[(x ` `+` `1` `, y)] ` `and` ` ` `point[(x ` `+` `2` `, y)]) ` ` ` `# This function checks if the points ` `# form T-shape pointing right ` `def` `isRightDirected(point, x, y): ` ` ` ` ` `return` `(point[(x, y ` `+` `1` `)] ` `and` ` ` `point[(x, y)] ` `and` ` ` `point[(x, y` `-` `1` `)] ` `and` ` ` `point[(x` `-` `1` `, y)] ` `and` ` ` `point[(x` `-` `2` `, y)]) ` ` ` ` ` `# This function checks if given points ` `# form a T-shape or not ` `def` `solve(grid): ` ` ` ` ` `# Initialize the dictionary with False value ` ` ` `point ` `=` `defaultdict(` `lambda` `: ` `False` `) ` ` ` `flag ` `=` `False` ` ` ` ` `for` `i ` `in` `range` `(` `len` `(grid)): ` ` ` ` ` `# Assign True value to the points which ` ` ` `# are present in the given list ` ` ` `point[(grid[i][` `0` `], grid[i][` `1` `])] ` `=` `True` ` ` ` ` `for` `i ` `in` `range` `(` `len` `(grid)): ` ` ` ` ` `# Check if the given points form any of the ` ` ` `# 4 possible T-shaped formations ` ` ` `if` `isUpDirected(point, grid[i][` `0` `], grid[i][` `1` `]) \ ` ` ` `or` `isDownDirected(point, grid[i][` `0` `], grid[i][` `1` `]) \ ` ` ` `or` `isLeftDirected(point, grid[i][` `0` `], grid[i][` `1` `]) \ ` ` ` `or` `isRightDirected(point, grid[i][` `0` `], grid[i][` `1` `]): ` ` ` ` ` `flag ` `=` `True` ` ` `break` ` ` ` ` `if` `flag ` `=` `=` `True` `: ` ` ` `return` `'Yes'` ` ` `else` `: ` ` ` `return` `'No'` ` ` `print` `(solve([[` `7` `, ` `5` `], [` `8` `, ` `5` `], [` `6` `, ` `5` `], [` `7` `, ` `7` `], [` `7` `, ` `6` `]])) ` `print` `(solve([[` `0` `, ` `0` `], [` `1` `, ` `0` `], [` `2` `, ` `0` `], [` `1` `, ` `-` `1` `], [` `1` `, ` `-` `2` `]])) ` |

*chevron_right*

*filter_none*

**Output:**

Yes Yes

**Time Complexity:** O(1)

## Recommended Posts:

- Count of obtuse angles in a circle with 'k' equidistant points between 2 given points
- Find the point on X-axis from given N points having least Sum of Distances from all other points
- How to check if given four points form a square
- Number of Integral Points between Two Points
- Prime points (Points that split a number into two primes)
- Minimum number of points to be removed to get remaining points on one side of axis
- Ways to choose three points with distance between the most distant points <= L
- Steps required to visit M points in order on a circular ring of N points
- Check whether two points (x1, y1) and (x2, y2) lie on same side of a given line or not
- Check whether triangle is valid or not if three points are given
- Check if the given point lies inside given N points of a Convex Polygon
- Number of lines from given N points not parallel to X or Y axis
- Find Four points such that they form a square whose sides are parallel to x and y axes
- Check whether it is possible to join two points given on circle such that distance between them is k
- Find whether only two parallel lines contain all coordinates points or not
- Find points at a given distance on a line of given slope
- Convert a given temperature to another system based on given boiling and freezing points
- Count rectangles generated in a given rectangle by lines drawn parallel to X and Y axis from a given set of points
- Program to check if three points are collinear
- Check whether four points make a parallelogram

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.