Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace _04
- {
- class Program
- {
- public static string[,] matrix;
- public static bool inside;
- static void Main(string[] args)
- {
- var size = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
- var rows = size[0];
- var cols = size[1];
- matrix = new string[rows, cols];
- matrixRead(rows, cols);
- inside = false;
- while (true)
- {
- var command = Console.ReadLine();
- var cmd = command.Split(" ",StringSplitOptions.RemoveEmptyEntries);
- if (cmd[0] == "END")
- {
- break;
- }
- if(cmd[0].StartsWith("swap") && cmd.Length == 5)
- {
- var currentCordinatesRow = int.Parse(cmd[1]);
- var currentCordinatesCol = int.Parse(cmd[2]);
- var cordinatesToSwapRow = int.Parse(cmd[3]);
- var cordinatesToSwapCol = int.Parse(cmd[4]);
- IsInside(currentCordinatesRow, currentCordinatesCol, cordinatesToSwapRow, cordinatesToSwapCol,matrix);
- if(inside)
- {
- var currentNum = matrix[currentCordinatesRow, currentCordinatesCol];
- matrix[currentCordinatesRow, currentCordinatesCol] = matrix[cordinatesToSwapRow, cordinatesToSwapCol];
- matrix[cordinatesToSwapRow, cordinatesToSwapCol] = currentNum;
- PrintMatrix(matrix);
- }
- else
- {
- Console.WriteLine("Invalid input!");
- }
- }
- else
- {
- Console.WriteLine("Invalid input!");
- }
- }
- }
- private static void PrintMatrix(string[,] matrix)
- {
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- Console.Write(matrix[row, col] + " ");
- }
- Console.WriteLine();
- }
- }
- private static void matrixRead(int rows, int cols)
- {
- for (int row = 0; row < matrix.GetLength(0) ; row++)
- {
- var currentRow = Console.ReadLine().Split(" ",StringSplitOptions.RemoveEmptyEntries);
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- matrix[row, col] = currentRow[col];
- }
- }
- }
- private static void IsInside(int currentCordinatesRow, int currentCordinatesCol, int cordinatesToSwapRow, int cordinatesToSwapCol, string[,] matrix)
- {
- var matrixCol = matrix.GetLength(1);
- var matrixRow = matrix.GetLength(0);
- if(currentCordinatesRow <= matrixRow && currentCordinatesCol <= matrixCol && cordinatesToSwapRow <= matrixRow && cordinatesToSwapCol <= matrixCol)
- {
- inside = true;
- }
- else
- {
- inside = false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement