📌  相关文章
📜  给定矩阵从左上角到右下角单元格的路径的最大XOR

📅  最后修改于: 2021-05-25 08:11:22             🧑  作者: Mango

给定尺寸为N * M的矩阵mat [] [] ,任务是打印从左上单元格(0,0)到右下单元格( 0,0)的路径可获得的最大按位XOR值。 N – 1,M – 1)的给定矩阵。 (i,j)(i,j + 1)只能从任何像元(i,j)移动。
注意:路径的按位XOR值定义为该路径上所有可能元素的按位XOR。

例子:

方法:想法是使用递归生成从给定矩阵的左上单元格(0,0)到右下单元格(N – 1,M – 1)的所有可能路径,并打印所有路径的最大XOR值可能的路径。以下是递归关系及其基本情况:

请按照以下步骤解决问题:

  • 初始化一个变量,例如xorValue,以存储从左上单元格(0,0)到右下单元格(N – 1,M – 1)的路径上所有可能元素的按位XOR。
  • 使用上述递归关系可以找到从左上单元格(0,0)到右下单元格(N – 1,M – 1)的所有可能路径的最大XOR值。
  • 最后,打印从左上角的单元格(0,0)到右下角的单元格(N – 1,M – 1)的所有可能路径的最大XOR值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to print maximum XOR
// value of all possible path
// from (0, 0) to (N - 1, M - 1)
int printMaxXOR(vector >& mat,
                int i, int j,
                int xorValue)
{
    // Base case
    if (i == 0 && j == 0) {
        return mat[i][j] ^ xorValue;
    }
 
    // Base case
    if (i == 0) {
 
        // Stores maximum XOR value
        // by selecting path from (i, j)
        // to (i, j - 1)
        return printMaxXOR(mat, i, j - 1,
                           mat[i][j] ^ xorValue);
    }
 
    if (j == 0) {
 
        // Stores maximum XOR value
        // by selecting path from (i, j)
        // to (i - 1, j)
        return printMaxXOR(mat, i - 1, j,
                           mat[i][j] ^ xorValue);
    }
 
    // Stores maximum XOR value
    // by selecting path from (i, j)
    // to (i - 1, j)
    int X
        = printMaxXOR(mat, i - 1,
                      j, mat[i][j] ^ xorValue);
 
    // Stores maximum XOR value
    // by selecting path from (i, j)
    // to (i, j - 1)
    int Y
        = printMaxXOR(mat, i, j - 1,
                      mat[i][j] ^ xorValue);
 
    return max(X, Y);
}
 
// Driver Code
int main()
{
    vector > mat
        = { { 3, 2, 1 },
           { 6, 5, 4 },
           { 7, 8, 9 } };
    int N = mat.size();
    int M = mat[0].size();
 
    // Stores bitwise XOR of
    // all elements on each possible path
    int xorValue = 0;
    cout << printMaxXOR(mat, N - 1,
                        M - 1, xorValue);
}


Java
import java.io.*;
import java.util.*;
class GFG {
    public static int printMaxXOR(int[][] mat,
                                  int i, int j,
                                  int xorValue)
    {
        // Base case
        if (i == 0 && j == 0)
        {
            return mat[i][j] ^ xorValue;
        }
 
        // Base case
        if (i == 0) {
 
            // Stores maximum XOR value
            // by selecting path from (i, j)
            // to (i, j - 1)
            return printMaxXOR(mat, i,
                               j - 1,
                               mat[i][j] ^ xorValue);
        }
 
        if (j == 0) {
 
            // Stores maximum XOR value
            // by selecting path from (i, j)
            // to (i - 1, j)
            return printMaxXOR(mat,
                               i - 1, j,
                               mat[i][j] ^ xorValue);
        }
 
        // Stores maximum XOR value
        // by selecting path from (i, j)
        // to (i - 1, j)
        int X = printMaxXOR(mat,
                            i - 1, j,
                            mat[i][j] ^ xorValue);
 
        // Stores maximum XOR value
        // by selecting path from (i, j)
        // to (i, j - 1)
        int Y = printMaxXOR(mat,
                            i, j - 1,
                            mat[i][j] ^ xorValue);
 
        return Math.max(X, Y);
    }
   
    // Driver Code
    public static void main(String[] args)
    {
 
        int[][] mat
            = { { 3, 2, 1 },
               { 6, 5, 4 },
               { 7, 8, 9 } };
        int N = mat.length;
        int M = mat[0].length;
 
        // Stores bitwise XOR of
        // all elements on each possible path
        int xorValue = 0;
        System.out.println(
            printMaxXOR(mat, N - 1, M - 1, xorValue));
    }
    // This code is contributed by hemanth gadarla
}


Python3
# Python 3 program to implement
# the above approach
 
# Function to print maximum XOR
# value of all possible path
# from (0, 0) to (N - 1, M - 1)
def printMaxXOR(mat, i, j,
                xorValue):
 
    # Base case
    if (i == 0 and j == 0):
        return mat[i][j] ^ xorValue
 
    # Base case
    if (i == 0):
 
        # Stores maximum XOR value
        # by selecting path from (i, j)
        # to (i, j - 1)
        return printMaxXOR(mat, i, j - 1,
                           mat[i][j] ^
                           xorValue)     
    if (j == 0):
 
        # Stores maximum XOR value
        # by selecting path from (i, j)
        # to (i - 1, j)
        return printMaxXOR(mat, i - 1, j,
                           mat[i][j] ^
                           xorValue)
 
    # Stores maximum XOR value
    # by selecting path from (i, j)
    # to (i - 1, j)
    X = printMaxXOR(mat, i - 1,
                    j, mat[i][j] ^
                    xorValue)
 
    # Stores maximum XOR value
    # by selecting path from (i, j)
    # to (i, j - 1)
    Y = printMaxXOR(mat, i, j - 1,
                    mat[i][j] ^
                    xorValue)
 
    return max(X, Y)
 
  # Driver Code
if __name__ == "__main__":
 
    mat = [[3, 2, 1],
           [6, 5, 4],
           [7, 8, 9]]
    N = len(mat)
    M = len(mat[0])
 
    # Stores bitwise XOR of
    # all elements on each
    # possible path
    xorValue = 0
    print(printMaxXOR(mat, N - 1,
                      M - 1,
                      xorValue))
 
# This code is contributed by Chitranayal


C#
// C# program to implement the
// above approach
using System;
class GFG{
     
public static int printMaxXOR(int[,] mat,
                              int i, int j,
                              int xorValue)
{
  // Base case
  if (i == 0 && j == 0)
  {
    return mat[i,j] ^
           xorValue;
  }
 
  // Base case
  if (i == 0)
  {
    // Stores maximum XOR value
    // by selecting path from (i, j)
    // to (i, j - 1)
    return printMaxXOR(mat, i,
                       j - 1,
                       mat[i,j] ^
                       xorValue);
  }
 
  if (j == 0)
  {
    // Stores maximum XOR value
    // by selecting path from (i, j)
    // to (i - 1, j)
    return printMaxXOR(mat,
                       i - 1, j,
                       mat[i,j] ^
                       xorValue);
  }
 
  // Stores maximum XOR value
  // by selecting path from (i, j)
  // to (i - 1, j)
  int X = printMaxXOR(mat,
                      i - 1, j,
                      mat[i,j] ^
                      xorValue);
 
  // Stores maximum XOR value
  // by selecting path from (i, j)
  // to (i, j - 1)
  int Y = printMaxXOR(mat,
                      i, j - 1,
                      mat[i,j] ^
                      xorValue);
 
  return Math.Max(X, Y);
}
   
// Driver Code
public static void Main(String[] args)
{
  int[,] mat = {{3, 2, 1},
                {6, 5, 4}, 
                {7, 8, 9}};
  int N = mat.GetLength(0);
  int M = mat.GetLength(1);
 
  // Stores bitwise XOR of
  // all elements on each
  // possible path
  int xorValue = 0;
  Console.WriteLine(printMaxXOR(mat, N - 1,
                                M - 1,
                                xorValue));
}
}
 
// This code is contributed by gauravrajput1


Javascript


C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
#define N 3
#define M 3
 
int printMaxXOR(int mat[][N],
                int n, int m)
{
  int dp[n + 2][m + 2];
 
  // Initialise dp 1st row
  // and 1st column
  for (int i = 0; i < n; i++)
    dp[i][0] = ((i - 1 >= 0) ?
                 dp[i - 1][0] : 0) ^
                 mat[i][0];
   
  for (int j = 0; j < m; j++)
    dp[0][j] = ((j - 1 >= 0) ?
                 dp[0][j - 1] : 0) ^
                 mat[0][j];
 
  // d[i][j] =maxXOr value you can
  //  get till ith row and jth column
  for (int i = 1; i < n; i++)
  {
    for (int j = 1; j < m; j++)
    {
      // Find the maximum value You
      // can get from the top (i-1,j)
      // and left (i,j-1)
      int X = mat[i][j] ^
              dp[i - 1][j];
      int Y = mat[i][j] ^
              dp[i][j - 1];
      dp[i][j] = max(X, Y);
    }
  }
   
  // Return the maximum
  // Xorvalue
  return dp[n - 1][m - 1];
}
 
// Driver Code
int main()
{
  int mat[M][N] = {{3, 2, 1},
                   {6, 5, 4},
                   {7, 8, 9}};
 
  // Stores bitwise XOR of
  // all elements on each
  // possible path
  int xorValue = 0;
  cout << (printMaxXOR(mat,
                       N, M));
}
 
// This code is contributed by gauravrajput1


Java
import java.io.*;
import java.util.*;
class GFG {
    public static int printMaxXOR(int[][] mat,
                                  int n, int m)
    {
        int dp[][] = new int[n + 2][m + 2];
 
        // Initialise dp 1st row and 1st column
        for (int i = 0; i < n; i++)
            dp[i][0] = ((i - 1 >= 0)
                       ? dp[i - 1][0] : 0)
                       ^ mat[i][0];
        for (int j = 0; j < m; j++)
            dp[0][j] = ((j - 1 >= 0)
                       ? dp[0][j - 1] : 0)
                       ^ mat[0][j];
         
        // d[i][j] =maxXOr value you can
        //  get till ith row and jth column
        for (int i = 1; i < n; i++)
        {
            for (int j = 1; j < m; j++)
            {
                // Find the maximum value You
                // can get from the top (i-1,j)
                // and left (i,j-1)
                int X = mat[i][j] ^ dp[i - 1][j];
                int Y = mat[i][j] ^ dp[i][j - 1];
                dp[i][j] = Math.max(X, Y);
            }
        }
        // Return the maximum Xorvalue
        return dp[n - 1][m - 1];
    }
   
    // Driver Code
    public static void main(String[] args)
    {
 
        int[][] mat
            = { { 3, 2, 1 },
               { 6, 5, 4 },
               { 7, 8, 9 } };
        int N = mat.length;
        int M = mat[0].length;
 
        // Stores bitwise XOR of
        // all elements on each possible path
        int xorValue = 0;
        System.out.println(printMaxXOR(mat, N, M));
    }
    // This code is contributed by hemanth gadarla
}


Python3
# Python3 program for the
# above approach
def printMaxXOR(mat, n, m):
   
    dp = [[0 for i in range(m+2)]
             for j in range(n+2)];
 
    # Initialise dp 1st row and
    # 1st column
    for i in range(n):
        if((i - 1) >= 0):
            dp[i][0] = (dp[i - 1][0] ^
                        mat[i][0]);
        else:
            dp[i][0] =  0 ^ mat[i][0];
             
    for j in range(m):
        if((j - 1) >= 0):
            dp[0][j] = (dp[0][j - 1] ^
                        mat[0][j]);
        else:
            dp[0][j] = 0 ^ mat[0][j];
             
    # d[i][j] = maxXOr value you can
    # get till ith row and jth column
    for i in range(1, n):
        for j in range(1, m):
           
            # Find the maximum value You
            # can get from the top (i-1,j)
            # and left (i,j-1)
            X = (mat[i][j] ^
                 dp[i - 1][j]);
            Y = (mat[i][j] ^
                 dp[i][j - 1]);
            dp[i][j] = max(X, Y);
 
    # Return the maximum
    # Xorvalue
    return dp[n - 1][m - 1];
 
# Driver Code
if __name__ == '__main__':
   
    mat = [[3, 2, 1],
           [6, 5, 4],
           [7, 8, 9]];
    N = len(mat);
    M = len(mat[0]);
 
    # Stores bitwise XOR of
    # all elements on each
    # possible path
    xorValue = 0;
    print(printMaxXOR(mat, N, M));
 
# This code is contributed by gauravrajput1


C#
// C# Program for the
// above approach
using System;
class GFG{
   
public static int printMaxXOR(int[,] mat,
                              int n, int m)
{
  int [,]dp = new int[n + 2, m + 2];
 
  // Initialise dp 1st row and
  // 1st column
  for (int i = 0; i < n; i++)
    dp[i, 0] = ((i - 1 >= 0) ?
                 dp[i - 1, 0] : 0) ^
                 mat[i, 0];
   
  for (int j = 0; j < m; j++)
    dp[0, j] = ((j - 1 >= 0) ?
                 dp[0, j - 1] : 0) ^
                 mat[0, j];
 
  // d[i,j] =maxXOr value you can
  //  get till ith row and jth column
  for (int i = 1; i < n; i++)
  {
    for (int j = 1; j < m; j++)
    {
      // Find the maximum value You
      // can get from the top (i-1,j)
      // and left (i,j-1)
      int X = mat[i, j] ^ dp[i - 1, j];
      int Y = mat[i, j] ^ dp[i, j - 1];
      dp[i, j] = Math.Max(X, Y);
    }
  }
  // Return the maximum Xorvalue
  return dp[n - 1, m - 1];
}
 
// Driver Code
public static void Main(String[] args)
{
  int[,] mat = {{3, 2, 1},
                {6, 5, 4},
                {7, 8, 9}};
  int N = mat.GetLength(0);
  int M = mat.GetLength(1);
 
  // Stores bitwise XOR of
  // all elements on each
  // possible path
  int xorValue = 0;
  Console.WriteLine(printMaxXOR(mat,
                                N, M));
}
}
 
// This code is contributed by Rajput-Ji


输出
13

时间复杂度: O(2 N )
辅助空间: O(1)

高效方法:可以通过动态编程来优化上述方法.dp二维数组存储了我们可以获取到该行和列的maxxor值的值。

  • dp [i] [j] = max(dp [i-1] [j] ^ mat [i] [j],dp [i] [j-1] ^ mat [i] [j])
  • 最终结果存储在dp [n-1] [m-1]中
  • dp [i] [j] = maxxor直到第i行和第j列

下面是上述方法的实现:

C++

// C++ Program to implement
// the above approach
#include 
using namespace std;
#define N 3
#define M 3
 
int printMaxXOR(int mat[][N],
                int n, int m)
{
  int dp[n + 2][m + 2];
 
  // Initialise dp 1st row
  // and 1st column
  for (int i = 0; i < n; i++)
    dp[i][0] = ((i - 1 >= 0) ?
                 dp[i - 1][0] : 0) ^
                 mat[i][0];
   
  for (int j = 0; j < m; j++)
    dp[0][j] = ((j - 1 >= 0) ?
                 dp[0][j - 1] : 0) ^
                 mat[0][j];
 
  // d[i][j] =maxXOr value you can
  //  get till ith row and jth column
  for (int i = 1; i < n; i++)
  {
    for (int j = 1; j < m; j++)
    {
      // Find the maximum value You
      // can get from the top (i-1,j)
      // and left (i,j-1)
      int X = mat[i][j] ^
              dp[i - 1][j];
      int Y = mat[i][j] ^
              dp[i][j - 1];
      dp[i][j] = max(X, Y);
    }
  }
   
  // Return the maximum
  // Xorvalue
  return dp[n - 1][m - 1];
}
 
// Driver Code
int main()
{
  int mat[M][N] = {{3, 2, 1},
                   {6, 5, 4},
                   {7, 8, 9}};
 
  // Stores bitwise XOR of
  // all elements on each
  // possible path
  int xorValue = 0;
  cout << (printMaxXOR(mat,
                       N, M));
}
 
// This code is contributed by gauravrajput1

Java

import java.io.*;
import java.util.*;
class GFG {
    public static int printMaxXOR(int[][] mat,
                                  int n, int m)
    {
        int dp[][] = new int[n + 2][m + 2];
 
        // Initialise dp 1st row and 1st column
        for (int i = 0; i < n; i++)
            dp[i][0] = ((i - 1 >= 0)
                       ? dp[i - 1][0] : 0)
                       ^ mat[i][0];
        for (int j = 0; j < m; j++)
            dp[0][j] = ((j - 1 >= 0)
                       ? dp[0][j - 1] : 0)
                       ^ mat[0][j];
         
        // d[i][j] =maxXOr value you can
        //  get till ith row and jth column
        for (int i = 1; i < n; i++)
        {
            for (int j = 1; j < m; j++)
            {
                // Find the maximum value You
                // can get from the top (i-1,j)
                // and left (i,j-1)
                int X = mat[i][j] ^ dp[i - 1][j];
                int Y = mat[i][j] ^ dp[i][j - 1];
                dp[i][j] = Math.max(X, Y);
            }
        }
        // Return the maximum Xorvalue
        return dp[n - 1][m - 1];
    }
   
    // Driver Code
    public static void main(String[] args)
    {
 
        int[][] mat
            = { { 3, 2, 1 },
               { 6, 5, 4 },
               { 7, 8, 9 } };
        int N = mat.length;
        int M = mat[0].length;
 
        // Stores bitwise XOR of
        // all elements on each possible path
        int xorValue = 0;
        System.out.println(printMaxXOR(mat, N, M));
    }
    // This code is contributed by hemanth gadarla
}

Python3

# Python3 program for the
# above approach
def printMaxXOR(mat, n, m):
   
    dp = [[0 for i in range(m+2)]
             for j in range(n+2)];
 
    # Initialise dp 1st row and
    # 1st column
    for i in range(n):
        if((i - 1) >= 0):
            dp[i][0] = (dp[i - 1][0] ^
                        mat[i][0]);
        else:
            dp[i][0] =  0 ^ mat[i][0];
             
    for j in range(m):
        if((j - 1) >= 0):
            dp[0][j] = (dp[0][j - 1] ^
                        mat[0][j]);
        else:
            dp[0][j] = 0 ^ mat[0][j];
             
    # d[i][j] = maxXOr value you can
    # get till ith row and jth column
    for i in range(1, n):
        for j in range(1, m):
           
            # Find the maximum value You
            # can get from the top (i-1,j)
            # and left (i,j-1)
            X = (mat[i][j] ^
                 dp[i - 1][j]);
            Y = (mat[i][j] ^
                 dp[i][j - 1]);
            dp[i][j] = max(X, Y);
 
    # Return the maximum
    # Xorvalue
    return dp[n - 1][m - 1];
 
# Driver Code
if __name__ == '__main__':
   
    mat = [[3, 2, 1],
           [6, 5, 4],
           [7, 8, 9]];
    N = len(mat);
    M = len(mat[0]);
 
    # Stores bitwise XOR of
    # all elements on each
    # possible path
    xorValue = 0;
    print(printMaxXOR(mat, N, M));
 
# This code is contributed by gauravrajput1

C#

// C# Program for the
// above approach
using System;
class GFG{
   
public static int printMaxXOR(int[,] mat,
                              int n, int m)
{
  int [,]dp = new int[n + 2, m + 2];
 
  // Initialise dp 1st row and
  // 1st column
  for (int i = 0; i < n; i++)
    dp[i, 0] = ((i - 1 >= 0) ?
                 dp[i - 1, 0] : 0) ^
                 mat[i, 0];
   
  for (int j = 0; j < m; j++)
    dp[0, j] = ((j - 1 >= 0) ?
                 dp[0, j - 1] : 0) ^
                 mat[0, j];
 
  // d[i,j] =maxXOr value you can
  //  get till ith row and jth column
  for (int i = 1; i < n; i++)
  {
    for (int j = 1; j < m; j++)
    {
      // Find the maximum value You
      // can get from the top (i-1,j)
      // and left (i,j-1)
      int X = mat[i, j] ^ dp[i - 1, j];
      int Y = mat[i, j] ^ dp[i, j - 1];
      dp[i, j] = Math.Max(X, Y);
    }
  }
  // Return the maximum Xorvalue
  return dp[n - 1, m - 1];
}
 
// Driver Code
public static void Main(String[] args)
{
  int[,] mat = {{3, 2, 1},
                {6, 5, 4},
                {7, 8, 9}};
  int N = mat.GetLength(0);
  int M = mat.GetLength(1);
 
  // Stores bitwise XOR of
  // all elements on each
  // possible path
  int xorValue = 0;
  Console.WriteLine(printMaxXOR(mat,
                                N, M));
}
}
 
// This code is contributed by Rajput-Ji
输出
13

时间复杂度: O(N * M)
辅助空间复杂度: O(N * M)