Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class BitsAtCrossroads
- {
- static int fieldSize = int.Parse(Console.ReadLine());
- static int[,] matrix = new int[fieldSize, fieldSize];
- static List<string> binaryNumbers = new List<string>();
- static int crossroadsCount = 0;
- static void Main()
- {
- string command = Console.ReadLine();
- try
- {
- while (command != "end")
- {
- string[] inputCoordinates = command.Split(' ');
- int rowCoordinate = int.Parse(inputCoordinates[0]);
- int colCoordinate = int.Parse(inputCoordinates[1]);
- PrintRightDiagonal(rowCoordinate, colCoordinate);
- PrintLeftDiagonal(rowCoordinate, colCoordinate);
- command = Console.ReadLine();
- }
- ConvertToBinary();
- ConvertToDecimalAndPrintResult();
- }
- catch (Exception)
- {
- throw;
- }
- }
- static void PrintRightDiagonal(int x, int y)
- {
- //Upper side
- int col = fieldSize - y - 1;
- try
- {
- for (int row = x; row >= 0 && col >= 0; row--)
- {
- if (matrix[row, col] == 1)
- {
- crossroadsCount++;
- }
- matrix[row, col] = 1;
- col--;
- }
- }
- catch (Exception)
- {
- throw;
- }
- //Bottom side
- col = fieldSize - y ;
- try
- {
- for (int row = x + 1; row <= fieldSize - 1 && col <= fieldSize - 1; row++)
- {
- if (matrix[row, col] == 1)
- {
- crossroadsCount++;
- }
- matrix[row, col] = 1;
- col++;
- }
- }
- catch (Exception)
- {
- throw;
- }
- }
- static void PrintLeftDiagonal(int x, int y)
- {
- //Upper side
- int col = fieldSize - y - 1;
- try
- {
- for (int row = x; row >= 0 && col <= fieldSize - 1; row--)
- {
- if (matrix[row, col] == 1)
- {
- crossroadsCount++;
- }
- matrix[row, col] = 1;
- col++;
- }
- }
- catch (Exception)
- {
- throw;
- }
- //Bottom side
- col = fieldSize - y - 2 ;
- try
- {
- for (int row = x + 1; row <= fieldSize - 1 && col >= 0; row++)
- {
- if (matrix[row, col] == 1)
- {
- crossroadsCount++;
- }
- matrix[row, col] = 1;
- col--;
- }
- }
- catch (Exception)
- {
- throw;
- }
- }
- static void ConvertToBinary()
- {
- for (int rows = 0; rows < fieldSize; rows++)
- {
- string binaryNumber = "";
- for (int cols = 0; cols < fieldSize; cols++)
- {
- binaryNumber += matrix[rows, cols];
- }
- binaryNumbers.Add(binaryNumber);
- }
- }
- static void ConvertToDecimalAndPrintResult()
- {
- uint[] decimalNumbers = new uint[fieldSize];
- try
- {
- for (int i = 0; i < binaryNumbers.Count; i++)
- {
- decimalNumbers[i] = Convert.ToUInt32(binaryNumbers[i], 2);
- }
- }
- catch (Exception)
- {
- throw;
- }
- try
- {
- for (int d = 0; d < decimalNumbers.Length; d++)
- {
- Console.WriteLine(decimalNumbers[d]);
- }
- Console.WriteLine(crossroadsCount);
- }
- catch (Exception)
- {
- throw;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement