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.
Input: X1 = 0, Y1 = 0, X2 = 1, Y2 = 1, X3 = 2 and Y3 = 2
(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 = 1
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:
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
- Find whether there is path between two cells in matrix
- Minimum cells to be flipped to get a 2*2 submatrix with equal elements
- Minimum cells traversed to reach corner where every cell represents jumps
- Minimum cells required to reach destination with jumps equal to cell values
- Find safe cells in a matrix
- Find the number of cells in the table contains X
- Unique cells in a binary matrix
- Shortest distance between two cells in a matrix or grid
- Print cells with same rectangular sums in a matrix
- Number of cells in matrix which are equidistant from given two points
- Find Number of Even cells in a Zero Matrix after Q queries
- Number of cells in a matrix that satisfy the given condition
- Number of cells in the right and left diagonals passing through (x, y) in a matrix
- Number of cells a queen can move with obstacles on the chessborad
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Improved By : AnkitRai01