矩阵中的查询
给定一个大小为 mxn ( 1 <= m,n <= 1000 ) 的矩阵 M。它最初按行主要顺序依次填充从 1 到 mxn 的整数。任务是处理操纵 M 的查询列表,使得每个查询都是以下三个查询之一。
- R(x, y):交换 M 的第 x 行和第 y 行,其中 x 和 y 从 1 变化到 m。
- C(x, y):交换 M 的第 x 列和第 y 列,其中 x 和 y 从 1 变化到 n。
- P(x, y):打印第 x 行和第 y 列的元素,其中 x 从 1 到 m 变化,y 从 1 到 n 变化。
请注意,给定矩阵存储为典型的二维数组,其索引从 0 开始,但 x 和 y 的值从 1 开始。
例子:
Input : m = 3, n = 3
R(1, 2)
P(1, 1)
P(2, 1)
C(1, 2)
P(1, 1)
P(2, 1)
Output: value at (1, 1) = 4
value at (2, 1) = 1
value at (1, 1) = 5
value at (2, 1) = 2
Explanation:
The matrix is {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
After first R(1, 2) matrix becomes,
{{4, 5, 6},
{1, 2, 3},
{7, 8, 9}}
After first C(1, 2) matrix becomes,
{{5, 4, 6},
{2, 1, 3},
{8, 7, 9}}
Input : m = 1234, n = 5678
R(1, 2)
P(1, 1)
P(2, 1)
C(1, 2)
P(1, 1)
P(2, 1)
Output: value at (1, 1) = 5679
value at (2, 1) = 1
value at (1, 1) = 5680
value at (2, 1) = 2
这个问题的一个简单解决方案是手动完成所有查询,这意味着当我们必须交换行时,只需交换第 x 行和第 y 行的元素,类似地交换列。但是这种方法可能具有 q*O(m) 或 q*O(n) 的时间复杂度,其中 'q' 是查询次数和所需的辅助空间 O(m*n)。
解决这个问题的有效方法需要一点点数学观察。这里给定矩阵中的元素按行主要顺序从 1 到 mxn 依次填充,因此我们将利用这个给定的场景来解决这个问题。
- 创建一个辅助数组 rows[m] 并依次用值 0 到 m-1 填充它。
- 创建另一个辅助数组 cols[n] 并依次用值 0 到 n-1 填充它。
- 现在对于查询'R(x, y)',只需将 rows[x-1] 的值与 rows[y-1] 交换。
- 现在对于查询“C(x, y)”,只需将 cols[x-1] 的值与 cols[y-1] 交换即可。
- 现在对于查询 'P(x, y)' 只需跳过您看到的列数并按rows[x-1]*n + cols[y-1] + 1计算 (x, y) 处的值。
下面是上述想法的实现。
C++
// C++ implementation of program
#include
using namespace std;
// Fills initial values in rows[] and cols[]
void preprocessMatrix(int rows[], int cols[],
int m, int n)
{
// Fill rows with 1 to m-1
for (int i=0; i number of rows
// n --> number of columns
// ch --> type of query
// x --> number of row for query
// y --> number of column for query
void queryMatrix(int rows[], int cols[], int m,
int n, char ch, int x, int y)
{
// perform queries
int tmp;
switch(ch)
{
case 'R':
// swap row x with y
swap(rows[x-1], rows[y-1]);
break;
case 'C':
// swap column x with y
swap(cols[x-1], cols[y-1]);
break;
case 'P':
// Print value at (x, y)
printf("value at (%d, %d) = %d\n", x, y,
rows[x-1]*n + cols[y-1]+1);
break;
}
return ;
}
// Driver program to run the case
int main()
{
int m = 1234, n = 5678;
// row[] is array for rows and cols[]
// is array for columns
int rows[m], cols[n];
// Fill initial values in rows[] and cols[]
preprocessMatrix(rows, cols, m, n);
queryMatrix(rows, cols, m, n, 'R', 1, 2);
queryMatrix(rows, cols, m, n, 'P', 1, 1);
queryMatrix(rows, cols, m, n, 'P', 2, 1);
queryMatrix(rows, cols, m, n, 'C', 1, 2);
queryMatrix(rows, cols, m, n, 'P', 1, 1);
queryMatrix(rows, cols, m, n, 'P', 2, 1);
return 0;
}
Java
// Java implementation of program
class GFG
{
// Fills initial values in rows[] and cols[]
static void preprocessMatrix(int rows[], int cols[],
int m, int n)
{
// Fill rows with 1 to m-1
for (int i = 0; i < m; i++)
{
rows[i] = i;
}
// Fill columns with 1 to n-1
for (int i = 0; i < n; i++)
{
cols[i] = i;
}
}
// Function to perform queries on matrix
// m --> number of rows
// n --> number of columns
// ch --> type of query
// x --> number of row for query
// y --> number of column for query
static void queryMatrix(int rows[], int cols[], int m,
int n, char ch, int x, int y)
{
// perform queries
int tmp;
switch (ch)
{
case 'R':
// swap row x with y
swap(rows, x - 1, y - 1);
break;
case 'C':
// swap column x with y
swap(cols, x - 1, y - 1);
break;
case 'P':
// Print value at (x, y)
System.out.printf("value at (%d, %d) = %d\n", x, y,
rows[x - 1] * n + cols[y - 1] + 1);
break;
}
return;
}
static int[] swap(int[] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
// Driver code
public static void main(String[] args)
{
int m = 1234, n = 5678;
// row[] is array for rows and cols[]
// is array for columns
int rows[] = new int[m], cols[] = new int[n];
// Fill initial values in rows[] and cols[]
preprocessMatrix(rows, cols, m, n);
queryMatrix(rows, cols, m, n, 'R', 1, 2);
queryMatrix(rows, cols, m, n, 'P', 1, 1);
queryMatrix(rows, cols, m, n, 'P', 2, 1);
queryMatrix(rows, cols, m, n, 'C', 1, 2);
queryMatrix(rows, cols, m, n, 'P', 1, 1);
queryMatrix(rows, cols, m, n, 'P', 2, 1);
}
}
// This code contributed by Rajput-Ji
Python3
# Python3 implementation of program
# Fills initial values in rows[] and cols[]
def preprocessMatrix(rows, cols, m, n):
# Fill rows with 1 to m-1
for i in range(m):
rows[i] = i;
# Fill columns with 1 to n-1
for i in range(n):
cols[i] = i;
# Function to perform queries on matrix
# m --> number of rows
# n --> number of columns
# ch --> type of query
# x --> number of row for query
# y --> number of column for query
def queryMatrix(rows, cols, m, n, ch, x, y):
# perform queries
tmp = 0;
if ch == 'R':
# swap row x with y
rows[x-1], rows[y-1] = rows[y-1], rows[x-1];
elif ch == 'C':
# swap column x with y
cols[x-1], cols[y-1] = cols[y-1],cols[x-1];
elif ch == 'P':
# Print value at (x, y)
print('value at (',x,',',y,') = ',rows[x-1]*n + cols[y-1]+1, sep='');
return ;
# Driver program to run the case
m = 1234
n = 5678;
# row[] is array for rows and cols[]
# is array for columns
rows = [0 for i in range(m)]
cols = [0 for i in range(n)];
# Fill initial values in rows[] and cols[]
preprocessMatrix(rows, cols, m, n);
queryMatrix(rows, cols, m, n, 'R', 1, 2);
queryMatrix(rows, cols, m, n, 'P', 1, 1);
queryMatrix(rows, cols, m, n, 'P', 2, 1);
queryMatrix(rows, cols, m, n, 'C', 1, 2);
queryMatrix(rows, cols, m, n, 'P', 1, 1);
queryMatrix(rows, cols, m, n, 'P', 2, 1);
# This code is contributed by rutvik_56.
C#
// C# implementation of program
using System;
class GFG
{
// Fills initial values in rows[] and cols[]
static void preprocessMatrix(int []rows, int []cols,
int m, int n)
{
// Fill rows with 1 to m-1
for (int i = 0; i < m; i++)
{
rows[i] = i;
}
// Fill columns with 1 to n-1
for (int i = 0; i < n; i++)
{
cols[i] = i;
}
}
// Function to perform queries on matrix
// m --> number of rows
// n --> number of columns
// ch --> type of query
// x --> number of row for query
// y --> number of column for query
static void queryMatrix(int []rows, int []cols, int m,
int n, char ch, int x, int y)
{
// perform queries
int tmp;
switch (ch)
{
case 'R':
// swap row x with y
swap(rows, x - 1, y - 1);
break;
case 'C':
// swap column x with y
swap(cols, x - 1, y - 1);
break;
case 'P':
// Print value at (x, y)
Console.Write("value at ({0}, {1}) = {2}\n", x, y,
rows[x - 1] * n + cols[y - 1] + 1);
break;
}
return;
}
static int[] swap(int[] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
// Driver code
public static void Main()
{
int m = 1234, n = 5678;
// row[] is array for rows and cols[]
// is array for columns
int []rows = new int[m]; int []cols = new int[n];
// Fill initial values in rows[] and cols[]
preprocessMatrix(rows, cols, m, n);
queryMatrix(rows, cols, m, n, 'R', 1, 2);
queryMatrix(rows, cols, m, n, 'P', 1, 1);
queryMatrix(rows, cols, m, n, 'P', 2, 1);
queryMatrix(rows, cols, m, n, 'C', 1, 2);
queryMatrix(rows, cols, m, n, 'P', 1, 1);
queryMatrix(rows, cols, m, n, 'P', 2, 1);
}
}
/* This code contributed by PrinciRaj1992 */
Javascript
输出:
value at (1, 1) = 5679
value at (2, 1) = 1
value at (1, 1) = 5680
value at (2, 1) = 2