Advertisement
Guest User

Untitled

a guest
Oct 4th, 2023
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace Zad._4
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int[] sizes = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  12.             string[,] matrix = new string[sizes[0], sizes[1]];
  13.             for (int row = 0; row < matrix.GetLength(0); row++)
  14.             {
  15.                 string[] rowValues = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);
  16.                 for (int col = 0; col < matrix.GetLength(1); col++)
  17.                 {
  18.                     matrix[row, col] = rowValues[col];
  19.                 }
  20.             }
  21.             string input = Console.ReadLine();
  22.             while (input!="END")
  23.             {
  24.                 string[] commandInfo = input.Split(" ", StringSplitOptions.RemoveEmptyEntries);
  25.                 if (commandInfo.Length != 5)
  26.                 {
  27.                     Console.WriteLine("Invalid input!");
  28.                     input = Console.ReadLine();
  29.                     continue;
  30.                 }
  31.                 string swap = commandInfo[0];
  32.                 int rowOne = int.Parse(commandInfo[1]);
  33.                 int colOne = int.Parse(commandInfo[2]);
  34.                 int rowTwo = int.Parse(commandInfo[3]);
  35.                 int colTwo = int.Parse(commandInfo[4]);
  36.                 //int length = commandInfo.Skip(1).Count();
  37.                 if (swap != "swap" || commandInfo.Length!=5
  38.                     || rowOne >= matrix.GetLength(0)
  39.                     || rowTwo >= matrix.GetLength(0)
  40.                     || colOne >= matrix.GetLength(1)
  41.                     || colTwo >= matrix.GetLength(1)
  42.                     || rowOne < 0 || rowTwo < 0
  43.                     || colOne < 0 || colTwo < 0)
  44.                 {
  45.                     Console.WriteLine("Invalid input!");
  46.                 }
  47.                 else
  48.                 {
  49.                     string firstParameter = matrix[rowOne, colOne];
  50.                     string secondParameter = matrix[rowTwo, colTwo];
  51.                     matrix[rowOne, colOne] = secondParameter;
  52.                     matrix[rowTwo, colTwo] = firstParameter;
  53.                     for (int row = 0; row < matrix.GetLength(0); row++)
  54.                     {
  55.                         for (int col = 0; col < matrix.GetLength(1); col++)
  56.                         {
  57.                             Console.Write(matrix[row, col] + " ");
  58.                         }
  59.                         Console.WriteLine();
  60.                     }
  61.                 }
  62.                 input = Console.ReadLine();
  63.             }
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement