Given coordinates of 3 cells **(X1, Y1)**, **(X2, Y2)** and **(X3, Y3)** of a matrix. The task is to find the minimum path which connects all three of these cells and print the count of all the cells that are connected through this path.**Note:** Only possible moves are up, down, left and right.

**Examples:**

Input:X1 = 0, Y1 = 0, X2 = 1, Y2 = 1, X3 = 2 and Y3 = 2Output:5

(0, 0), (1, 0), (1, 1), (1, 2), (2, 2) are the required cells.

Input:X1 = 0, Y1 = 0, X2 = 2, Y2 = 0, X3 = 1 and Y3 = 1Output:4

**Approach:** First sort the cells from the one with minimum row number at first to one with maximum row number at last. Also, store minimum column number and maximum column number among these three cells in variable **MinCol** and **MaxCol** respectively.

After that, store row number of the middle cell(from sorted cells) in variable **MidRow** and mark all the cells of this **MidRow** from **MinCol** to **MaxCol**.

Now our final step will be to mark all the column number of 1st and 3rd cell till they reach **MidRow**.

Here, marking means we will store the required cells coordinate in a set. Thus, our answer will be size of this set.

Below is the implementation of the above approach:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; `
` ` `// Function to return the minimum cells that are ` `// connected via the minimum length path ` `int` `Minimum_Cells(vector<pair<` `int` `, ` `int` `> > v) `
`{ ` ` ` `int` `col[3], i, j; `
` ` `for` `(i = 0; i < 3; i++) { `
` ` `int` `column_number = v[i].second; `
` ` ` ` `// Array to store column number `
` ` `// of the given cells `
` ` `col[i] = column_number; `
` ` `} `
` ` ` ` `sort(col, col + 3); `
` ` ` ` `// Sort cells in ascending `
` ` `// order of row number `
` ` `sort(v.begin(), v.end()); `
` ` ` ` `// Middle row number `
` ` `int` `MidRow = v[1].first; `
` ` ` ` `// Set pair to store required cells `
` ` `set<pair<` `int` `, ` `int` `> > s; `
` ` ` ` `// Range of column number `
` ` `int` `Maxcol = col[2], MinCol = col[0]; `
` ` ` ` `// Store all cells of middle row `
` ` `// within column number range `
` ` `for` `(i = MinCol; i <= Maxcol; i++) { `
` ` `s.insert({ MidRow, i }); `
` ` `} `
` ` ` ` `for` `(i = 0; i < 3; i++) { `
` ` `if` `(v[i].first == MidRow) `
` ` `continue` `; `
` ` ` ` `// Final step to store all the column number `
` ` `// of 1st and 3rd cell upto MidRow `
` ` `for` `(j = min(v[i].first, MidRow); `
` ` `j <= max(v[i].first, MidRow); j++) { `
` ` `s.insert({ j, v[i].second }); `
` ` `} `
` ` `} `
` ` ` ` `return` `s.size(); `
`} ` ` ` `// Driver Function ` `int` `main() `
`{ ` ` ` `// vector pair to store X, Y, Z `
` ` `vector<pair<` `int` `, ` `int` `> > v = { { 0, 0 }, { 1, 1 }, { 2, 2 } }; `
` ` ` ` `cout << Minimum_Cells(v); `
` ` ` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

`# Python3 implementation of the approach ` ` ` `# Function to return the minimum cells that ` `# are connected via the minimum length path ` `def` `Minimum_Cells(v) : `
` ` ` ` `col ` `=` `[` `0` `] ` `*` `3`
` ` `for` `i ` `in` `range` `(` `3` `) : `
` ` `column_number ` `=` `v[i][` `1` `] `
` ` ` ` `# Array to store column number `
` ` `# of the given cells `
` ` `col[i] ` `=` `column_number `
` ` ` ` `col.sort() `
` ` ` ` `# Sort cells in ascending order `
` ` `# of row number `
` ` `v.sort() `
` ` ` ` `# Middle row number `
` ` `MidRow ` `=` `v[` `1` `][` `0` `] `
` ` ` ` `# Set pair to store required cells `
` ` `s ` `=` `set` `() `
` ` ` ` `# Range of column number `
` ` `Maxcol ` `=` `col[` `2` `] `
` ` `MinCol ` `=` `col[` `0` `] `
` ` ` ` `# Store all cells of middle row `
` ` `# within column number range `
` ` `for` `i ` `in` `range` `(MinCol, ` `int` `(Maxcol) ` `+` `1` `) : `
` ` `s.add((MidRow, i)) `
` ` ` ` `for` `i ` `in` `range` `(` `3` `) : `
` ` `if` `(v[i][` `0` `] ` `=` `=` `MidRow) : `
` ` `continue` `; `
` ` ` ` `# Final step to store all the column `
` ` `# number of 1st and 3rd cell upto MidRow `
` ` `for` `j ` `in` `range` `(` `min` `(v[i][` `0` `], MidRow), `
` ` `max` `(v[i][` `0` `], MidRow) ` `+` `1` `) : `
` ` `s.add((j, v[i][` `1` `])); `
` ` ` ` `return` `len` `(s) `
` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: `
` ` ` ` `# vector pair to store X, Y, Z `
` ` `v ` `=` `[(` `0` `, ` `0` `), ( ` `1` `, ` `1` `), ( ` `2` `, ` `2` `)] `
` ` ` ` `print` `(Minimum_Cells(v)) `
` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

**Output:**

5

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Check if cells numbered 1 to K in a grid can be connected after removal of atmost one blocked cell
- Color a grid such that all same color cells are connected either horizontally or vertically
- Min number of moves to traverse entire Matrix through connected cells with equal values
- Size of all connected non-empty cells of a Matrix
- Queries to find the count of connected Non-Empty Cells in a Matrix with updates
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
- Count of cells in a matrix whose adjacent cells's sum is prime Number
- Find whether there is path between two cells in matrix
- Maximize path sum from top-left cell to all other cells of a given Matrix
- Divide Matrix into K groups of adjacent cells having minimum difference between maximum and minimum sized groups
- Minimum Distance from a given Cell to all other Cells of a Matrix
- Minimum number of Water to Land conversion to make two islands connected in a Grid
- Kth largest node among all directly connected nodes to the given node in an undirected graph
- Minimum cells traversed to reach corner where every cell represents jumps
- Minimum cells required to reach destination with jumps equal to cell values
- Minimum cells to be flipped to get a 2*2 submatrix with equal elements
- Queries to return the absolute difference between L-th smallest number and the R-th smallest number
- Shortest distance between two cells in a matrix or grid
- Number of cells in a matrix that satisfy the given condition
- Number of cells in matrix which are equidistant from given two points

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.