📜  通过将 mat[i][j] 替换为 mat[ mat[i][j] ][ mat[j][i] ] 来重新排列给定的 Matrix

📅  最后修改于: 2022-05-13 01:57:22.312000             🧑  作者: Mango

通过将 mat[i][j] 替换为 mat[ mat[i][j] ][ mat[j][i] ] 来重新排列给定的 Matrix

给定一个 N 阶方阵mat[][] ,其中包含[0, N)范围内的整数,任务是以给定方式重新排列矩阵元素。将mat[i][j]处的元素替换为mat[x][y]处的元素,其中xmat[i][j]处的数字, ymat[j][i]处的数字。

例子:

方法:想法是按照问题的要求去。逐行遍历矩阵并找到 mat[i][j] 和 mat[j][i] 处的元素。使用这些数字 x 和 y 作为坐标来获得 mat[i][j] 的替换值。

下面是上述方法的实现:

C++
// C++ code to rearrange the matrix
// in specified manner.
#include 
using namespace std;
 
const int N = 5;
 
// Function to print matrix.
void showMatrix(int mat[][N])
{
    int i, j;
    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            cout << mat[i][j] << " ";
        }
        cout << endl;
    }
}
 
// Function to rearrange the matrix
// in specified manner.
void RearrangeMatrix(int mat[][N])
{
    int x, y, i, j;
    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            x = mat[i][j];
            y = mat[j][i];
            cout << mat[x][y] << " ";
        }
        cout << "\n";
    }
}
 
// Driver code
int main()
{
    int mat[][N] = { { 1, 3, 0, 4, 2 },
                     { 0, 1, 2, 3, 4 },
                     { 3, 4, 2, 1, 0 },
                     { 4, 1, 0, 2, 2 },
                     { 0, 1, 2, 1, 0 } };
 
    RearrangeMatrix(mat);
    return 0;
}


Java
// Java code to rearrange the matrix
// in specified manner.
import java.util.*;
public class GFG
{
 
static int N = 5;
 
// Function to print matrix.
static void showMatrix(int mat[][])
{
    int i, j;
    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            System.out.print(mat[i][j] + " ");
        }
        System.out.println();
    }
}
 
// Function to rearrange the matrix
// in specified manner.
static void RearrangeMatrix(int mat[][])
{
    int x, y, i, j;
    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            x = mat[i][j];
            y = mat[j][i];
            System.out.print(mat[x][y] + " ");
        }
        System.out.println();
    }
}
 
// Driver code
public static void main(String args[])
{
    int mat[][] = { { 1, 3, 0, 4, 2 },
                     { 0, 1, 2, 3, 4 },
                     { 3, 4, 2, 1, 0 },
                     { 4, 1, 0, 2, 2 },
                     { 0, 1, 2, 1, 0 } };
 
    RearrangeMatrix(mat);
 
}
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# Python code for the above approach
N = 5;
 
# Function to print matrix.
def showMatrix(mat):
    i = None
    j = None
    for i in range(N):
        for j in range(N):
            print(mat[i][j], end=" ");
        print('')
 
# Function to rearrange the matrix
# in specified manner.
def RearrangeMatrix(mat):
    x = None
    y = None
    i = None
    j = None
    for i in range(N):
        for j in range(N):
            x = mat[i][j];
            y = mat[j][i];
            print(mat[x][y], end= " ");
        print('')
 
# Driver code
mat = [[1, 3, 0, 4, 2], [0, 1, 2, 3, 4],
       [3, 4, 2, 1, 0], [4, 1, 0, 2, 2],
       [0, 1, 2, 1, 0]];
 
RearrangeMatrix(mat);
 
# This code is contributed by Saurabh Jaiswal


C#
// C# code to rearrange the matrix
// in specified manner.
using System;
public class GFG
{
 
  static int N = 5;
 
  // Function to print matrix.
  static void showMatrix(int[, ] mat)
  {
    int i, j;
    for (i = 0; i < N; i++) {
      for (j = 0; j < N; j++) {
        Console.Write(mat[i, j] + " ");
      }
      Console.WriteLine();
    }
  }
 
  // Function to rearrange the matrix
  // in specified manner.
  static void RearrangeMatrix(int[, ] mat)
  {
    int x, y, i, j;
    for (i = 0; i < N; i++) {
      for (j = 0; j < N; j++) {
        x = mat[i, j];
        y = mat[j, i];
        Console.Write(mat[x, y] + " ");
      }
      Console.WriteLine();
    }
  }
 
  // Driver code
  public static void Main()
  {
    int[, ] mat = { { 1, 3, 0, 4, 2 },
                   { 0, 1, 2, 3, 4 },
                   { 3, 4, 2, 1, 0 },
                   { 4, 1, 0, 2, 2 },
                   { 0, 1, 2, 1, 0 } };
 
    RearrangeMatrix(mat);
  }
}
 
// This code is contributed by ukasp.


Javascript



输出
1 4 4 0 3 
4 1 0 1 1 
4 2 2 0 0 
0 3 3 2 4 
0 4 3 2 1 

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