Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private int[] IterateOnSpiral(int[,] matrix)
- {
- var pairs = this.GeneratePairsCollection(matrix.GetLength(0), matrix.GetLength(1));
- var direction = Directions.Right;
- int i = 0, j = 0, resultsIter = 0;
- var result = new int[matrix.Length];
- while (pairs.Any())
- {
- result[resultIter++] = matrix[i, j];
- pairs.Remove(new Tuple<int, int>(i, j));
- if (direction == Directions.Right)
- {
- if (pairs.Contains(new Tuple<int, int>(i, j + 1)))
- {
- j++;
- }
- else
- {
- directions = Directions.Down;
- }
- }
- }
- }
- private ISet<Tuple<int, int>> GeneratePairsCollection(int m, int n)
- {
- var result = new HashSet<Tuple<int, int>>();
- for (var i = 0; i < matrix.GetLength(0); i++)
- {
- for (var j = 0; j < matrix.GetLength(1); j++)
- {
- result.Add(new Tuple<int, int>(i, j));
- }
- }
- return result;
- }
- private enum Directions
- {
- Right, Left, Up, Down
- }
- }
Add Comment
Please, Sign In to add comment