logo

Matristeki Sorgular

İki tam sayı verildiğinde m ve n bu siparişi açıklıyor m*n bir matrisin ile birlikte[][] ilk olarak dolu tam sayılarla 1 ila m*n sırayla bir satır ana sırası . Ayrıca bir tane var 2 gün sıralamak sorgu[][] oluşan Q sorgular hangisini içerir üç tam sayıların her biri burada Birinci tamsayı T açıklar tip sorgu ve Sonraki iki tamsayı X Ve Ve tarif et sıra veya kolon olması gerekiyor işletilen . Görev, işlem bunlar Q sorguları manipüle etmek ile birlikte[][]. Her sorgu aşağıdaki üç türden biridir:

gimp rengini değiştir
  • [1xy]: değiştir X bu Ve Ve bu hasır sıraları[][].
  • [2xy]: değiştir X o Ve Ve bu mat sütunları[][].
  • [3xy]: yazdır eleman en X o sıra ve Ve bu mat sütunu[][].

Örnekler: 

Giriş: m = 3 n = 3
sorgu[][] = [[1 0 1]
[3 0 0]
[3 1 0]
[2 0 1]
[3 0 0]
[3 1 0]]



Çıkış: 4 1 5 2

Açıklama: Başlangıçta matris şu şekildedir:
ile[][] = [[1 2 3]
[4 5 6]
[7 8 9]]
İlk Çalıştırma [1 0 1] : 0. satır ile 1. satırın yerini değiştirmemiz gerekiyor.
Bu işlemden sonra matris şu hale gelir:
ile[][] =[[4 5 6]
[1 2 3]
[7 8 9]]
Sonraki iki işlem için, verilen hücrelerdeki elemanları yazdırmamız gerekiyor.
Dördüncü Operasyon [2 0 1]: 0. sütun ile 1. sütunu değiştirmemiz gerekiyor.
Bu işlemden sonra matris şu hale gelir:
ile[][] =[[5 4 6]
[2 1 3]
[8 7 9]]
Sonraki iki işlem için, verilen hücrelerdeki elemanları yazdırmamız gerekiyor.

İçerik Tablosu

[Naif Yaklaşım] - O(q*n) Zaman ve O(m*n) Uzay

Fikir şu ki yaratmak bir matris ile birlikte[][] düzenli m*n ilk olarak dolu tam sayılarla 1 ila m*n sırayla bir satır ana sırası . Sorularınız için tip 1 Ve 2 yani. takas çaprazlamak gerekli satır veya sütunu seçin ve öğelerinin her birini değiştirin. Ve sorgusu için tip 3 yani. baskı öğeyi yalnızca belirtilen dizinde yazdırırız.

C++
// C++ Program to perform queries in a matrix. #include    using namespace std; // function to swap rows of the matrix. void swapRows(vector<vector<int>> &mat int r1 int r2) {  for (int i = 0; i < mat[0].size(); i++) {  swap(mat[r1][i] mat[r2][i]);  } } // function to swap columns of the matrix. void swapCols(vector<vector<int>> &mat int c1 int c2) {  for (int i = 0; i < mat.size(); i++) {  swap(mat[i][c1] mat[i][c2]);  } } // function to operate queries. void solveQueries(int m int n vector<vector<int>> &query) {  // create a matrix or order m*n filled with  // values from 1 to m*n.  vector<vector<int>> mat(m vector<int>(n));  for (int i = 0; i < m; i++) {  for (int j = 0; j < n; j++) {  mat[i][j] = (i * n) + j + 1;  }  }  // perform the queries on the matrix.  for (int i = 0; i < query.size(); i++) {  // if query is of type 1  // swap the rows.  if (query[i][0] == 1) {  swapRows(mat query[i][1] query[i][2]);  }  // if query is of type 2  // swap the columns.  else if (query[i][0] == 2) {  swapCols(mat query[i][1] query[i][2]);  }  // if query is of type 3  // print the value at the given index.  else {  cout << mat[query[i][1]][query[i][2]] << ' ';  }  } } int main() {  int m = 3 n = 3;  vector<vector<int>> query = {{1 0 1}  {3 0 0}  {3 1 0}  {2 0 1}  {3 0 0}  {3 1 0}};  solveQueries(m n query);  return 0; } 
Java
// Java Program to perform queries in a matrix. import java.util.*; class GfG {  // function to swap rows of the matrix.  static void swapRows(int[][] mat int r1 int r2) {  for (int i = 0; i < mat[0].length; i++) {  int temp = mat[r1][i];  mat[r1][i] = mat[r2][i];  mat[r2][i] = temp;  }  }  // function to swap columns of the matrix.  static void swapCols(int[][] mat int c1 int c2) {  for (int i = 0; i < mat.length; i++) {  int temp = mat[i][c1];  mat[i][c1] = mat[i][c2];  mat[i][c2] = temp;  }  }  // function to operate queries.  static void solveQueries(int m int n int[][] query) {  // create a matrix or order m*n filled with  // values from 1 to m*n.  int[][] mat = new int[m][n];  for (int i = 0; i < m; i++) {  for (int j = 0; j < n; j++) {  mat[i][j] = (i * n) + j + 1;  }  }  // perform the queries on the matrix.  for (int i = 0; i < query.length; i++) {  // if query is of type 1  // swap the rows.  if (query[i][0] == 1) {  swapRows(mat query[i][1] query[i][2]);  }  // if query is of type 2  // swap the columns.  else if (query[i][0] == 2) {  swapCols(mat query[i][1] query[i][2]);  }  // if query is of type 3  // print the value at the given index.  else {  System.out.print(mat[query[i][1]][query[i][2]] + ' ');  }  }  }  public static void main(String[] args) {  int m = 3 n = 3;  int[][] query = {  {1 0 1}  {3 0 0}  {3 1 0}  {2 0 1}  {3 0 0}  {3 1 0}  };  solveQueries(m n query);  } } 
Python
# Python Program to perform queries in a matrix. # function to swap rows of the matrix. def swapRows(mat r1 r2): mat[r1] mat[r2] = mat[r2] mat[r1] # function to swap columns of the matrix. def swapCols(mat c1 c2): for row in mat: row[c1] row[c2] = row[c2] row[c1] # function to operate queries. def solveQueries(m n query): # create a matrix of order m*n filled with # values from 1 to m*n. mat = [[(i * n) + j + 1 for j in range(n)] for i in range(m)] # perform the queries on the matrix. for q in query: # if query is of type 1 # swap the rows. if q[0] == 1: swapRows(mat q[1] q[2]) # if query is of type 2 # swap the columns. elif q[0] == 2: swapCols(mat q[1] q[2]) # if query is of type 3 # print the value at the given index. else: print(mat[q[1]][q[2]] end=' ') if __name__ == '__main__': m n = 3 3 query = [ [1 0 1] [3 0 0] [3 1 0] [2 0 1] [3 0 0] [3 1 0] ] solveQueries(m n query) 
C#
// C# Program to perform queries in a matrix. using System; class GfG {  // function to swap rows of the matrix.  static void SwapRows(int[] mat int r1 int r2) {  for (int i = 0; i < mat.GetLength(1); i++) {  int temp = mat[r1 i];  mat[r1 i] = mat[r2 i];  mat[r2 i] = temp;  }  }  // function to swap columns of the matrix.  static void SwapCols(int[] mat int c1 int c2) {  for (int i = 0; i < mat.GetLength(0); i++) {  int temp = mat[i c1];  mat[i c1] = mat[i c2];  mat[i c2] = temp;  }  }  // function to operate queries.  static void SolveQueries(int m int n int[][] query) {  // create a matrix or order m*n filled with  // values from 1 to m*n.  int[] mat = new int[m n];  for (int i = 0; i < m; i++) {  for (int j = 0; j < n; j++) {  mat[i j] = (i * n) + j + 1;  }  }  // perform the queries on the matrix.  for (int i = 0; i < query.Length; i++) {  // if query is of type 1  // swap the rows.  if (query[i][0] == 1) {  SwapRows(mat query[i][1] query[i][2]);  }  // if query is of type 2  // swap the columns.  else if (query[i][0] == 2) {  SwapCols(mat query[i][1] query[i][2]);  }  // if query is of type 3  // print the value at the given index.  else {  Console.Write(mat[query[i][1] query[i][2]] + ' ');  }  }  }  static void Main(string[] args) {  int m = 3 n = 3;  int[][] query = {  new int[] { 1 0 1 }  new int[] { 3 0 0 }  new int[] { 3 1 0 }  new int[] { 2 0 1 }  new int[] { 3 0 0 }  new int[] { 3 1 0 }  };  SolveQueries(m n query);  } } 
JavaScript
// JavaScript Program to perform queries in a matrix. // function to swap rows of the matrix. function swapRows(mat r1 r2) {  [mat[r1] mat[r2]] = [mat[r2] mat[r1]]; } // function to swap columns of the matrix. function swapCols(mat c1 c2) {  for (let i = 0; i < mat.length; i++) {  [mat[i][c1] mat[i][c2]] = [mat[i][c2] mat[i][c1]];  } } // function to operate queries. function solveQueries(m n query) {  // create a matrix or order m*n filled with  // values from 1 to m*n.  const mat = Array.from({ length: m } (_ i) =>  Array.from({ length: n } (_ j) => i * n + j + 1)  );  // perform the queries on the matrix.  for (const q of query) {    // if query is of type 1  // swap the rows.  if (q[0] === 1) {  swapRows(mat q[1] q[2]);  }  // if query is of type 2  // swap the columns.  else if (q[0] === 2) {  swapCols(mat q[1] q[2]);  }  // if query is of type 3  // print the value at the given index.  else {  console.log(mat[q[1]][q[2]] + ' ');  }  } } // driver code const m = 3 n = 3; const query = [  [1 0 1]  [3 0 0]  [3 1 0]  [2 0 1]  [3 0 0]  [3 1 0] ]; solveQueries(m n query); 

Çıkış
4 1 5 2 

Zaman Karmaşıklığı: O(q*n) tür 1 ve 2'deki q sorgularını gerçekleştirmek için gereklidir.
Yardımcı Alan: O(m*n) matris oluşturmak için gerekli yardımcı alan ile birlikte[][] m*n düzeyinde.

[Beklenen Yaklaşım] - O(q) Zaman ve O(m + n) Uzay

eleman herhangi bir pozisyonda mat[x][y] matriste şu şekilde tanımlanabilir: mat[x][y] = (n*x) + y + 1 Neresi N sayısı sütunlar . Matrisi doğrudan değiştirmek yerine şunu kullanabiliriz: iki ek diziler satırlar[m] Ve sütunlar[n] . satırlar dizi: başlatıldı gelen değerlerle ile m-1 satırların indekslerini temsil eden ve sütunlar dizi: başlatıldı gelen değerlerle ile n-1 sütunların indekslerini temsil eder.

Bir sorguyu işlemek için tip 1 hangi takaslar satırlar X Ve Ve biz basitçe takas the değerler ile ilgili satırlar[x] Ve satırlar[y]. Benzer şekilde bir sorgu için tip 2 hangi takaslar sütunlar X Ve Ve Biz takas the değerler ile ilgili sütunlar[x] Ve sütunlar[y] . Bir sorgu için tip 3 Hangi baskı the değer pozisyonda (xy) formülü kullanarak konumu hesaplıyoruz mat[x][y] = satırlar[x]*n + sütunlar[y] + 1.

Aşağıda yukarıdaki fikrin uygulanması yer almaktadır:

C++
// C++ Program to perform queries in a matrix. #include    using namespace std; // function to operate queries. void solveQueries(int m int n vector<vector<int>> &query) {  // create two arrays rows and cols  // and fill the value from 0 to size-1  vector<int> rows(m) cols(n);  for(int i = 0; i < m; i++) {  rows[i] = i;  }  for(int i = 0; i < n; i++) {  cols[i] = i;  }  // perform the queries on the matrix.  for (int i = 0; i < query.size(); i++) {  // if query is of type 1  // swap the rows.  if (query[i][0] == 1) {  swap(rows[query[i][1]] rows[query[i][2]]);  }  // if query is of type 2  // swap the columns.  else if (query[i][0] == 2) {  swap(cols[query[i][1]] cols[query[i][2]]);  }  // if query is of type 3  // print the value at the given index.  else {  cout<< (rows[query[i][1]] * n) + cols[query[i][2]] + 1 << ' ';  }  } } int main() {  int m = 3 n = 3;  vector<vector<int>> query = {{1 0 1}  {3 0 0}  {3 1 0}  {2 0 1}  {3 0 0}  {3 1 0}};  solveQueries(m n query);  return 0; } 
Java
// Java Program to perform queries in a matrix. import java.util.*; class GfG {  // function to operate queries.  static void solveQueries(int m int n int[][] query) {  // create two arrays rows and cols  // and fill the value from 0 to size-1  int[] rows = new int[m];  int[] cols = new int[n];  for (int i = 0; i < m; i++) {  rows[i] = i;  }  for (int i = 0; i < n; i++) {  cols[i] = i;  }  // perform the queries on the matrix.  for (int i = 0; i < query.length; i++) {  // if query is of type 1  // swap the rows.  if (query[i][0] == 1) {  int temp = rows[query[i][1]];   rows[query[i][1]] = rows[query[i][2]];  rows[query[i][2]] = temp;  }  // if query is of type 2  // swap the columns.  else if (query[i][0] == 2) {  int temp = cols[query[i][1]];  cols[query[i][1]] = cols[query[i][2]];  cols[query[i][2]] = temp;  }  // if query is of type 3  // print the value at the given index.  else {  System.out.print((rows[query[i][1]]*n +   cols[query[i][2]] + 1) + ' ');  }  }  }  public static void main(String[] args) {  int m = 3 n = 3;  int[][] query = {  {1 0 1}  {3 0 0}  {3 1 0}  {2 0 1}  {3 0 0}  {3 1 0}  };  solveQueries(m n query);  } } 
Python
# Python Program to perform queries in a matrix. # function to operate queries. def solveQueries(m n query): # create two arrays rows and cols # and fill the value from 0 to size-1 rows = [i for i in range(m)] cols = [i for i in range(n)] # perform the queries on the matrix. for q in query: # if query is of type 1 # swap the rows. if q[0] == 1: rows[q[1]] rows[q[2]] = rows[q[2]] rows[q[1]] # if query is of type 2 # swap the columns. elif q[0] == 2: cols[q[1]] cols[q[2]] = cols[q[2]] cols[q[1]] # if query is of type 3 # print the value at the given index. else: print((rows[q[1]] * n) + cols[q[2]] + 1 end=' ') if __name__ == '__main__': m n = 3 3 query = [ [1 0 1] [3 0 0] [3 1 0] [2 0 1] [3 0 0] [3 1 0] ] solveQueries(m n query) 
C#
// C# Program to perform queries in a matrix. using System; class GfG {  // function to operate queries.  static void SolveQueries(int m int n int[][] query) {  // create two arrays rows and cols  // and fill the value from 0 to size-1  int[] rows = new int[m];  int[] cols = new int[n];  for(int i = 0; i < m; i++) {  rows[i] = i;  }  for(int i = 0; i < n; i++) {  cols[i] = i;  }  // perform the queries on the matrix.  for (int i = 0; i < query.Length; i++) {  // if query is of type 1  // swap the rows.  if (query[i][0] == 1) {  int temp = rows[query[i][1]];   rows[query[i][1]] = rows[query[i][2]];  rows[query[i][2]] = temp;  }  // if query is of type 2  // swap the columns.  else if (query[i][0] == 2) {  int temp = cols[query[i][1]];  cols[query[i][1]] = cols[query[i][2]];  cols[query[i][2]] = temp;  }  // if query is of type 3  // print the value at the given index.  else {  Console.Write((rows[query[i][1]]*n + cols[query[i][2]] + 1) + ' ');  }  }  }  static void Main(string[] args) {  int m = 3 n = 3;  int[][] query = {  new int[] { 1 0 1 }  new int[] { 3 0 0 }  new int[] { 3 1 0 }  new int[] { 2 0 1 }  new int[] { 3 0 0 }  new int[] { 3 1 0 }  };  SolveQueries(m n query);  } } 
JavaScript
// JavaScript Program to perform queries in a matrix. // function to operate queries. function solveQueries(m n query) {  // create two arrays rows and cols  // and fill the value from 0 to size-1  let rows = new Array(m);  let cols = new Array(n);  for (let i = 0; i < m; i++) {  rows[i] = i;  }  for (let i = 0; i < n; i++) {  cols[i] = i;  }  // perform the queries on the matrix.  for (const q of query) {    // if query is of type 1  // swap the rows.  if (q[0] === 1) {  [rows[q[1]] rows[q[2]]] = [rows[q[2]] rows[q[1]]];  }  // if query is of type 2  // swap the columns.  else if (q[0] === 2) {  [cols[q[1]] cols[q[2]]] = [cols[q[2]] cols[q[1]]];  }  // if query is of type 3  // print the value at the given index.  else {  process.stdout.write(((rows[q[1]] * n) + cols[q[2]] + 1) + ' ');  }  } } const m = 3 n = 3; const query = [  [1 0 1]  [3 0 0]  [3 1 0]  [2 0 1]  [3 0 0]  [3 1 0] ]; solveQueries(m n query); 

Çıkış
4 1 5 2 

Zaman Karmaşıklığı: O(q) q = sorgu sayısı 
Yardımcı Alan: İki dizi oluşturmak için O(m + n) yardımcı alanı gerekir.

tarihe dize