# Surrounded Regions

Here given code implementation process.

``````/*
C Program for
Surrounded Regions
*/
#include <stdio.h>
#define R 8
#define C 7

// Print record elements
void printResult(char record[R][C])
{
for (int i = 0; i < R; ++i)
{
for (int j = 0; j < C; ++j)
{

printf(" %c",record[i][j]);

}
printf("\n");
}
}
// Fill corner elements with a "O" value
void fill(char record[R][C],int i, int j)
{
if(i < 0 || i >= R || j < 0 || j >= C)
{
return;
}

if(record[i][j] == '+')
{
// Update element
record[i][j] = 'O';

// visit to down element
fill(record,i+1,j);
// visit to up element
fill(record,i-1,j);

// visit to right element
fill(record,i,j+1);
// visit to left element
fill(record,i,j-1);
}
}

// Fill surrounded regions
void solve(char record[R][C])
{
// loop controlling variables
int i = 0;
int j = 0;
printf("\n Before Surrounded \n");
printResult(record);
// First change the all zero with other symbol  "+"
for ( i = 0; i < R; ++i)
{
for ( j = 0; j < C; ++j)
{
if(record[i][j]=='O')
{
record[i][j] = '+';
}
}
}
// Change boundary
// Check first and last column
for ( i = 0; i < R; ++i)
{
if(record[i][0] == '+')
{
fill(record,i,0);
}

if(record[i][C-1]=='+')
{
fill(record,i,C-1);
}
}

// Check first and last row
for ( i = 0; i < C; ++i)
{
if(record[0][i] == '+')
{
fill(record,0,i);
}

if(record[R-1][i]=='+')
{
fill(record,R-1,i);
}
}
for ( i = 0; i < R; ++i)
{
for ( j = 0; j < C; ++j)
{
if(record[i][j]=='+')
{
record[i][j] = 'X';
}
}
}
printf("\n After Surrounded \n");
printResult(record);
}

int main()
{
char record[R][C] =
{
{'O', 'X', 'X', 'X', 'X', 'O', 'X'},
{'X', 'X', 'X', 'X', 'O', 'X', 'O'},
{'O', 'O', 'O', 'X', 'X', 'O', 'O'},
{'X', 'O', 'X', 'X', 'X', 'X', 'X'},
{'X', 'X', 'O', 'X', 'X', 'X', 'X'},
{'X', 'O', 'X', 'X', 'X', 'X', 'X'},
{'X', 'X', 'O', 'O', 'X', 'O', 'O'},
{'X', 'X', 'O', 'X', 'O', 'X', 'O'},
};
/*
Change all O with X when O is Surrounded by X
Example
========
// Left,Right, Bottom and Top are have X
X   X
X   O   O  X
X   X

Then change center O with X

X   X
X   X   X  X
X   X
-----------------------------
or
X
X   O  X
X
Then change center O with X
X
X   O  X
X
*/
solve(record);

return 0;
}``````

#### Output

`````` Before Surrounded
O X X X X O X
X X X X O X O
O O O X X O O
X O X X X X X
X X O X X X X
X O X X X X X
X X O O X O O
X X O X O X O

After Surrounded
O X X X X O X
X X X X X X O
O O O X X O O
X O X X X X X
X X X X X X X
X X X X X X X
X X O O X O O
X X O X O X O``````
``````/*
Java Program for
surrounded regions
*/
public class Regions
{
// Print record elements
public void printresult(char[][] record, int r, int c)
{
for (int i = 0; i < r; ++i)
{
for (int j = 0; j < c; ++j)
{
System.out.print("  " + record[i][j]);
}
System.out.print("\n");
}
}
// Fill corner elements with a "O" value
public void fill(char[][] record, int i, int j, int r, int c)
{
if (i < 0 || i >= r || j < 0 || j >= c)
{
return;
}
if (record[i][j] == '+')
{
// Update element
record[i][j] = 'O';
// visit to down element
fill(record, i + 1, j, r, c);
// visit to up element
fill(record, i - 1, j, r, c);
// visit to right element
fill(record, i, j + 1, r, c);
// visit to left element
fill(record, i, j - 1, r, c);
}
}
// Fill surrounded regions
public void solve(char[][] record)
{
// loop controlling variables
int i = 0;
int j = 0;
int r = record.length;
int c = record[0].length;
System.out.print("\n Before Surrounded \n");
printresult(record, r, c);
// First change the all zero with other symbol  "+"
for (i = 0; i < r; ++i)
{
for (j = 0; j < c; ++j)
{
if (record[i][j] == 'O')
{
record[i][j] = '+';
}
}
}
// change boundary
// check first and last column
for (i = 0; i < r; ++i)
{
if (record[i][0] == '+')
{
fill(record, i, 0, r, c);
}
if (record[i][c - 1] == '+')
{
fill(record, i, c - 1, r, c);
}
}
// check first and last row
for (i = 0; i < c; ++i)
{
if (record[0][i] == '+')
{
fill(record, 0, i, r, c);
}
if (record[r - 1][i] == '+')
{
fill(record, r - 1, i, r, c);
}
}
for (i = 0; i < r; ++i)
{
for (j = 0; j < c; ++j)
{
if (record[i][j] == '+')
{
record[i][j] = 'X';
}
}
}
System.out.print("\n After Surrounded \n");
printresult(record, r, c);
}
public static void main(String[] args)
{
char [][]record =
{
{'O', 'X', 'X', 'X', 'X', 'O', 'X'},
{'X', 'X', 'X', 'X', 'O', 'X', 'O'},
{'O', 'O', 'O', 'X', 'X', 'O', 'O'},
{'X', 'O', 'X', 'X', 'X', 'X', 'X'},
{'X', 'X', 'O', 'X', 'X', 'X', 'X'},
{'X', 'O', 'X', 'X', 'X', 'X', 'X'},
{'X', 'X', 'O', 'O', 'X', 'O', 'O'},
{'X', 'X', 'O', 'X', 'O', 'X', 'O'},
};
/*
Change all O with X when O is Surrounded by X
Example
========
// Left,right, Bottom and Top are have X
X   X
X   O   O  X
X   X

Then change center O with X

X   X
X   X   X  X
X   X
-----------------------------
or
X
X   O  X
X
Then change center O with X
X
X   O  X
X
*/
}
}``````

#### Output

`````` Before Surrounded
O  X  X  X  X  O  X
X  X  X  X  O  X  O
O  O  O  X  X  O  O
X  O  X  X  X  X  X
X  X  O  X  X  X  X
X  O  X  X  X  X  X
X  X  O  O  X  O  O
X  X  O  X  O  X  O

After Surrounded
O  X  X  X  X  O  X
X  X  X  X  X  X  O
O  O  O  X  X  O  O
X  O  X  X  X  X  X
X  X  X  X  X  X  X
X  X  X  X  X  X  X
X  X  O  O  X  O  O
X  X  O  X  O  X  O``````
``````// Include header file
#include <iostream>
#define R 8
#define C 7
using namespace std;
/*
C++ Program for
surrounded regions
*/
class Regions
{
public:
// Print record elements
void printresult(char record[R][C])
{
for (int i = 0; i < R; ++i)
{
for (int j = 0; j < C; ++j)
{
cout << "  " << record[i][j];
}
cout << "\n";
}
}
// Fill corner elements with a "O" value
void fill(char record[R][C], int i, int j)
{
if (i < 0 || i >= R || j < 0 || j >= C)
{
return;
}
if (record[i][j] == '+')
{
// Update element
record[i][j] = 'O';
// visit to down element
this->fill(record, i + 1, j);
// visit to up element
this->fill(record, i - 1, j);
// visit to right element
this->fill(record, i, j + 1);
// visit to left element
this->fill(record, i, j - 1);
}
}
// Fill surrounded regions
void solve(char record[R][C])
{
// loop controlling variables
int i = 0;
int j = 0;

cout << "\n  Before Surrounded \n";
this->printresult(record);
// First change the all zero with other symbol  "+"
for (i = 0; i < R; ++i)
{
for (j = 0; j < C; ++j)
{
if (record[i][j] == 'O')
{
record[i][j] = '+';
}
}
}
// change boundary
// check first and last column
for (i = 0; i < R; ++i)
{
if (record[i][0] == '+')
{
this->fill(record, i, 0);
}
if (record[i][C - 1] == '+')
{
this->fill(record, i, C - 1);
}
}
// check first and last row
for (i = 0; i < C; ++i)
{
if (record[0][i] == '+')
{
this->fill(record, 0, i);
}
if (record[R - 1][i] == '+')
{
this->fill(record, R - 1, i);
}
}
for (i = 0; i < R; ++i)
{
for (j = 0; j < C; ++j)
{
if (record[i][j] == '+')
{
record[i][j] = 'X';
}
}
}
cout << "\n  After Surrounded \n";
this->printresult(record);
}
};
int main()
{
char record[R][C] =
{
{'O', 'X', 'X', 'X', 'X', 'O', 'X'},
{'X', 'X', 'X', 'X', 'O', 'X', 'O'},
{'O', 'O', 'O', 'X', 'X', 'O', 'O'},
{'X', 'O', 'X', 'X', 'X', 'X', 'X'},
{'X', 'X', 'O', 'X', 'X', 'X', 'X'},
{'X', 'O', 'X', 'X', 'X', 'X', 'X'},
{'X', 'X', 'O', 'O', 'X', 'O', 'O'},
{'X', 'X', 'O', 'X', 'O', 'X', 'O'},
};
/*
Change all O with X when O is Surrounded by X
Example
========
// Left,right, Bottom and Top are have X
X   X
X   O   O  X
X   X

Then change center O with X
X   X
X   X   X  X
X   X
-----------------------------
or
X
X   O  X
X
Then change center O with X
X
X   O  X
X
*/
return 0;
}``````

#### Output

``````  Before Surrounded
O  X  X  X  X  O  X
X  X  X  X  O  X  O
O  O  O  X  X  O  O
X  O  X  X  X  X  X
X  X  O  X  X  X  X
X  O  X  X  X  X  X
X  X  O  O  X  O  O
X  X  O  X  O  X  O

After Surrounded
O  X  X  X  X  O  X
X  X  X  X  X  X  O
O  O  O  X  X  O  O
X  O  X  X  X  X  X
X  X  X  X  X  X  X
X  X  X  X  X  X  X
X  X  O  O  X  O  O
X  X  O  X  O  X  O``````
``````// Include namespace system
using System;
/*
C# Program for
surrounded regions
*/
public class Regions
{
// Print record elements
public void printresult(char[,] record, int r, int c)
{
for (int i = 0; i < r; ++i)
{
for (int j = 0; j < c; ++j)
{
Console.Write("  " + record[i,j]);
}
Console.Write("\n");
}
}
// Fill corner elements with a "O" value
public void fill(char[,] record, int i, int j, int r, int c)
{
if (i < 0 || i >= r || j < 0 || j >= c)
{
return;
}
if (record[i,j] == '+')
{
// Update element
record[i,j] = 'O';
// visit to down element
fill(record, i + 1, j, r, c);
// visit to up element
fill(record, i - 1, j, r, c);
// visit to right element
fill(record, i, j + 1, r, c);
// visit to left element
fill(record, i, j - 1, r, c);
}
}
// Fill surrounded regions
public void solve(char[,] record)
{
// loop controlling variables
int i = 0;
int j = 0;
int r = record.GetLength(0);
int c = record.GetLength(1);
Console.Write("\n  Before Surrounded \n");
printresult(record, r, c);
// First change the all zero with other symbol  "+"
for (i = 0; i < r; ++i)
{
for (j = 0; j < c; ++j)
{
if (record[i,j] == 'O')
{
record[i,j] = '+';
}
}
}
// change boundary
// check first and last column
for (i = 0; i < r; ++i)
{
if (record[i,0] == '+')
{
fill(record, i, 0, r, c);
}
if (record[i,c - 1] == '+')
{
fill(record, i, c - 1, r, c);
}
}
// check first and last row
for (i = 0; i < c; ++i)
{
if (record[0,i] == '+')
{
fill(record, 0, i, r, c);
}
if (record[r - 1,i] == '+')
{
fill(record, r - 1, i, r, c);
}
}
for (i = 0; i < r; ++i)
{
for (j = 0; j < c; ++j)
{
if (record[i,j] == '+')
{
record[i,j] = 'X';
}
}
}
Console.Write("\n  After Surrounded \n");
printresult(record, r, c);
}
public static void Main(String[] args)
{
char[,] record =
{
{'O', 'X', 'X', 'X', 'X', 'O', 'X'},
{'X', 'X', 'X', 'X', 'O', 'X', 'O'},
{'O', 'O', 'O', 'X', 'X', 'O', 'O'},
{'X', 'O', 'X', 'X', 'X', 'X', 'X'},
{'X', 'X', 'O', 'X', 'X', 'X', 'X'},
{'X', 'O', 'X', 'X', 'X', 'X', 'X'},
{'X', 'X', 'O', 'O', 'X', 'O', 'O'},
{'X', 'X', 'O', 'X', 'O', 'X', 'O'}
};
/*
Change all O with X when O is Surrounded by X
Example
========
// Left,right, Bottom and Top are have X
X   X
X   O   O  X
X   X

Then change center O with X
X   X
X   X   X  X
X   X
-----------------------------
or
X
X   O  X
X
Then change center O with X
X
X   O  X
X
*/
}
}``````

#### Output

``````  Before Surrounded
O  X  X  X  X  O  X
X  X  X  X  O  X  O
O  O  O  X  X  O  O
X  O  X  X  X  X  X
X  X  O  X  X  X  X
X  O  X  X  X  X  X
X  X  O  O  X  O  O
X  X  O  X  O  X  O

After Surrounded
O  X  X  X  X  O  X
X  X  X  X  X  X  O
O  O  O  X  X  O  O
X  O  X  X  X  X  X
X  X  X  X  X  X  X
X  X  X  X  X  X  X
X  X  O  O  X  O  O
X  X  O  X  O  X  O``````
``````<?php
/*
Php Program for
surrounded regions
*/
class Regions
{
// Print record elements
public  function printresult( & \$record, \$r, \$c)
{
for (\$i = 0; \$i < \$r; ++\$i)
{
for (\$j = 0; \$j < \$c; ++\$j)
{
echo "  ". \$record[\$i][\$j];
}
echo "\n";
}
}
// Fill corner elements with a "O" value
public  function fill( & \$record, \$i, \$j, \$r, \$c)
{
if (\$i < 0 || \$i >= \$r || \$j < 0 || \$j >= \$c)
{
return;
}
if (\$record[\$i][\$j] == '+')
{
// Update element
\$record[\$i][\$j] = 'O';
// visit to down element
\$this->fill(\$record, \$i + 1, \$j, \$r, \$c);
// visit to up element
\$this->fill(\$record, \$i - 1, \$j, \$r, \$c);
// visit to right element
\$this->fill(\$record, \$i, \$j + 1, \$r, \$c);
// visit to left element
\$this->fill(\$record, \$i, \$j - 1, \$r, \$c);
}
}
// Fill surrounded regions
public  function solve( & \$record)
{
// loop controlling variables
\$i = 0;
\$j = 0;
\$r = count(\$record);
\$c = count(\$record[0]);
echo "\n  Before Surrounded \n";
\$this->printresult(\$record, \$r, \$c);
// First change the all zero with other symbol  "+"
for (\$i = 0; \$i < \$r; ++\$i)
{
for (\$j = 0; \$j < \$c; ++\$j)
{
if (\$record[\$i][\$j] == 'O')
{
\$record[\$i][\$j] = '+';
}
}
}
// change boundary
// check first and last column
for (\$i = 0; \$i < \$r; ++\$i)
{
if (\$record[\$i][0] == '+')
{
\$this->fill(\$record, \$i, 0, \$r, \$c);
}
if (\$record[\$i][\$c - 1] == '+')
{
\$this->fill(\$record, \$i, \$c - 1, \$r, \$c);
}
}
// check first and last row
for (\$i = 0; \$i < \$c; ++\$i)
{
if (\$record[0][\$i] == '+')
{
\$this->fill(\$record, 0, \$i, \$r, \$c);
}
if (\$record[\$r - 1][\$i] == '+')
{
\$this->fill(\$record, \$r - 1, \$i, \$r, \$c);
}
}
for (\$i = 0; \$i < \$r; ++\$i)
{
for (\$j = 0; \$j < \$c; ++\$j)
{
if (\$record[\$i][\$j] == '+')
{
\$record[\$i][\$j] = 'X';
}
}
}
echo "\n  After Surrounded \n";
\$this->printresult(\$record, \$r, \$c);
}
}

function main()
{
\$record = array(
array('O', 'X', 'X', 'X', 'X', 'O', 'X'),
array('X', 'X', 'X', 'X', 'O', 'X', 'O'),
array('O', 'O', 'O', 'X', 'X', 'O', 'O'),
array('X', 'O', 'X', 'X', 'X', 'X', 'X'),
array('X', 'X', 'O', 'X', 'X', 'X', 'X'),
array('X', 'O', 'X', 'X', 'X', 'X', 'X'),
array('X', 'X', 'O', 'O', 'X', 'O', 'O'),
array('X', 'X', 'O', 'X', 'O', 'X', 'O')
);
/*
Change all O with X when O is Surrounded by X
Example
========
// Left,right, Bottom and Top are have X
X   X
X   O   O  X
X   X

Then change center O with X
X   X
X   X   X  X
X   X
-----------------------------
or
X
X   O  X
X
Then change center O with X
X
X   O  X
X
*/
}
main();``````

#### Output

``````  Before Surrounded
O  X  X  X  X  O  X
X  X  X  X  O  X  O
O  O  O  X  X  O  O
X  O  X  X  X  X  X
X  X  O  X  X  X  X
X  O  X  X  X  X  X
X  X  O  O  X  O  O
X  X  O  X  O  X  O

After Surrounded
O  X  X  X  X  O  X
X  X  X  X  X  X  O
O  O  O  X  X  O  O
X  O  X  X  X  X  X
X  X  X  X  X  X  X
X  X  X  X  X  X  X
X  X  O  O  X  O  O
X  X  O  X  O  X  O``````
``````/*
Node Js Program for
surrounded regions
*/
class Regions
{
// Print record elements
printresult(record, r, c)
{
for (var i = 0; i < r; ++i)
{
for (var j = 0; j < c; ++j)
{
process.stdout.write("  " + record[i][j]);
}
process.stdout.write("\n");
}
}
// Fill corner elements with a "O" value
fill(record, i, j, r, c)
{
if (i < 0 || i >= r || j < 0 || j >= c)
{
return;
}
if (record[i][j] == '+')
{
// Update element
record[i][j] = 'O';
// visit to down element
this.fill(record, i + 1, j, r, c);
// visit to up element
this.fill(record, i - 1, j, r, c);
// visit to right element
this.fill(record, i, j + 1, r, c);
// visit to left element
this.fill(record, i, j - 1, r, c);
}
}
// Fill surrounded regions
solve(record)
{
// loop controlling variables
var i = 0;
var j = 0;
var r = record.length;
var c = record[0].length;
process.stdout.write("\n  Before Surrounded \n");
this.printresult(record, r, c);
// First change the all zero with other symbol  "+"
for (i = 0; i < r; ++i)
{
for (j = 0; j < c; ++j)
{
if (record[i][j] == 'O')
{
record[i][j] = '+';
}
}
}
// change boundary
// check first and last column
for (i = 0; i < r; ++i)
{
if (record[i][0] == '+')
{
this.fill(record, i, 0, r, c);
}
if (record[i][c - 1] == '+')
{
this.fill(record, i, c - 1, r, c);
}
}
// check first and last row
for (i = 0; i < c; ++i)
{
if (record[0][i] == '+')
{
this.fill(record, 0, i, r, c);
}
if (record[r - 1][i] == '+')
{
this.fill(record, r - 1, i, r, c);
}
}
for (i = 0; i < r; ++i)
{
for (j = 0; j < c; ++j)
{
if (record[i][j] == '+')
{
record[i][j] = 'X';
}
}
}
process.stdout.write("\n  After Surrounded \n");
this.printresult(record, r, c);
}
}

function main()
{
var record = [
['O', 'X', 'X', 'X', 'X', 'O', 'X'] ,
['X', 'X', 'X', 'X', 'O', 'X', 'O'] ,
['O', 'O', 'O', 'X', 'X', 'O', 'O'] ,
['X', 'O', 'X', 'X', 'X', 'X', 'X'] ,
['X', 'X', 'O', 'X', 'X', 'X', 'X'] ,
['X', 'O', 'X', 'X', 'X', 'X', 'X'] ,
['X', 'X', 'O', 'O', 'X', 'O', 'O'] ,
['X', 'X', 'O', 'X', 'O', 'X', 'O']
];
/*
Change all O with X when O is Surrounded by X
Example
========
// Left,right, Bottom and Top are have X
X   X
X   O   O  X
X   X

Then change center O with X
X   X
X   X   X  X
X   X
-----------------------------
or
X
X   O  X
X
Then change center O with X
X
X   O  X
X
*/
}
main();``````

#### Output

``````  Before Surrounded
O  X  X  X  X  O  X
X  X  X  X  O  X  O
O  O  O  X  X  O  O
X  O  X  X  X  X  X
X  X  O  X  X  X  X
X  O  X  X  X  X  X
X  X  O  O  X  O  O
X  X  O  X  O  X  O

After Surrounded
O  X  X  X  X  O  X
X  X  X  X  X  X  O
O  O  O  X  X  O  O
X  O  X  X  X  X  X
X  X  X  X  X  X  X
X  X  X  X  X  X  X
X  X  O  O  X  O  O
X  X  O  X  O  X  O``````
``````#    Python 3 Program for
#    surrounded regions

class Regions :
#  Print record elements
def printresult(self, record, r, c) :
i = 0
j = 0
while (i < r) :
while (j < c) :
print("  ", record[i][j], end = "")
j += 1

print(end = "\n")
i += 1
j = 0

#  Fill corner elements with a "O" value
def fill(self, record, i, j, r, c) :
if (i < 0 or i >= r or j < 0 or j >= c) :
return

if (record[i][j] == '+') :
#  Update element
record[i][j] = 'O'
#  visit to down element
self.fill(record, i + 1, j, r, c)
#  visit to up element
self.fill(record, i - 1, j, r, c)
#  visit to right element
self.fill(record, i, j + 1, r, c)
#  visit to left element
self.fill(record, i, j - 1, r, c)

#  Fill surrounded regions
def solve(self, record) :
#  loop controlling variables
i = 0
j = 0
r = len(record)
c = len(record[0])
print("\n  Before Surrounded ")
self.printresult(record, r, c)
#  First change the all zero with other symbol  "+"
while (i < r) :
while (j < c) :
if (record[i][j] == 'O') :
record[i][j] = '+'

j += 1

i += 1
j = 0

#  change boundary
#  check first and last column
i = 0
while (i < r) :
if (record[i][0] == '+') :
self.fill(record, i, 0, r, c)

if (record[i][c - 1] == '+') :
self.fill(record, i, c - 1, r, c)

i += 1

#  check first and last row
i = 0
while (i < c) :
if (record[0][i] == '+') :
self.fill(record, 0, i, r, c)

if (record[r - 1][i] == '+') :
self.fill(record, r - 1, i, r, c)

i += 1

i = 0
while (i < r) :
j = 0
while (j < c) :
if (record[i][j] == '+') :
record[i][j] = 'X'

j += 1

i += 1

print("\n  After Surrounded ")
self.printresult(record, r, c)

def main() :
record = [
['O', 'X', 'X', 'X', 'X', 'O', 'X'] ,
['X', 'X', 'X', 'X', 'O', 'X', 'O'] ,
['O', 'O', 'O', 'X', 'X', 'O', 'O'] ,
['X', 'O', 'X', 'X', 'X', 'X', 'X'] ,
['X', 'X', 'O', 'X', 'X', 'X', 'X'] ,
['X', 'O', 'X', 'X', 'X', 'X', 'X'] ,
['X', 'X', 'O', 'O', 'X', 'O', 'O'] ,
['X', 'X', 'O', 'X', 'O', 'X', 'O']
]
#
#     Change all O with X when O is Surrounded by X
#     Example
#     ========
#     // Left,right, Bottom and Top are have X
#         X   X
#     X   O   O  X
#         X   X
#
#     Then change center O with X
#         X   X
#     X   X   X  X
#         X   X
#     -----------------------------
#     or
#         X
#     X   O  X
#         X
#     Then change center O with X
#         X
#     X   O  X
#         X

if __name__ == "__main__": main()``````

#### Output

``````  Before Surrounded
O   X   X   X   X   O   X
X   X   X   X   O   X   O
O   O   O   X   X   O   O
X   O   X   X   X   X   X
X   X   O   X   X   X   X
X   O   X   X   X   X   X
X   X   O   O   X   O   O
X   X   O   X   O   X   O

After Surrounded
O   X   X   X   X   O   X
X   X   X   X   X   X   O
O   O   O   X   X   O   O
X   O   X   X   X   X   X
X   X   X   X   X   X   X
X   X   X   X   X   X   X
X   X   O   O   X   O   O
X   X   O   X   O   X   O``````
``````#    Ruby Program for
#    surrounded regions

class Regions
#  Print record elements
def printresult(record, r, c)
i = 0
j = 0
while (i < r)
while (j < c)
print("  ", record[i][j])
j += 1
end

print("\n")
i += 1
j = 0
end

end

#  Fill corner elements with a "O" value
def fill(record, i, j, r, c)
if (i < 0 || i >= r || j < 0 || j >= c)
return
end

if (record[i][j] == '+')
#  Update element
record[i][j] = 'O'
#  visit to down element
self.fill(record, i + 1, j, r, c)
#  visit to up element
self.fill(record, i - 1, j, r, c)
#  visit to right element
self.fill(record, i, j + 1, r, c)
#  visit to left element
self.fill(record, i, j - 1, r, c)
end

end

#  Fill surrounded regions
def solve(record)
#  loop controlling variables
i = 0
j = 0
r = record.length
c = record[0].length
print("\n  Before Surrounded \n")
self.printresult(record, r, c)
#  First change the all zero with other symbol  "+"
while (i < r)
while (j < c)
if (record[i][j] == 'O')
record[i][j] = '+'
end

j += 1
end

i += 1
j = 0
end

#  change boundary
#  check first and last column
i = 0
while (i < r)
if (record[i][0] == '+')
self.fill(record, i, 0, r, c)
end

if (record[i][c - 1] == '+')
self.fill(record, i, c - 1, r, c)
end

i += 1
end

#  check first and last row
i = 0
while (i < c)
if (record[0][i] == '+')
self.fill(record, 0, i, r, c)
end

if (record[r - 1][i] == '+')
self.fill(record, r - 1, i, r, c)
end

i += 1
end

i = 0
while (i < r)
j = 0
while (j < c)
if (record[i][j] == '+')
record[i][j] = 'X'
end

j += 1
end

i += 1
end

print("\n  After Surrounded \n")
self.printresult(record, r, c)
end

end

def main()
record = [
['O', 'X', 'X', 'X', 'X', 'O', 'X'] ,
['X', 'X', 'X', 'X', 'O', 'X', 'O'] ,
['O', 'O', 'O', 'X', 'X', 'O', 'O'] ,
['X', 'O', 'X', 'X', 'X', 'X', 'X'] ,
['X', 'X', 'O', 'X', 'X', 'X', 'X'] ,
['X', 'O', 'X', 'X', 'X', 'X', 'X'] ,
['X', 'X', 'O', 'O', 'X', 'O', 'O'] ,
['X', 'X', 'O', 'X', 'O', 'X', 'O']
]
#
#     Change all O with X when O is Surrounded by X
#     Example
#     ========
#     // Left,right, Bottom and Top are have X
#         X   X
#     X   O   O  X
#         X   X
#
#     Then change center O with X
#         X   X
#     X   X   X  X
#         X   X
#     -----------------------------
#     or
#         X
#     X   O  X
#         X
#     Then change center O with X
#         X
#     X   O  X
#         X

end

main()``````

#### Output

``````  Before Surrounded
O  X  X  X  X  O  X
X  X  X  X  O  X  O
O  O  O  X  X  O  O
X  O  X  X  X  X  X
X  X  O  X  X  X  X
X  O  X  X  X  X  X
X  X  O  O  X  O  O
X  X  O  X  O  X  O

After Surrounded
O  X  X  X  X  O  X
X  X  X  X  X  X  O
O  O  O  X  X  O  O
X  O  X  X  X  X  X
X  X  X  X  X  X  X
X  X  X  X  X  X  X
X  X  O  O  X  O  O
X  X  O  X  O  X  O
``````
``````/*
Scala Program for
surrounded regions
*/
class Regions
{
// Print record elements
def printresult(record: Array[Array[Character]], r: Int, c: Int): Unit = {
var i: Int = 0;
var j: Int = 0;
while (i < r)
{
while (j < c)
{
print("  " + record(i)(j));
j += 1;
}
print("\n");
i += 1;
j = 0;
}
}
// Fill corner elements with a "O" value
def fill(record: Array[Array[Character]], i: Int, j: Int, r: Int, c: Int): Unit = {
if (i < 0 || i >= r || j < 0 || j >= c)
{
return;
}
if (record(i)(j) == '+')
{
// Update element
record(i)(j) = 'O';
// visit to down element
this.fill(record, i + 1, j, r, c);
// visit to up element
this.fill(record, i - 1, j, r, c);
// visit to right element
this.fill(record, i, j + 1, r, c);
// visit to left element
this.fill(record, i, j - 1, r, c);
}
}
// Fill surrounded regions
def solve(record: Array[Array[Character]]): Unit = {
// loop controlling variables
var i: Int = 0;
var j: Int = 0;
var r: Int = record.length;
var c: Int = record(0).length;
print("\n  Before Surrounded \n");
this.printresult(record, r, c);
// First change the all zero with other symbol  "+"
while (i < r)
{
while (j < c)
{
if (record(i)(j) == 'O')
{
record(i)(j) = '+';
}
j += 1;
}
i += 1;
j = 0;
}
// change boundary
// check first and last column
i = 0;
while (i < r)
{
if (record(i)(0) == '+')
{
this.fill(record, i, 0, r, c);
}
if (record(i)(c - 1) == '+')
{
this.fill(record, i, c - 1, r, c);
}
i += 1;
}
// check first and last row
i = 0;
while (i < c)
{
if (record(0)(i) == '+')
{
this.fill(record, 0, i, r, c);
}
if (record(r - 1)(i) == '+')
{
this.fill(record, r - 1, i, r, c);
}
i += 1;
}
i = 0;
while (i < r)
{
j = 0;
while (j < c)
{
if (record(i)(j) == '+')
{
record(i)(j) = 'X';
}
j += 1;
}
i += 1;
}
print("\n  After Surrounded \n");
this.printresult(record, r, c);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Regions = new Regions();
var record: Array[Array[Character]] = Array(
Array('O', 'X', 'X', 'X', 'X', 'O', 'X'),
Array('X', 'X', 'X', 'X', 'O', 'X', 'O'),
Array('O', 'O', 'O', 'X', 'X', 'O', 'O'),
Array('X', 'O', 'X', 'X', 'X', 'X', 'X'),
Array('X', 'X', 'O', 'X', 'X', 'X', 'X'),
Array('X', 'O', 'X', 'x', 'X', 'X', 'X'),
Array('X', 'X', 'O', 'O', 'X', 'O', 'O'),
Array('X', 'X', 'O', 'X', 'O', 'X', 'O')
);
/*
Change all O with X when O is Surrounded by X
Example
========
// Left,right, Bottom and Top are have X
X   X
X   O   O  X
X   X

Then change center O with X
X   X
X   X   X  X
X   X
-----------------------------
or
X
X   O  X
X
Then change center O with X
X
X   O  X
X
*/
}
}``````

#### Output

``````  Before Surrounded
O  X  X  X  X  O  X
X  X  X  X  O  X  O
O  O  O  X  X  O  O
X  O  X  X  X  X  X
X  X  O  X  X  X  X
X  O  X  X  X  X  X
X  X  O  O  X  O  O
X  X  O  X  O  X  O

After Surrounded
O  X  X  X  X  O  X
X  X  X  X  X  X  O
O  O  O  X  X  O  O
X  O  X  X  X  X  X
X  X  X  X  X  X  X
X  X  X  X  X  X  X
X  X  O  O  X  O  O
X  X  O  X  O  X  O``````
``````/*
Swift 4 Program for
surrounded regions
*/
class Regions
{
// Print record elements
func printresult(_ record: [
[Character]
], _ r: Int, _ c: Int)
{
var i: Int = 0;
var j: Int = 0;
while (i < r)
{
while (j < c)
{
print("  ", record[i][j], terminator: "");
j += 1;
}
print(terminator: "\n");
i += 1;
j = 0;
}
}
// Fill corner elements with a "O" value
func fill(_ record: inout[[Character]], _ i: Int, _ j: Int, _ r: Int, _ c: Int)
{
if (i < 0 || i >= r || j < 0 || j >= c)
{
return;
}
if (record[i][j] == "+")
{
// Update element
record[i][j] = "O";
// visit to down element
self.fill(&record, i + 1, j, r, c);
// visit to up element
self.fill(&record, i - 1, j, r, c);
// visit to right element
self.fill(&record, i, j + 1, r, c);
// visit to left element
self.fill(&record, i, j - 1, r, c);
}
}
// Fill surrounded regions
func solve(_ record: inout[[Character]])
{
// loop controlling variables
var i: Int = 0;
var j: Int = 0;
let r: Int = record.count;
let c: Int = record[0].count;
print("\n  Before Surrounded ");
self.printresult(record, r, c);
// First change the all zero with other symbol  "+"
while (i < r)
{
while (j < c)
{
if (record[i][j] == "O")
{
record[i][j] = "+";
}
j += 1;
}
i += 1;
j = 0;
}
// change boundary
// check first and last column
i = 0;
while (i < r)
{
if (record[i][0] == "+")
{
self.fill(&record, i, 0, r, c);
}
if (record[i][c - 1] == "+")
{
self.fill(&record, i, c - 1, r, c);
}
i += 1;
}
// check first and last row
i = 0;
while (i < c)
{
if (record[0][i] == "+")
{
self.fill(&record, 0, i, r, c);
}
if (record[r - 1][i] == "+")
{
self.fill(&record, r - 1, i, r, c);
}
i += 1;
}
i = 0;
while (i < r)
{
j = 0;
while (j < c)
{
if (record[i][j] == "+")
{
record[i][j] = "X";
}
j += 1;
}
i += 1;
}
print("\n  After Surrounded ");
self.printresult(record, r, c);
}
}
func main()
{
var record: [[Character]] = [
["O", "X", "X", "X", "X", "O", "X"] ,
["X", "X", "X", "X", "O", "X", "O"] ,
["O", "O", "O", "X", "X", "O", "O"] ,
["X", "O", "X", "X", "X", "X", "X"] ,
["X", "X", "O", "X", "X", "X", "X"] ,
["X", "O", "X", "X", "X", "X", "X"] ,
["X", "X", "O", "O", "X", "O", "O"] ,
["X", "X", "O", "X", "O", "X", "O"]
];
/*
Change all O with X when O is Surrounded by X
Example
========
// Left,right, Bottom and Top are have X
X   X
X   O   O  X
X   X

Then change center O with X
X   X
X   X   X  X
X   X
-----------------------------
or
X
X   O  X
X
Then change center O with X
X
X   O  X
X
*/
}
main();``````

#### Output

``````  Before Surrounded
O   X   X   X   X   O   X
X   X   X   X   O   X   O
O   O   O   X   X   O   O
X   O   X   X   X   X   X
X   X   O   X   X   X   X
X   O   X   X   X   X   X
X   X   O   O   X   O   O
X   X   O   X   O   X   O

After Surrounded
O   X   X   X   X   O   X
X   X   X   X   X   X   O
O   O   O   X   X   O   O
X   O   X   X   X   X   X
X   X   X   X   X   X   X
X   X   X   X   X   X   X
X   X   O   O   X   O   O
X   X   O   X   O   X   O``````
``````/*
Kotlin Program for
surrounded regions
*/
class Regions
{
// Print record elements
fun printresult(record: Array <Array<Char>> , r: Int, c: Int): Unit
{
var i: Int = 0;
var j: Int = 0;
while (i < r)
{
while (j < c)
{
print("  " + record[i][j]);
j += 1;
}
print("\n");
i += 1;
j = 0;
}
}
// Fill corner elements with a "O" value
fun fill(record: Array <Array<Char>> , i: Int, j: Int, r: Int, c: Int): Unit
{
if (i < 0 || i >= r || j < 0 || j >= c)
{
return;
}
if (record[i][j] == '+')
{
// Update element
record[i][j] = 'O';
// visit to down element
this.fill(record, i + 1, j, r, c);
// visit to up element
this.fill(record, i - 1, j, r, c);
// visit to right element
this.fill(record, i, j + 1, r, c);
// visit to left element
this.fill(record, i, j - 1, r, c);
}
}
// Fill surrounded regions
fun solve(record: Array < Array < Char >> ): Unit
{
// loop controlling variables
var i: Int = 0;
var j: Int = 0;
var r: Int = record.count();
var c: Int = record[0].count();
print("\n  Before Surrounded \n");
this.printresult(record, r, c);
// First change the all zero with other symbol  "+"
while (i < r)
{
while (j < c)
{
if (record[i][j] == 'O')
{
record[i][j] = '+';
}
j += 1;
}
i += 1;
j = 0;
}
// change boundary
// check first and last column
i = 0;
while (i < r)
{
if (record[i][0] == '+')
{
this.fill(record, i, 0, r, c);
}
if (record[i][c - 1] == '+')
{
this.fill(record, i, c - 1, r, c);
}
i += 1;
}
// check first and last row
i = 0;
while (i < c)
{
if (record[0][i] == '+')
{
this.fill(record, 0, i, r, c);
}
if (record[r - 1][i] == '+')
{
this.fill(record, r - 1, i, r, c);
}
i += 1;
}
i = 0;
while (i < r)
{
j = 0;
while (j < c)
{
if (record[i][j] == '+')
{
record[i][j] = 'X';
}
j += 1;
}
i += 1;
}
print("\n  After Surrounded \n");
this.printresult(record, r, c);
}
}
fun main(args: Array <String> ): Unit
{
var record: Array<Array<Char>> = arrayOf(
arrayOf('O', 'X', 'X', 'X', 'X', 'O', 'X'),
arrayOf('X', 'X', 'X', 'X', 'O', 'X', 'O'),
arrayOf('O', 'O', 'O', 'X', 'X', 'O', 'O'),
arrayOf('X', 'O', 'X', 'X', 'X', 'X', 'X'),
arrayOf('X', 'X', 'O', 'X', 'X', 'X', 'X'),
arrayOf('X', 'O', 'X', 'X', 'X', 'X', 'X'),
arrayOf('X', 'X', 'O', 'O', 'X', 'O', 'O'),
arrayOf('X', 'X', 'O', 'X', 'O', 'X', 'O')
);
/*
Change all O with X when O is Surrounded by X
Example
========
// Left,right, Bottom and Top are have X
X   X
X   O   O  X
X   X

Then change center O with X
X   X
X   X   X  X
X   X
-----------------------------
or
X
X   O  X
X
Then change center O with X
X
X   O  X
X
*/
}``````

#### Output

``````  Before Surrounded
O  X  X  X  X  O  X
X  X  X  X  O  X  O
O  O  O  X  X  O  O
X  O  X  X  X  X  X
X  X  O  X  X  X  X
X  O  X  X  X  X  X
X  X  O  O  X  O  O
X  X  O  X  O  X  O

After Surrounded
O  X  X  X  X  O  X
X  X  X  X  X  X  O
O  O  O  X  X  O  O
X  O  X  X  X  X  X
X  X  X  X  X  X  X
X  X  X  X  X  X  X
X  X  O  O  X  O  O
X  X  O  X  O  X  O``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.