CC-15 : Replaces Os with Xs.
Description:
Given an adjacency matrix, filled with Os and Xs. For each O, Replace O by X, if there is no path of Os from current O to boundary of matrix. Os path can be in left, right, top, bottom direction only and not diagonal wise.
Test cases and expected outputs:
| Input Parameters | Expected outputs |
|---|---|
| Original Matrix : X,O,X,O, O,X,O,X, X,X,O,X, X,X,X,X, |
Image Matrix after Replace O by X : X,O,X,O, O,X,X,X, X,X,X,X, X,X,X,X, |
| Original Matrix : X,O,X,O,X, O,X,O,O,X, X,X,X,X,X, O,X,X,O,X, X,O,X,X,X, |
Image Matrix after Replace O by X : X,X,X,X,X, X,X,X,X,X, X,X,X,X,X, X,X,X,X,X, X,X,X,X,X, |
Pseudocode:
replaceOByX (int[][] matrix) method:
| Iterate through vertices of matrix using a for loop with iIdx as loop variable with values from 0 to count of matrix.length:
Iterate through vertices of matrix using a for loop with jIdx as loop variable with values from 0 to count of matrix[0].length:
Set the Os to ‘ ‘;to indicate these cells need to be processed.
|
| Iterate through vertices of matrix using a for loop with iIdx as loop variable with values from 0 to count of matrix.length:
Iterate through vertices of matrix using a for loop with jIdx as loop variable with values from 0 to count of matrix[0].length:
If first column or last column cells are ‘ ‘, call dfsReplaceOByX(matrix, iIdx, jIdx, ‘ ‘, ‘O’).
|
| Iterate through vertices of matrix using a for loop with iIdx as loop variable with values from 0 to count of matrix.length:
Iterate through vertices of matrix using a for loop with jIdx as loop variable with values from 0 to count of matrix[0].length:
If top row or bottom row cells are ‘ ‘, call dfsReplaceOByX(matrix, iIdx, jIdx, ‘ ‘, ‘O’).
|
| Return matrix. |
dfsReplaceOByX (int[][] matrix, int iIdx, int jIdx, int oldColor, int newColor) method:
| While i and j are within index limits of matric and matrix[iIdx][jIdx]==1 and matrix[iIdx][ jIdx]==oldColor:
Set matrix[iIdx][ jIdx]=newColor.
Recursively call floodFillImageMatrix (matrix, iIdx +1, jIdx, int oldColor, int newColor).
Recursively call floodFillImageMatrix (matrix, iIdx +1, jIdx, int oldColor, int newColor).
Recursively call floodFillImageMatrix (matrix, iIdx +1, jIdx, int oldColor, int newColor).
Recursively call floodFillImageMatrix (matrix, iIdx +1, jIdx, int oldColor, int newColor).
|
Code:
public void dfsReplaceOByX(char[][] matrix, int iIdx, int jIdx, char oldChar, char newChar) {
while ((iIdx >= 0 ) && (iIdx < matrix.length)
&& (jIdx >= 0 ) &&(jIdx < matrix[0].length)
&& (matrix[iIdx][jIdx] == oldChar)){
matrix[iIdx][jIdx]=newChar;
dfsReplaceOByX(matrix, iIdx+1, jIdx, oldChar, newChar);
dfsReplaceOByX(matrix, iIdx-1, jIdx, oldChar, newChar);
dfsReplaceOByX(matrix, iIdx, jIdx+1, oldChar, newChar);
dfsReplaceOByX(matrix, iIdx, jIdx-1, oldChar, newChar);
}
}
public char[][] replaceOByX(char[][] matrix) throws Exception{
for (int iIdx=0; iIdx < matrix.length; iIdx++) {
for (int jIdx=0; jIdx < matrix[0].length; jIdx++) {
if (matrix[iIdx][jIdx] =='O'){
matrix[iIdx][jIdx]=' ';
}
}
}
for (int iIdx=0; iIdx < matrix.length; iIdx++) {
if (matrix[iIdx][0] ==' '){
dfsReplaceOByX(matrix, iIdx, 0,' ', 'O');
}
if (matrix[iIdx][matrix[0].length-1] ==' '){
dfsReplaceOByX(matrix, iIdx, matrix[0].length-1,' ', 'O');
}
}
for (int jIdx=0; jIdx < matrix[0].length; jIdx++) {
if (matrix[0][jIdx] ==' '){
dfsReplaceOByX(matrix, 0, jIdx,' ', 'O');
}
if (matrix[matrix.length-1][jIdx] ==' '){
dfsReplaceOByX(matrix, matrix.length-1, jIdx,' ', 'O');
}
}
for (int iIdx=0; iIdx < matrix.length; iIdx++) {
for (int jIdx=0; jIdx < matrix[0].length; jIdx++) {
if (matrix[iIdx][jIdx] ==' '){
matrix[iIdx][jIdx]='X';
}
}
}
return matrix;
}
Click here to download and run code and test cases !
| About Us | Privacy Policy | Contact us |