Advertisement
Guest User

Untitled

a guest
May 31st, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.14 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _05_RubiksMatrix
  5. {
  6.     class Startup
  7.     {
  8.         static void Main()
  9.         {
  10.             var size = Console.ReadLine().Trim().Split(' ').ToArray();
  11.             int rows = int.Parse(size[0]);
  12.             int cols = int.Parse(size[1]);
  13.             int[,] matrix = new int[rows, cols];
  14.             int n = int.Parse(Console.ReadLine().Trim());
  15.  
  16.             int counter = 0;
  17.             for (int i = 0; i < rows; i++)
  18.             {
  19.                 for (int j = 0; j < cols; j++)
  20.                 {
  21.                     counter++;
  22.                     matrix[i, j] = counter;
  23.                 }
  24.             }
  25.  
  26.             for (int command = 0; command < n; command++)
  27.             {
  28.                 var tokens = Console.ReadLine().Trim().Split();
  29.                 int rcn = int.Parse(tokens[0]);
  30.                 string direction = tokens[1].Trim();
  31.                 long moves = int.Parse(tokens[2]);
  32.  
  33.                 if (direction == "left" || direction == "right")
  34.                 {
  35.                     moves %= cols;
  36.                     if (direction == "right") moves = cols - moves;
  37.                     for (int m = 0; m < moves % cols; m++)
  38.                     {
  39.                         for (int i = 0; i < cols - 1; i++)
  40.                         {
  41.                             int temp = matrix[rcn, i];
  42.                             matrix[rcn, i] = matrix[rcn, i + 1];
  43.                             matrix[rcn, i + 1] = temp;
  44.                         }
  45.                     }
  46.                 }
  47.                 else
  48.                 {
  49.                     moves %= rows;
  50.                     if (direction == "down") moves = rows - moves;
  51.                     for (int m = 0; m < moves % rows; m++)
  52.                     {
  53.                         for (int i = 0; i < rows - 1; i++)
  54.                         {
  55.                             int temp = matrix[i, rcn];
  56.                             matrix[i, rcn] = matrix[i + 1, rcn];
  57.                             matrix[i + 1, rcn] = temp;
  58.                         }
  59.                     }
  60.                 }
  61.             }
  62.  
  63.             int[] swaps = new int[rows * cols];
  64.             counter = 0;
  65.             for (int i = 0; i < rows; i++)
  66.             {
  67.                 for (int j = 0; j < cols; j++)
  68.                 {
  69.                     swaps[counter] = matrix[i, j];
  70.                     counter++;
  71.                 }
  72.             }
  73.  
  74.             for (int i = 0; i < rows * cols; i++)
  75.             {
  76.                 if (swaps[i] == i + 1)
  77.                 {
  78.                     Console.WriteLine("No swap required");
  79.                 }
  80.                 else
  81.                 {
  82.                     int index = Array.IndexOf(swaps, i + 1);
  83.                     int r1 = i / cols;
  84.                     int c1 = i % cols;
  85.                     int r2 = index / cols;
  86.                     int c2 = index % cols;
  87.  
  88.                     int temp = swaps[i];
  89.                     swaps[i] = swaps[index];
  90.                     swaps[index] = temp;
  91.  
  92.                     Console.WriteLine($"Swap ({r1}, {c1}) with ({r2}, {c2})");
  93.                 }
  94.             }
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement