Minimum Numbers of cells that are connected with the smallest path between 3 given cells
Last Updated :
31 Jan, 2023
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 = 2
Output: 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 = 1
Output: 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++
#include <bits/stdc++.h>
using namespace std;
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;
col[i] = column_number;
}
sort(col, col + 3);
sort(v.begin(), v.end());
int MidRow = v[1].first;
set<pair< int , int > > s;
int Maxcol = col[2], MinCol = col[0];
for (i = MinCol; i <= Maxcol; i++) {
s.insert({ MidRow, i });
}
for (i = 0; i < 3; i++) {
if (v[i].first == MidRow)
continue ;
for (j = min(v[i].first, MidRow);
j <= max(v[i].first, MidRow); j++) {
s.insert({ j, v[i].second });
}
}
return s.size();
}
int main()
{
vector<pair< int , int > > v = { { 0, 0 }, { 1, 1 }, { 2, 2 } };
cout << Minimum_Cells(v);
return 0;
}
|
Java
import java.util.*;
import java.awt.Point;
public class Main
{
static int Minimum_Cells(Vector<Point> v)
{
int [] col = new int [ 3 ];
int i, j;
for (i = 0 ; i < 3 ; i++) {
int column_number = v.get(i).y;
col[i] = column_number;
}
Arrays.sort(col);
int MidRow = v.get( 1 ).x;
Set<Point> s = new HashSet<Point>();
int Maxcol = col[ 2 ], MinCol = col[ 0 ];
for (i = MinCol; i <= Maxcol; i++) {
s.add( new Point(MidRow, i));
}
for (i = 0 ; i < 3 ; i++) {
if (v.get(i).x == MidRow)
continue ;
for (j = Math.min(v.get(i).x, MidRow);
j <= Math.max(v.get(i).x, MidRow); j++) {
s.add( new Point(j, v.get(i).x));
}
}
return s.size();
}
public static void main(String[] args)
{
Vector<Point> v = new Vector<Point>();
v.add( new Point( 0 , 0 ));
v.add( new Point( 1 , 1 ));
v.add( new Point( 2 , 2 ));
System.out.print(Minimum_Cells(v));
}
}
|
Python3
def Minimum_Cells(v) :
col = [ 0 ] * 3
for i in range ( 3 ) :
column_number = v[i][ 1 ]
col[i] = column_number
col.sort()
v.sort()
MidRow = v[ 1 ][ 0 ]
s = set ()
Maxcol = col[ 2 ]
MinCol = col[ 0 ]
for i in range (MinCol, int (Maxcol) + 1 ) :
s.add((MidRow, i))
for i in range ( 3 ) :
if (v[i][ 0 ] = = MidRow) :
continue ;
for j in range ( min (v[i][ 0 ], MidRow),
max (v[i][ 0 ], MidRow) + 1 ) :
s.add((j, v[i][ 1 ]));
return len (s)
if __name__ = = "__main__" :
v = [( 0 , 0 ), ( 1 , 1 ), ( 2 , 2 )]
print (Minimum_Cells(v))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int Minimum_Cells(List<Tuple< int , int >> v)
{
int [] col = new int [3];
int i, j;
for (i = 0; i < 3; i++) {
int column_number = v[i].Item2;
col[i] = column_number;
}
Array.Sort(col);
v.Sort();
int MidRow = v[1].Item1;
HashSet<Tuple< int , int >> s = new HashSet<Tuple< int , int >>();
int Maxcol = col[2], MinCol = col[0];
for (i = MinCol; i <= Maxcol; i++) {
s.Add( new Tuple< int , int >(MidRow, i));
}
for (i = 0; i < 3; i++) {
if (v[i].Item1 == MidRow)
continue ;
for (j = Math.Min(v[i].Item1, MidRow);
j <= Math.Max(v[i].Item1, MidRow); j++) {
s.Add( new Tuple< int , int >(j, v[i].Item1));
}
}
return s.Count;
}
static void Main()
{
List<Tuple< int , int >> v = new List<Tuple< int , int >>();
v.Add( new Tuple< int , int >(0, 0));
v.Add( new Tuple< int , int >(1, 1));
v.Add( new Tuple< int , int >(2, 2));
Console.Write(Minimum_Cells(v));
}
}
|
Javascript
<script>
function Minimum_Cells(v)
{
let col = new Array(3);
let i, j;
for (i = 0; i < 3; i++) {
let column_number = v[i][1];
col[i] = column_number;
}
col.sort( function (a, b){ return a - b});
v.sort();
let MidRow = v[1][0];
let s = new Set();
let Maxcol = col[2], MinCol = col[0];
for (i = MinCol; i <= Maxcol; i++) {
s.add([MidRow, i]);
}
for (i = 0; i < 3; i++) {
if (v[i][0] == MidRow)
continue ;
for (j = Math.min(v[i][0], MidRow);
j <= Math.max(v[i][0], MidRow); j++) {
s.add([j, v[i][0]]);
}
}
return s.size-2;
}
let v = [];
v.push([0, 0]);
v.push([1, 1]);
v.push([2, 2]);
document.write(Minimum_Cells(v));
</script>
|
Time complexity: O(nlogn) as it uses the sort function
Space complexity: O(n) as it uses a set.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...