Given a matrix where every element is either ‘O’ or ‘X’, replace ‘O’ with ‘X’ if surrounded by ‘X’. A ‘O’ (or a set of ‘O’) is considered to be surrounded by ‘X’ if there are ‘X’ at locations just below, just above, just left, and just right of it.
Examples:
Input: mat[M][N] = {{‘X’, ‘O’, ‘X’, ‘X’, ‘X’, ‘X’},
{‘X’, ‘O’, ‘X’, ‘X’, ‘O’, ‘X’},
{‘X’, ‘X’, ‘X’, ‘O’, ‘O’, ‘X’},
{‘O’, ‘X’, ‘X’, ‘X’, ‘X’, ‘X’},
{‘X’, ‘X’, ‘X’, ‘O’, ‘X’, ‘O’},
{‘O’, ‘O’, ‘X’, ‘O’, ‘O’, ‘O’}};
Output: mat[M][N] = {{‘X’, ‘O’, ‘X’, ‘X’, ‘X’, ‘X’},
{‘X’, ‘O’, ‘X’, ‘X’, ‘X’, ‘X’},
{‘X’, ‘X’, ‘X’, ‘X’, ‘X’, ‘X’},
{‘O’, ‘X’, ‘X’, ‘X’, ‘X’, ‘X’},
{‘X’, ‘X’, ‘X’, ‘O’, ‘X’, ‘O’},
{‘O’, ‘O’, ‘X’, ‘O’, ‘O’, ‘O’}};
Input: mat[M][N] = {{‘X’, ‘X’, ‘X’, ‘X’}
{‘X’, ‘O’, ‘X’, ‘X’}
{‘X’, ‘O’, ‘O’, ‘X’}
{‘X’, ‘O’, ‘X’, ‘X’}
{‘X’, ‘X’, ‘O’, ‘O’} };
Output: mat[M][N] = {{‘X’, ‘X’, ‘X’, ‘X’}
{‘X’, ‘X’, ‘X’, ‘X’}
{‘X’, ‘X’, ‘X’, ‘X’}
{‘X’, ‘X’, ‘X’, ‘X’}
{‘X’, ‘X’, ‘O’, ‘O’}};
This is mainly an application of Flood-Fill algorithm. The main difference here is that a ‘O’ is not replaced by ‘X’ if it lies in region that ends on a boundary. Following are simple steps to do this special flood fill.
Algorithm:
- Traverse the given matrix and replace all ‘O’ with a special character ‘-‘.
- Traverse four edges of given matrix and call floodFill(‘-‘, ‘O’) for every ‘-‘ on edges. The remaining ‘-‘ are the characters that indicate ‘O’s (in the original matrix) to be replaced by ‘X’.
- Traverse the matrix and replace all ‘-‘s with ‘X’s.
Illustration:
Let following be the input matrix.
mat[M][N] = {{'X', 'O', 'X', 'X', 'X', 'X'},
{'X', 'O', 'X', 'X', 'O', 'X'},
{'X', 'X', 'X', 'O', 'O', 'X'},
{'O', 'X', 'X', 'X', 'X', 'X'},
{'X', 'X', 'X', 'O', 'X', 'O'},
{'O', 'O', 'X', 'O', 'O', 'O'},
};
Step 1: Replace all ‘O’ with ‘-‘.
mat[M][N] = {{'X', '-', 'X', 'X', 'X', 'X'},
{'X', '-', 'X', 'X', '-', 'X'},
{'X', 'X', 'X', '-', '-', 'X'},
{'-', 'X', 'X', 'X', 'X', 'X'},
{'X', 'X', 'X', '-', 'X', '-'},
{'-', '-', 'X', '-', '-', '-'},
};
Step 2: Call floodFill(‘-‘, ‘O’) for all edge elements with value equals to ‘-‘
mat[M][N] = {{'X', 'O', 'X', 'X', 'X', 'X'},
{'X', 'O', 'X', 'X', '-', 'X'},
{'X', 'X', 'X', '-', '-', 'X'},
{'O', 'X', 'X', 'X', 'X', 'X'},
{'X', 'X', 'X', 'O', 'X', 'O'},
{'O', 'O', 'X', 'O', 'O', 'O'},
};
Step 3: Replace all ‘-‘ with ‘X’.
mat[M][N] = {{'X', 'O', 'X', 'X', 'X', 'X'},
{'X', 'O', 'X', 'X', 'X', 'X'},
{'X', 'X', 'X', 'X', 'X', 'X'},
{'O', 'X', 'X', 'X', 'X', 'X'},
{'X', 'X', 'X', 'O', 'X', 'O'},
{'O', 'O', 'X', 'O', 'O', 'O'},
};
The following is implementation of above algorithm.
C++
#include<iostream>
using namespace std;
#define M 6
#define N 6
void floodFillUtil(char mat[][N], int x, int y, char prevV, char newV)
{
if (x < 0 || x >= M || y < 0 || y >= N)
return;
if (mat[x][y] != prevV)
return;
mat[x][y] = newV;
floodFillUtil(mat, x+1, y, prevV, newV);
floodFillUtil(mat, x-1, y, prevV, newV);
floodFillUtil(mat, x, y+1, prevV, newV);
floodFillUtil(mat, x, y-1, prevV, newV);
}
int replaceSurrounded(char mat[][N])
{
for (int i=0; i<M; i++)
for (int j=0; j<N; j++)
if (mat[i][j] == 'O')
mat[i][j] = '-';
for (int i=0; i<M; i++)
if (mat[i][0] == '-')
floodFillUtil(mat, i, 0, '-', 'O');
for (int i=0; i<M; i++)
if (mat[i][N-1] == '-')
floodFillUtil(mat, i, N-1, '-', 'O');
for (int i=0; i<N; i++)
if (mat[0][i] == '-')
floodFillUtil(mat, 0, i, '-', 'O');
for (int i=0; i<N; i++)
if (mat[M-1][i] == '-')
floodFillUtil(mat, M-1, i, '-', 'O');
for (int i=0; i<M; i++)
for (int j=0; j<N; j++)
if (mat[i][j] == '-')
mat[i][j] = 'X';
}
int main()
{
char mat[][N] = {{'X', 'O', 'X', 'O', 'X', 'X'},
{'X', 'O', 'X', 'X', 'O', 'X'},
{'X', 'X', 'X', 'O', 'X', 'X'},
{'O', 'X', 'X', 'X', 'X', 'X'},
{'X', 'X', 'X', 'O', 'X', 'O'},
{'O', 'O', 'X', 'O', 'O', 'O'},
};
replaceSurrounded(mat);
for (int i=0; i<M; i++)
{
for (int j=0; j<N; j++)
cout << mat[i][j] << " ";
cout << endl;
}
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int M = 6;
static int N = 6;
static void floodFillUtil(char mat[][], int x,
int y, char prevV,
char newV)
{
if (x < 0 || x >= M ||
y < 0 || y >= N)
return;
if (mat[x][y] != prevV)
return;
mat[x][y] = newV;
floodFillUtil(mat, x + 1, y,
prevV, newV);
floodFillUtil(mat, x - 1, y,
prevV, newV);
floodFillUtil(mat, x, y + 1,
prevV, newV);
floodFillUtil(mat, x, y - 1,
prevV, newV);
}
static void replaceSurrounded(char mat[][])
{
for (int i = 0; i < M; i++)
for (int j = 0; j < N; j++)
if (mat[i][j] == 'O')
mat[i][j] = '-';
for (int i = 0; i < M; i++)
if (mat[i][0] == '-')
floodFillUtil(mat, i, 0,
'-', 'O');
for (int i = 0; i < M; i++)
if (mat[i][N - 1] == '-')
floodFillUtil(mat, i, N - 1,
'-', 'O');
for (int i = 0; i < N; i++)
if (mat[0][i] == '-')
floodFillUtil(mat, 0, i,
'-', 'O');
for (int i = 0; i < N; i++)
if (mat[M - 1][i] == '-')
floodFillUtil(mat, M - 1,
i, '-', 'O');
for (int i = 0; i < M; i++)
for (int j = 0; j < N; j++)
if (mat[i][j] == '-')
mat[i][j] = 'X';
}
public static void main (String[] args)
{
char[][] mat = {{'X', 'O', 'X',
'O', 'X', 'X'},
{'X', 'O', 'X',
'X', 'O', 'X'},
{'X', 'X', 'X',
'O', 'X', 'X'},
{'O', 'X', 'X',
'X', 'X', 'X'},
{'X', 'X', 'X',
'O', 'X', 'O'},
{'O', 'O', 'X',
'O', 'O', 'O'}};
replaceSurrounded(mat);
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
System.out.print(mat[i][j] + " ");
System.out.println("");
}
}
}
|
Python3
M = 6
N = 6
def floodFillUtil(mat, x, y, prevV, newV):
if (x < 0 or x >= M or y < 0 or y >= N):
return
if (mat[x][y] != prevV):
return
mat[x][y] = newV
floodFillUtil(mat, x + 1, y, prevV, newV)
floodFillUtil(mat, x - 1, y, prevV, newV)
floodFillUtil(mat, x, y + 1, prevV, newV)
floodFillUtil(mat, x, y - 1, prevV, newV)
def replaceSurrounded(mat):
for i in range(M):
for j in range(N):
if (mat[i][j] == 'O'):
mat[i][j] = '-'
for i in range(M):
if (mat[i][0] == '-'):
floodFillUtil(mat, i, 0, '-', 'O')
for i in range(M):
if (mat[i][N - 1] == '-'):
floodFillUtil(mat, i, N - 1, '-', 'O')
for i in range(N):
if (mat[0][i] == '-'):
floodFillUtil(mat, 0, i, '-', 'O')
for i in range(N):
if (mat[M - 1][i] == '-'):
floodFillUtil(mat, M - 1, i, '-', 'O')
for i in range(M):
for j in range(N):
if (mat[i][j] == '-'):
mat[i][j] = 'X'
if __name__ == '__main__':
mat = [ [ 'X', 'O', 'X', 'O', 'X', 'X' ],
[ 'X', 'O', 'X', 'X', 'O', 'X' ],
[ 'X', 'X', 'X', 'O', 'X', 'X' ],
[ 'O', 'X', 'X', 'X', 'X', 'X' ],
[ 'X', 'X', 'X', 'O', 'X', 'O' ],
[ 'O', 'O', 'X', 'O', 'O', 'O' ] ];
replaceSurrounded(mat)
for i in range(M):
print(*mat[i])
|
C#
using System;
class GFG
{
static int M = 6;
static int N = 6;
static void floodFillUtil(char [,]mat, int x,
int y, char prevV,
char newV)
{
if (x < 0 || x >= M ||
y < 0 || y >= N)
return;
if (mat[x, y] != prevV)
return;
mat[x, y] = newV;
floodFillUtil(mat, x + 1, y,
prevV, newV);
floodFillUtil(mat, x - 1, y,
prevV, newV);
floodFillUtil(mat, x, y + 1,
prevV, newV);
floodFillUtil(mat, x, y - 1,
prevV, newV);
}
static void replaceSurrounded(char [,]mat)
{
for (int i = 0; i < M; i++)
for (int j = 0; j < N; j++)
if (mat[i, j] == 'O')
mat[i, j] = '-';
for (int i = 0; i < M; i++)
if (mat[i, 0] == '-')
floodFillUtil(mat, i, 0,
'-', 'O');
for (int i = 0; i < M; i++)
if (mat[i, N - 1] == '-')
floodFillUtil(mat, i, N - 1,
'-', 'O');
for (int i = 0; i < N; i++)
if (mat[0, i] == '-')
floodFillUtil(mat, 0, i,
'-', 'O');
for (int i = 0; i < N; i++)
if (mat[M - 1, i] == '-')
floodFillUtil(mat, M - 1,
i, '-', 'O');
for (int i = 0; i < M; i++)
for (int j = 0; j < N; j++)
if (mat[i, j] == '-')
mat[i, j] = 'X';
}
public static void Main ()
{
char [,]mat = new char[,]
{{'X', 'O', 'X',
'O', 'X', 'X'},
{'X', 'O', 'X',
'X', 'O', 'X'},
{'X', 'X', 'X',
'O', 'X', 'X'},
{'O', 'X', 'X',
'X', 'X', 'X'},
{'X', 'X', 'X',
'O', 'X', 'O'},
{'O', 'O', 'X',
'O', 'O', 'O'}};
replaceSurrounded(mat);
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
Console.Write(mat[i, j] + " ");
Console.WriteLine("");
}
}
}
|
Javascript
<script>
let M = 6;
let N = 6;
function floodFillUtil(mat,x,y,prevV,newV)
{
if (x < 0 || x >= M ||
y < 0 || y >= N)
return;
if (mat[x][y] != prevV)
return;
mat[x][y] = newV;
floodFillUtil(mat, x + 1, y,
prevV, newV);
floodFillUtil(mat, x - 1, y,
prevV, newV);
floodFillUtil(mat, x, y + 1,
prevV, newV);
floodFillUtil(mat, x, y - 1,
prevV, newV);
}
function replaceSurrounded(mat)
{
for (let i = 0; i < M; i++)
for (let j = 0; j < N; j++)
if (mat[i][j] == 'O')
mat[i][j] = '-';
// Call floodFill for
// all '-' lying on edges
for (let i = 0; i < M; i++) // Left side
if (mat[i][0] == '-')
floodFillUtil(mat, i, 0,
'-', 'O');
for (let i = 0; i < M; i++) // Right side
if (mat[i][N - 1] == '-')
floodFillUtil(mat, i, N - 1,
'-', 'O');
for (let i = 0; i < N; i++) // Top side
if (mat[0][i] == '-')
floodFillUtil(mat, 0, i,
'-', 'O');
for (let i = 0; i < N; i++) // Bottom side
if (mat[M - 1][i] == '-')
floodFillUtil(mat, M - 1,
i, '-', 'O');
// Step 3: Replace
// all '-' with 'X'
for (let i = 0; i < M; i++)
for (let j = 0; j < N; j++)
if (mat[i][j] == '-')
mat[i][j] = 'X';
}
// Driver Code
let mat = [ [ 'X', 'O', 'X', 'O', 'X', 'X' ],
[ 'X', 'O', 'X', 'X', 'O', 'X' ],
[ 'X', 'X', 'X', 'O', 'X', 'X' ],
[ 'O', 'X', 'X', 'X', 'X', 'X' ],
[ 'X', 'X', 'X', 'O', 'X', 'O' ],
[ 'O', 'O', 'X', 'O', 'O', 'O' ] ];
replaceSurrounded(mat);
for (let i = 0; i < M; i++)
{
for (let j = 0; j < N; j++)
document.write(mat[i][j] + " ");
document.write("<br>");
}
</script>
|
PHP
<?php
$M = 6;
$N = 6;
function floodFillUtil(&$mat, $x, $y,
$prevV, $newV)
{
if ($x < 0 || $x >= $GLOBALS['M'] ||
$y < 0 || $y >= $GLOBALS['N'])
return;
if ($mat[$x][$y] != $prevV)
return;
$mat[$x][$y] = $newV;
floodFillUtil($mat, $x + 1, $y, $prevV, $newV);
floodFillUtil($mat, $x - 1, $y, $prevV, $newV);
floodFillUtil($mat, $x, $y + 1, $prevV, $newV);
floodFillUtil($mat, $x, $y - 1, $prevV, $newV);
}
function replaceSurrounded(&$mat)
{
for ($i = 0; $i < $GLOBALS['M']; $i++)
for ($j = 0; $j < $GLOBALS['N']; $j++)
if ($mat[$i][$j] == 'O')
$mat[$i][$j] = '-';
for ($i = 0;
$i < $GLOBALS['M']; $i++)
if ($mat[$i][0] == '-')
floodFillUtil($mat, $i, 0, '-', 'O');
for ($i = 0; $i < $GLOBALS['M']; $i++)
if ($mat[$i][$GLOBALS['N'] - 1] == '-')
floodFillUtil($mat, $i,
$GLOBALS['N'] - 1, '-', 'O');
for ($i = 0; $i < $GLOBALS['N']; $i++)
if ($mat[0][$i] == '-')
floodFillUtil($mat, 0, $i, '-', 'O');
for ($i = 0; $i < $GLOBALS['N']; $i++)
if ($mat[$GLOBALS['M'] - 1][$i] == '-')
floodFillUtil($mat, $GLOBALS['M'] - 1,
$i, '-', 'O');
for ($i = 0; $i < $GLOBALS['M']; $i++)
for ($j = 0; $j < $GLOBALS['N']; $j++)
if ($mat[$i][$j] == '-')
$mat[$i][$j] = 'X';
}
$mat = array(array('X', 'O', 'X', 'O', 'X', 'X'),
array('X', 'O', 'X', 'X', 'O', 'X'),
array('X', 'X', 'X', 'O', 'X', 'X'),
array('O', 'X', 'X', 'X', 'X', 'X'),
array('X', 'X', 'X', 'O', 'X', 'O'),
array('O', 'O', 'X', 'O', 'O', 'O'));
replaceSurrounded($mat);
for ($i = 0; $i < $GLOBALS['M']; $i++)
{
for ($j = 0; $j < $GLOBALS['N']; $j++)
echo $mat[$i][$j]." ";
echo "\n";
}
?>
|
Output
X O X O X X
X O X X X X
X X X X X X
O X X X X X
X X X O X O
O O X O O O
Time Complexity: O(MN). Note that every element of matrix is processed at most three times.
Auxiliary Space: O(M x N), as implicit stack is used due to recursive call
Similar Reads
Given a matrix of 'O' and 'X', find the largest subsquare surrounded by 'X'
Given a matrix mat[][] of size n x n where every element is either 'O' or 'X', the task is to find the size of the largest square subgrid that is completely surrounded by 'X', i.e. the largest square where all its border cells are 'X'. Examples: Input: mat[][] = [ ['X', 'X'], ['X', 'X'] ]Output: 2Ex
15 min read
Check if a Matrix can be superimposed on the given Matrix
Given a matrix letter[][] of size N * M, composed of '#' and '*' and another matrix stamp[][] of size X * Y containing only '$'. The task is to find if all the '*' of the larger one can be replaced by '$' by superimposing the stamp matrix on the letter matrix. Note: In a superimpose operation only a
10 min read
Create a matrix with alternating rectangles of O and X
Write a code which inputs two numbers m and n and creates a matrix of size m x n (m rows and n columns) in which every elements is either X or 0. The Xs and 0s must be filled alternatively, the matrix should have outermost rectangle of Xs, then a rectangle of 0s, then a rectangle of Xs, and so on. E
15 min read
Search in a row wise and column wise sorted matrix
Given a matrix mat[][] and an integer x, the task is to check if x is present in mat[][] or not. Every row and column of the matrix is sorted in increasing order. Examples: Input: x = 62, mat[][] = [[3, 30, 38], [20, 52, 54], [35, 60, 69]]Output: falseExplanation: 62 is not present in the matrix. In
14 min read
Find indices in a sorted Matrix where a new number can be replaced
Given a matrix arr[][] which is sorted by the increasing number of elements and a number X, the task is to find the position where the input integer can be replaced with an existing element without disturbing the order of the sorted elements. The matrix is sorted in such a manner that: Every row is
11 min read
Find all occurrences of a given word in a matrix
Given a 2D grid of characters and a word, find all occurrences of given word in grid. A word can be matched in all 8 directions at any point. Word is said be found in a direction if all characters match in this direction (not in zig-zag form). The solution should print all coordinates if a cycle is
12 min read
Queries to replace every array element by its XOR with a given value with updates
Given an array, initially consisting of 0 as the only element present and Q operations of the following two types: Add(X): Insert X into the array.Update(X): Replace every array element Ai by Ai ^ X, where ^ is the XOR operation. Examples: Input: Q = 2Add(5)Update(2)Output: 2 7Explanation:Initially,
5 min read
Replace every element of the array with BitWise XOR of all other
Given an array of integers. The task is to replace every element by the bitwise xor of all other elements of the array. Examples: Input: arr[] = { 2, 3, 3, 5, 5 } Output: 0 1 1 7 7 Bitwise Xor of 3, 3, 5, 5 = 0 Bitwise Xor of 2, 3, 5, 5 = 1 Bitwise Xor of 2, 3, 5, 5 = 1 Bitwise Xor of 2, 3, 3, 5 = 7
5 min read
Check if all rows of a Binary Matrix have all ones placed adjacently or not
Given a binary matrix mat[][] of dimension N*M, the task is to check if all 1s in each row are placed adjacently on the given matrix. If all 1s in each row are adjacent, then print "Yes". Otherwise, print "No". Examples: Input: mat[][] = {{0, 1, 1, 0}, {1, 1, 0, 0}, {0, 0, 0, 1}, {1, 1, 1, 0}Output:
9 min read
Convert string X to an anagram of string Y with minimum replacements
Given two strings X and Y, we need to convert string X into an anagram of string Y with minimum replacements. If we have multiple ways of achieving the target, we go for the lexicographically smaller string where the length of each string [Tex]\in [1, 100000] [/Tex] Examples: Input : X = "CDBABC" Y
13 min read