fork download
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Collections;
  5.  
  6. public class Test
  7. {
  8. public static void Main()
  9. {
  10. int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, 12, 14, 13, 10, 11 };
  11.  
  12. Dictionary<int, int> layers = new Dictionary<int, int>();
  13.  
  14. int n = arr.Length,
  15. nodesInRow = 1,
  16. currentNodeInRow = -1,
  17. rowCenter,
  18. currentNodeLayer = 0,
  19. maxLayer = -1;
  20.  
  21. for (int i = 0; i < n; i++)
  22. {
  23. if (currentNodeInRow == nodesInRow - 1)
  24. {
  25. nodesInRow *= 2;
  26. currentNodeInRow = 0;
  27. } else currentNodeInRow++;
  28.  
  29. if (i == 0) {
  30. layers.Add(0, 0); // Special case
  31. continue;
  32. }
  33.  
  34. rowCenter = nodesInRow / 2 - 1;
  35.  
  36. if (currentNodeInRow <= rowCenter)
  37. currentNodeLayer = currentNodeInRow;
  38. else
  39. currentNodeLayer = nodesInRow - currentNodeInRow - 1;
  40.  
  41. if (currentNodeLayer > maxLayer) maxLayer = currentNodeLayer;
  42. layers.Add(i, currentNodeLayer);
  43.  
  44. // Console.WriteLine("{0} => {1}", i, currentNodeLayer);
  45. }
  46.  
  47.  
  48. for (int i = 0; i <= maxLayer; i++)
  49. {
  50. Console.Write("Layer {0}:", i + 1);
  51. foreach (var x in layers.Where((p) => p.Value == i))
  52. if (arr[x.Key] != -1) Console.Write("{0} ", arr[x.Key]);
  53. Console.WriteLine();
  54. }
  55. }
  56. }
Success #stdin #stdout 0.05s 24376KB
stdin
Standard input is empty
stdout
Layer 1:1 2 3 4 7 8 11 
Layer 2:5 6 9 10 
Layer 3:13 
Layer 4:12 14