Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.39 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _4._Matrix_shuffling
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[] sizes = Console.ReadLine()
  11.                 .Split()
  12.                 .Select(int.Parse)
  13.                 .ToArray();
  14.  
  15.             string[][] matrix = new string[sizes[0]][];
  16.  
  17.             for (int i = 0; i < sizes[0]; i++)
  18.             {
  19.                 matrix[i] = new string[sizes[1]];
  20.                 matrix[i] = Console.ReadLine()
  21.                     .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  22.             }
  23.  
  24.             while (true)
  25.             {
  26.                 string line = Console.ReadLine();
  27.                 if (line == "END")
  28.                 {
  29.                     break;
  30.                 }
  31.  
  32.                 string[] tokens = line
  33.                         .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  34.                         .ToArray();
  35.  
  36.  
  37.                 if (tokens[0] != "swap" || tokens.Length != 5)
  38.                 {
  39.                     Console.WriteLine("Invalid input!");
  40.                     continue;
  41.                 }
  42.  
  43.                 int r1 = int.Parse(tokens[1]);
  44.                 int c1 = int.Parse(tokens[2]);
  45.                 int r2 = int.Parse(tokens[3]);
  46.                 int c2 = int.Parse(tokens[4]);
  47.  
  48.                 if (r1 >= matrix.Length
  49.                     || c1 >= sizes[1]
  50.                     || r2 >= matrix.Length
  51.                     || c2 >= sizes[1])
  52.                 {
  53.                     Console.WriteLine("Invalid input!");
  54.                 }
  55.                 else
  56.                 {
  57.                     string temporary = matrix[r2][c2];
  58.                     matrix[r2][c2] = matrix[r1][c1];
  59.                     matrix[r1][c1] = temporary;
  60.  
  61.                     for (int i = 0; i < sizes[0]; i++)
  62.                     {
  63.                         Console.WriteLine(string.Join(' ', matrix[i]));
  64.                     }
  65.                 }
  66.             }
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement