Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.IO;
  6.  
  7.  
  8. namespace BOunce
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. //initialize
  15. int[] input = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
  16. int rows = input[0];
  17. int cols = input[1];
  18.  
  19. int[,] matrix = new int[rows, cols];
  20.  
  21. //fill matrix
  22.  
  23. for (int row = 0; row < rows; row++)
  24. {
  25. for (int col = 0; col < cols; col++)
  26. {
  27. matrix[row, col] = (int)Math.Pow(2, (col + row));
  28. }
  29.  
  30. }
  31.  
  32. //sum numbers in matrix & bounce
  33. int dirX = 1;
  34. int dirY = 1;
  35. int x = 1;
  36. int y = 1;
  37. int sum = 1;
  38.  
  39. if (rows == 1 || cols == 1)
  40. {
  41. Console.WriteLine(1);
  42.  
  43. }
  44.  
  45. else
  46. {
  47. while (true)
  48. {
  49.  
  50. sum += matrix[x, y];
  51.  
  52. if ((x == rows - 1) && (y == cols - 1)) break;
  53. else if ((x == 0) && (y == 0)) break;
  54. else if ((x == rows - 1) && (y == 0)) break;
  55. else if ((x == 0) && (y == cols - 1)) break;
  56.  
  57. if (x == 0 || x == (rows - 1))
  58. {
  59. dirX *= -1;
  60. }
  61. else if (y == 0 || y == (cols - 1))
  62. {
  63. dirY *= -1;
  64. }
  65.  
  66. x += dirX;
  67. y += dirY;
  68. }
  69. Console.WriteLine(sum);
  70.  
  71. }
  72.  
  73.  
  74. //print matrix
  75. for (int row = 0; row < rows; row++)
  76. {
  77.  
  78. for (int col = 0; col < cols; col++)
  79. {
  80. Console.Write(matrix[row, col] + " ");
  81. }
  82.  
  83.  
  84. Console.WriteLine();
  85. }
  86.  
  87.  
  88.  
  89. }
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement