Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace t11
- {
- class Program
- {
- static void Main()
- {
- string[] input = Console.ReadLine().Split();
- int rows = int.Parse(input[0]);
- int columns = int.Parse(input[1]);
- byte[,] matrix = new byte[rows, columns]; // matrix filled with 0
- // 0 for empty, 1 for occupied
- string command = "";
- while ((command = Console.ReadLine()) != "stop")
- {
- int[] data = command.Split(' ').Select(int.Parse).ToArray();
- int entrance = data[0];
- int row = data[1];
- int col = data[2];
- int steps = Math.Abs(entrance - row) + 1; // initial steps in first (0) column
- if (IsParked(matrix, row, col))
- {
- steps += col; // add steps in the row to the initial steps
- Console.WriteLine(steps);
- }
- else // lower -- v v v v v v v v v --upper
- { // row-->|0|_|_|_|_|X|_|_|_|_|_|
- int lowerCounter = col - 1; // the cells below desired cell without(0)
- int upperCounter = columns - col - 1; // the cells after desired cell
- while (true)
- {
- if (lowerCounter > 0) // on every loop check lower cells
- {
- col = lowerCounter;
- if (IsParked(matrix, row, col))
- {
- steps += col;
- Console.WriteLine(steps);
- break;
- }
- lowerCounter--;
- }
- if (upperCounter > 0) // on every loop check upper cells
- {
- col = columns - upperCounter; // count of matrix columns - upperCounter
- if (IsParked(matrix, row, col))
- {
- steps += col;
- Console.WriteLine(steps);
- break;
- }
- upperCounter--;
- }
- if (lowerCounter == 0 && upperCounter == 0) // look for unchecked cells
- {
- Console.WriteLine($"Row {row} full");
- break;
- }
- }
- }
- }
- }
- static bool IsParked(byte[,] matrix, int row, int col)
- {
- if (matrix[row, col] == 0)
- {
- matrix[row, col] = 1;
- return true;
- }
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment