Advertisement
Gesh4o

BitsAtCrossroads

Oct 12th, 2015
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 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.  
  10. static List<string> binaryNumbers = new List<string>();
  11.  
  12. static int crossroadsCount = 0;
  13.  
  14. static void Main()
  15. {
  16.  
  17. string command = Console.ReadLine();
  18. try
  19. {
  20. while (command != "end")
  21. {
  22. string[] inputCoordinates = command.Split(' ');
  23.  
  24. int rowCoordinate = int.Parse(inputCoordinates[0]);
  25.  
  26. int colCoordinate = int.Parse(inputCoordinates[1]);
  27.  
  28. PrintRightDiagonal(rowCoordinate, colCoordinate);
  29.  
  30. PrintLeftDiagonal(rowCoordinate, colCoordinate);
  31.  
  32. command = Console.ReadLine();
  33. }
  34. ConvertToBinary();
  35.  
  36. ConvertToDecimalAndPrintResult();
  37. }
  38. catch (Exception)
  39. {
  40.  
  41. throw;
  42. }
  43.  
  44.  
  45. }
  46. static void PrintRightDiagonal(int x, int y)
  47. {
  48. //Upper side
  49. int col = fieldSize - y - 1;
  50. try
  51. {
  52. for (int row = x; row >= 0 && col >= 0; row--)
  53. {
  54. if (matrix[row, col] == 1)
  55. {
  56. crossroadsCount++;
  57. }
  58. matrix[row, col] = 1;
  59. col--;
  60. }
  61. }
  62. catch (Exception)
  63. {
  64.  
  65. throw;
  66. }
  67.  
  68. //Bottom side
  69. col = fieldSize - y ;
  70. try
  71. {
  72. for (int row = x + 1; row <= fieldSize - 1 && col <= fieldSize - 1; row++)
  73. {
  74. if (matrix[row, col] == 1)
  75. {
  76. crossroadsCount++;
  77. }
  78. matrix[row, col] = 1;
  79. col++;
  80. }
  81. }
  82. catch (Exception)
  83. {
  84.  
  85. throw;
  86. }
  87. }
  88.  
  89. static void PrintLeftDiagonal(int x, int y)
  90. {
  91. //Upper side
  92. int col = fieldSize - y - 1;
  93. try
  94. {
  95. for (int row = x; row >= 0 && col <= fieldSize - 1; row--)
  96. {
  97. if (matrix[row, col] == 1)
  98. {
  99. crossroadsCount++;
  100. }
  101. matrix[row, col] = 1;
  102. col++;
  103. }
  104. }
  105. catch (Exception)
  106. {
  107.  
  108. throw;
  109. }
  110.  
  111. //Bottom side
  112. col = fieldSize - y - 2 ;
  113. try
  114. {
  115. for (int row = x + 1; row <= fieldSize - 1 && col >= 0; row++)
  116. {
  117. if (matrix[row, col] == 1)
  118. {
  119. crossroadsCount++;
  120. }
  121. matrix[row, col] = 1;
  122. col--;
  123. }
  124. }
  125. catch (Exception)
  126. {
  127.  
  128. throw;
  129. }
  130.  
  131. }
  132.  
  133. static void ConvertToBinary()
  134. {
  135.  
  136. for (int rows = 0; rows < fieldSize; rows++)
  137. {
  138. string binaryNumber = "";
  139. for (int cols = 0; cols < fieldSize; cols++)
  140. {
  141. binaryNumber += matrix[rows, cols];
  142. }
  143. binaryNumbers.Add(binaryNumber);
  144. }
  145. }
  146.  
  147. static void ConvertToDecimalAndPrintResult()
  148. {
  149. uint[] decimalNumbers = new uint[fieldSize];
  150. try
  151. {
  152. for (int i = 0; i < binaryNumbers.Count; i++)
  153. {
  154. decimalNumbers[i] = Convert.ToUInt32(binaryNumbers[i], 2);
  155. }
  156. }
  157. catch (Exception)
  158. {
  159.  
  160. throw;
  161. }
  162.  
  163. try
  164. {
  165. for (int d = 0; d < decimalNumbers.Length; d++)
  166. {
  167. Console.WriteLine(decimalNumbers[d]);
  168. }
  169. Console.WriteLine(crossroadsCount);
  170. }
  171. catch (Exception)
  172. {
  173.  
  174. throw;
  175. }
  176. }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement