📌  相关文章
📜  给定矩阵“ O”和“ X”,如果被“ X”包围,则将“ O”替换为“ X”

📅  最后修改于: 2021-04-23 21:57:18             🧑  作者: Mango

给定一个矩阵,其中每个元素都是“ O”或“ X”,如果被“ X”包围,则用“ X”替换“ O”。如果“ O”(或一组“ O”)在其正下方,正上方,正左侧和正右侧的位置处存在“ X”,则视为被“ X”包围。
例子:

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'}
                    };
Input: mat[M][N] =  {{'X', 'X', 'X', 'X'}
                     {'X', 'X', 'X', 'X'}
                     {'X', 'X', 'X', 'X'}
                     {'X', 'X', 'X', 'X'}
                     {'X', 'X', 'O', 'O'}
                    };

这主要是Flood-Fill算法的应用。此处的主要区别在于,如果“ O”位于以边界结尾的区域中,则不会被“ X”代替。以下是执行此特殊洪水填充的简单步骤。
1)遍历给定的矩阵,并用特殊字符“-”替换所有“ O”。
2)遍历给定矩阵的四个边缘,并为边缘上的每个“-”调用floodFill(’-‘,’O’)。其余的“-”是表示要用“ X”替换的“ O”(在原始矩阵中)的字符。
3)遍历矩阵并将所有的“-”替换为“ X”。
让我们以示例的形式查看上述算法的步骤。令以下为输入矩阵。

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'},
                    };

步骤1:将所有“ O”替换为“-”。

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', '-', '-', '-'},
                    };

步骤2:对所有等于“-”的边元素调用FloodFill(’-‘,’O’)

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'},
                    };

步骤3:将所有的“-”替换为“ 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'},
                    };

以下是上述算法的实现。

C++
// A C++ program to replace all 'O's with 'X''s if surrounded by 'X'
#include
using namespace std;
 
// Size of given matrix is M X N
#define M 6
#define N 6
 
 
// A recursive function to replace previous value 'prevV' at  '(x, y)'
// and all surrounding values of (x, y) with new value 'newV'.
void floodFillUtil(char mat[][N], int x, int y, char prevV, char newV)
{
    // Base cases
    if (x < 0 || x >= M || y < 0 || y >= N)
        return;
    if (mat[x][y] != prevV)
        return;
 
    // Replace the color at (x, y)
    mat[x][y] = newV;
 
    // Recur for north, east, south and west
    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);
}
 
// Returns size of maximum size subsquare matrix
// surrounded by 'X'
int replaceSurrounded(char mat[][N])
{
   // Step 1: Replace all 'O'  with '-'
   for (int i=0; i


Java
// A Java program to replace
// all 'O's with 'X''s if
// surrounded by 'X'
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)
    {
        // Base cases
        if (x < 0 || x >= M ||
            y < 0 || y >= N)
            return;
             
        if (mat[x][y] != prevV)
            return;
     
        // Replace the color at (x, y)
        mat[x][y] = newV;
     
        // Recur for north,
        // east, south and west
        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);
    }
     
    // Returns size of maximum
    // size subsquare matrix
    // surrounded by 'X'
    static void replaceSurrounded(char mat[][])
    {
         
    // Step 1: Replace
    // all 'O' with '-'
    for (int i = 0; i < M; i++)
        for (int j = 0; j < N; j++)
            if (mat[i][j] == 'O')
                mat[i][j] = '-';
     
    // Call floodFill for
    // all '-' lying on edges
    for (int i = 0; i < M; i++) // Left side
        if (mat[i][0] == '-')
            floodFillUtil(mat, i, 0,
                          '-', 'O');
    for (int i = 0; i < M; i++) // Right side
        if (mat[i][N - 1] == '-')
            floodFillUtil(mat, i, N - 1,
                          '-', 'O');
    for (int i = 0; i < N; i++) // Top side
        if (mat[0][i] == '-')
            floodFillUtil(mat, 0, i,
                          '-', 'O');
    for (int 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 (int i = 0; i < M; i++)
        for (int j = 0; j < N; j++)
            if (mat[i][j] == '-')
                mat[i][j] = 'X';
    }
     
    // Driver Code
    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("");
        }
    }
}
 
// This code is contributed
// by shiv_bhakt


Python3
# Python3 program to replace all 'O's with
# 'X's if surrounded by 'X'
 
# Size of given matrix is M x N
M = 6
N = 6
 
# A recursive function to replace previous
# value 'prevV' at '(x, y)' and all surrounding
# values of (x, y) with new value 'newV'.
def floodFillUtil(mat, x, y, prevV, newV):
 
    # Base Cases
    if (x < 0 or x >= M or y < 0 or y >= N):
        return
 
    if (mat[x][y] != prevV):
        return
 
    # Replace the color at (x, y)
    mat[x][y] = newV
 
    # Recur for north, east, south and west
    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)
 
# Returns size of maximum size subsquare
#  matrix surrounded by 'X'
def replaceSurrounded(mat):
 
    # Step 1: Replace all 'O's with '-'
    for i in range(M):
        for j in range(N):
            if (mat[i][j] == 'O'):
                mat[i][j] = '-'
 
    # Call floodFill for all '-'
    # lying on edges
     # Left Side
    for i in range(M):
        if (mat[i][0] == '-'):
            floodFillUtil(mat, i, 0, '-', 'O')
     
    # Right side
    for i in range(M):
        if (mat[i][N - 1] == '-'):
            floodFillUtil(mat, i, N - 1, '-', 'O')
     
    # Top side
    for i in range(N):
        if (mat[0][i] == '-'):
            floodFillUtil(mat, 0, i, '-', 'O')
     
    # Bottom side
    for i in range(N):
        if (mat[M - 1][i] == '-'):
            floodFillUtil(mat, M - 1, i, '-', 'O')
 
    # Step 3: Replace all '-' with 'X'
    for i in range(M):
        for j in range(N):
            if (mat[i][j] == '-'):
                mat[i][j] = 'X'
 
# Driver code
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])
 
# This code is contributed by himanshu77


C#
// A C# program to replace
// all 'O's with 'X''s if
// surrounded by 'X'
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)
    {
        // Base cases
        if (x < 0 || x >= M ||
            y < 0 || y >= N)
            return;
             
        if (mat[x, y] != prevV)
            return;
     
        // Replace the color at (x, y)
        mat[x, y] = newV;
      
        // Recur for north,
        // east, south and west
        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);
    }
     
    // Returns size of maximum
    // size subsquare matrix
    // surrounded by 'X'
    static void replaceSurrounded(char [,]mat)
    {
         
    // Step 1: Replace
    // all 'O' with '-'
    for (int i = 0; i < M; i++)
        for (int j = 0; j < N; j++)
            if (mat[i, j] == 'O')
                mat[i, j] = '-';
     
    // Call floodFill for
    // all '-' lying on edges
    for (int i = 0; i < M; i++) // Left side
        if (mat[i, 0] == '-')
            floodFillUtil(mat, i, 0,
                          '-', 'O');
    for (int i = 0; i < M; i++) // Right side
        if (mat[i, N - 1] == '-')
            floodFillUtil(mat, i, N - 1,
                          '-', 'O');
    for (int i = 0; i < N; i++) // Top side
        if (mat[0, i] == '-')
            floodFillUtil(mat, 0, i,
                          '-', 'O');
    for (int 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 (int i = 0; i < M; i++)
        for (int j = 0; j < N; j++)
            if (mat[i, j] == '-')
                mat[i, j] = 'X';
    }
     
    // Driver Code
    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("");
        }
    }
}
 
// This code is contributed
// by shiv_bhakt


PHP
= $GLOBALS['M'] ||
        $y < 0 || $y >= $GLOBALS['N'])
        return;
    if ($mat[$x][$y] != $prevV)
        return;
 
    // Replace the color at (x, y)
    $mat[$x][$y] = $newV;
 
    // Recur for north,
    // east, south and west
    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);
}
 
// Returns size of maximum
// size subsquare matrix
// surrounded by 'X'
function replaceSurrounded(&$mat)
{
     
// Step 1: Replace all 'O' with '-'
for ($i = 0; $i < $GLOBALS['M']; $i++)
    for ($j = 0; $j < $GLOBALS['N']; $j++)
        if ($mat[$i][$j] == 'O')
            $mat[$i][$j] = '-';
 
// Call floodFill for all
// '-' lying on edges
for ($i = 0;
     $i < $GLOBALS['M']; $i++) // Left side
    if ($mat[$i][0] == '-')
        floodFillUtil($mat, $i, 0, '-', 'O');
         
for ($i = 0; $i < $GLOBALS['M']; $i++) // Right side
    if ($mat[$i][$GLOBALS['N'] - 1] == '-')
        floodFillUtil($mat, $i,
                      $GLOBALS['N'] - 1, '-', 'O');
         
for ($i = 0; $i < $GLOBALS['N']; $i++) // Top side
    if ($mat[0][$i] == '-')
        floodFillUtil($mat, 0, $i, '-', 'O');
         
for ($i = 0; $i < $GLOBALS['N']; $i++) // Bottom side
    if ($mat[$GLOBALS['M'] - 1][$i] == '-')
        floodFillUtil($mat, $GLOBALS['M'] - 1,
                            $i, '-', 'O');
 
// Step 3: Replace all '-' with 'X'
for ($i = 0; $i < $GLOBALS['M']; $i++)
    for ($j = 0; $j < $GLOBALS['N']; $j++)
        if ($mat[$i][$j] == '-')
            $mat[$i][$j] = 'X';
 
}
 
// Driver Code
$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";
}
 
// This code is contributed by ChitraNayal
?>


Javascript


输出:

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