Guest User

Untitled

a guest
Feb 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. private int[] IterateOnSpiral(int[,] matrix)
  2. {
  3. var pairs = this.GeneratePairsCollection(matrix.GetLength(0), matrix.GetLength(1));
  4. var direction = Directions.Right;
  5. int i = 0, j = 0, resultsIter = 0;
  6. var result = new int[matrix.Length];
  7.  
  8. while (pairs.Any())
  9. {
  10. result[resultIter++] = matrix[i, j];
  11. pairs.Remove(new Tuple<int, int>(i, j));
  12.  
  13. if (direction == Directions.Right)
  14. {
  15. if (pairs.Contains(new Tuple<int, int>(i, j + 1)))
  16. {
  17. j++;
  18. }
  19. else
  20. {
  21. directions = Directions.Down;
  22. }
  23. }
  24. }
  25. }
  26.  
  27. private ISet<Tuple<int, int>> GeneratePairsCollection(int m, int n)
  28. {
  29. var result = new HashSet<Tuple<int, int>>();
  30.  
  31. for (var i = 0; i < matrix.GetLength(0); i++)
  32. {
  33. for (var j = 0; j < matrix.GetLength(1); j++)
  34. {
  35. result.Add(new Tuple<int, int>(i, j));
  36. }
  37. }
  38.  
  39. return result;
  40. }
  41.  
  42. private enum Directions
  43. {
  44. Right, Left, Up, Down
  45. }
  46. }
Add Comment
Please, Sign In to add comment