4. Matrix Shuffling - 80 т.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
namespace Basic_Stack_Operations
{
class Program
{
public static object Matrix { get; private set; }
static void Main(string[] args)
{
int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
string[,] matrix = ReadMatrix(input[0], input[1]);
string text;
while ((text = Console.ReadLine()) != "END")
{
string patern = @"swap (\d+) (\d+) (\d+) (\d+)";
Match element = Regex.Match(text, patern);
if (element.Success)
{
int row1 = int.Parse(element.Groups[1].Value);
int col1 = int.Parse(element.Groups[2].Value);
int row2 = int.Parse(element.Groups[3].Value);
int col2 = int.Parse(element.Groups[4].Value);
if (row1 < matrix.GetLength(0)
&&col1 < matrix.GetLength(1)
&&row2 < matrix.GetLength(0)
&&col2 < matrix.GetLength(1))
{
string newElement = matrix[row1, col1];
matrix[row1, col1] = matrix[row2, col2];
matrix[row2, col2] = newElement;
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}
}
else
{
Console.WriteLine("Invalid input!");
}
}
else
{
Console.WriteLine("Invalid input!");
}
}
static string[,] ReadMatrix(int row, int col)
{
string[,] matrix = new string[row, col];
for (int i = 0; i < matrix.GetLength(0); i++)
{
string[] array = Console.ReadLine().Split(' ',StringSplitOptions.RemoveEmptyEntries).ToArray();
for (int j = 0; j < matrix.GetLength(1); j++)
{
matrix[i, j] = array[j];
}
}
return matrix;
}
}
}
}