Advertisement
Gesh4o

BitsAtCrossroads

Oct 12th, 2015
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class BitsAtCrossroads
  5. {
  6. static int fieldSize = int.Parse(Console.ReadLine());
  7.  
  8. static int[,] matrix = new int[fieldSize, fieldSize];
  9. static List<string> binaryNumbers = new List<string>();
  10. static int crossroadsCount = 0;
  11.  
  12. static void Main()
  13. {
  14.  
  15. string command = Console.ReadLine();
  16.  
  17. while (command != "end")
  18. {
  19. string[] inputCoordinates = command.Split(' ');
  20.  
  21. int rowCoordinate = int.Parse(inputCoordinates[0]);
  22.  
  23. int colCoordinate = int.Parse(inputCoordinates[1]);
  24.  
  25. PrintRightDiagonal(rowCoordinate, colCoordinate);
  26.  
  27. PrintLeftDiagonal(rowCoordinate, colCoordinate);
  28.  
  29. command = Console.ReadLine();
  30. }
  31. ConvertToBinary();
  32.  
  33. ConvertToDecimalAndPrintResult();
  34.  
  35. }
  36. static void PrintRightDiagonal(int x, int y)
  37. {
  38. //Upper side
  39. int col = fieldSize - y - 1;
  40. for (int row = x ; row >= 0 && col >= 0; row--)
  41. {
  42. if (matrix[row,col] ==1)
  43. {
  44. crossroadsCount++;
  45. }
  46. matrix[row, col] = 1;
  47. col--;
  48. }
  49.  
  50. //Bottom side
  51. col = fieldSize - y ;
  52. for (int row = x + 1; row <= fieldSize -1 && col <= fieldSize -1; row++)
  53. {
  54. if (matrix[row, col] == 1)
  55. {
  56. crossroadsCount++;
  57. }
  58. matrix[row, col] = 1;
  59. col++;
  60. }
  61. }
  62.  
  63. static void PrintLeftDiagonal(int x, int y)
  64. {
  65. //Upper side
  66. int col = fieldSize - y - 1;
  67. for (int row = x ; row >= 0 && col <= fieldSize-1 ; row-- )
  68. {
  69. if (matrix[row, col] == 1)
  70. {
  71. crossroadsCount++;
  72. }
  73. matrix[row, col] = 1;
  74. col++;
  75. }
  76.  
  77. //Bottom side
  78. col = fieldSize - y - 2 ;
  79. for (int row = x +1 ; row <= fieldSize -1 && col >= 0; row++)
  80. {
  81. if (matrix[row, col] == 1)
  82. {
  83. crossroadsCount++;
  84. }
  85. matrix[row, col] = 1;
  86. col--;
  87. }
  88. }
  89.  
  90. static void ConvertToBinary()
  91. {
  92.  
  93. for (int rows = 0; rows < fieldSize; rows++)
  94. {
  95. string binaryNumber = "";
  96. for (int cols = 0; cols < fieldSize; cols++)
  97. {
  98. binaryNumber += matrix[rows, cols];
  99. }
  100. binaryNumbers.Add(binaryNumber);
  101. }
  102. }
  103.  
  104. static void ConvertToDecimalAndPrintResult()
  105. {
  106. int[] decimalNumbers = new int[fieldSize];
  107. for (int i = 0; i < binaryNumbers.Count; i++)
  108. {
  109. decimalNumbers[i] = Convert.ToInt32(binaryNumbers[i], 2);
  110. }
  111.  
  112. for (int d = 0; d < decimalNumbers.Length; d++)
  113. {
  114. Console.WriteLine(decimalNumbers[d]);
  115. }
  116. Console.WriteLine(crossroadsCount);
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement