top of page

Daily Coding Problem #32

Writer's picture: Mischievous_FaceMischievous_Face

// Given a 2-D matrix representing an image, a location of a pixel in the screen and a color C,

// replace the color of the given pixel and all adjacent same colored pixels with C.

// I had a bit more time to work on this so I added more detail and optimizations than usual

// *Note*: my coordinates are reversed and my point of origin (0,0) is in the "top left corner" of the matrix

// Meaning that positive y values go further down the list of rows, and larger positive x values go further into the rows index


#include <iostream>

#include <string>

#include <vector>


using namespace std;


void ChangePxColor(vector<vector<string>>& matrix, const string c, pair<int,int> p)

{

int rows = matrix.size();

int cols = matrix[0].size();

int i;

const string oldC = matrix[p.first][p.second];


// ensure point is in bounds

if (p.first < 0 || p.first >= rows || p.second < 0 || p.second >= cols)

return;


matrix[p.first][p.second] = c;

//cout << "Rows: " << rows << endl;

//cout << "Cols: " << cols << endl;

cout << "Changed " << p.first << ", " << p.second << endl;


// check the top row

if (p.first > 0)

{

// -1, 0, 1

for (i = -1; i < 2; ++i)

{

int x = p.second + i;

int y = p.first - 1;

// always check that were inside the matrix

if (x >= 0 && x < cols)

{

// check for match and replace color

if (matrix[y][x] == oldC)

ChangePxColor(matrix, c, pair<int, int>(y, x));

}

}

} // else: theres no top row


// check the middle row

if (p.second > 0)

{

// we need to check left cell

if (matrix[p.first][p.second - 1] == oldC)

ChangePxColor(matrix, c, pair<int, int>(p.first, p.second - 1));

}


if (p.second < cols-1)

{

// we need to check right cell

if (matrix[p.first][p.second + 1] == oldC)

ChangePxColor(matrix, c, pair<int, int>(p.first, p.second + 1));

}


// check the bottom row

if (p.first < rows-1)

{

// -1, 0, 1

for (i = -1; i < 2; ++i)

{

int x = p.second + i;

int y = p.first + 1;

// always check that were inside the matrix

if (x >= 0 && x < cols)

{

if (matrix[y][x] == oldC)

ChangePxColor(matrix, c, pair<int, int>(y, x));

}

}

} // else: theres no top row

}


void printMatrix(vector<vector<string>> matrix)

{

cout << "----------" << endl;


for (int i = 0; i < matrix.size(); ++i)

{

for (int j = 0; j < matrix[i].size(); ++j)

{

cout << matrix[i][j] << " ";

}


cout << endl;

}


cout << "----------" << endl;

}


//B B W

//W W W

//W W W

//B B B

vector<vector<string>> generateMatrix()

{

vector<vector<string>> pixelMatrix = vector<vector<string>>();

vector<string> first = vector<string>(),

second = vector<string>(),

third = vector<string>(),

fourth = vector<string>();


for(int i = 0; i < 2; ++i)

first.push_back("B");

first.push_back("W");

pixelMatrix.push_back(first); // Row 1

for (int i = 0; i < 3; ++i)

second.push_back("W");

pixelMatrix.push_back(second); // Row 2

for (int i = 0; i < 3; ++i)

third.push_back("W");

pixelMatrix.push_back(third); // Row 3

for (int i = 0; i < 3; ++i)

fourth.push_back("B");

pixelMatrix.push_back(fourth); // Row 4


return pixelMatrix;

}


int main()

{

vector<vector<string>> pixelMatrix = generateMatrix();

string newColor = "G";

pair<int, int> pixelPoint = pair<int, int>(2, 2);


printMatrix(pixelMatrix);


ChangePxColor(pixelMatrix, newColor, pixelPoint);


// expected outcome

/* B B G

G G G

G G G

B B B */

printMatrix(pixelMatrix);


return 0;

}






35 views0 comments

Recent Posts

See All

Comments


© 2020 Josh Painter

bottom of page