✅💪🔥 Simulate Spiral movements || Simple python solution || Beginner friendly || Easy explanation
Intuition\nSimulate the spiral movements and move like that and collect all the indexes visited.\n\n\n# Explanation of logic \n\n\n\n- Suppose R is going right,
# Intuition\nSimulate the spiral movements and move like that and collect all the indexes visited.\n\n\n# Explanation of logic \n\n![image.png](https://assets.leetcode.com/users/images/7054110d-d118-44eb-b4e9-c35405d9d189_1723089716.9857733.png)\n\n- Suppose R is going right, D is going down, L is going left and U is going up . So , we can clearly see that the spiral movements are RDLU but the number of occurances are 1,1,2,2,3,3,4,4,5,5 and so on...\n- So R1, D1, L2, U2, R3, D3, L4, U4 and so on. \n(R1 notifies going right once)\n\n# Approach\n\n- Create a list `var[]` for collecting the traversal data and set the changes of x cordinates and y cordinates as `x=0` and `y=1` (**Because we want the first movement to be right**) and we set the variable `n` as 0. n is the variable which ould help us generate the sequence. \n```\n    res = []\n    x, y, n = 0, 1, 0\n```\n\n\n- We want to traverse the whole 2-D array , so we would traverse untill the size or length of the traverse array `res` is equal to the 2-D array .\n\n```\n    while len(res) < rows * cols:\n```\n\n- We would generate the sequnece and run for the number of times. The explanation of how this code generates our sequence would be given below the appraoch under the title **Other Explanations**.\n\n```\n        for i in xrange(n / 2 + 1):\n```\n\n- If the present traversing indices are within the range of the 2-D array, then append it into the traversing array `res[]`. And also do the necessary changes into the indexes to simulate the movements. And outside the for loop, make the necessary changes to x , y to change the directions . the explanation on how the directions are changing would be given under the **Other Explanations** title. \n\n```\n            if 0 <= rStart < rows and 0 <= cStart < cols:\n                res.append([rStart, cStart])\n            rStart, cStart = rStart + x, cStart + y\n        x, y, n = y, -x, n + 1\n```\n\n\n- Finally, return the traversed list `res`.\n```\n    return res\n```\n\n\n# Other Explanations :\n- **Sequence Generation** : \n\n```\nn  = 0\nfor i in xrange(n / 2 + 1)\nn = n + 1 \n```\nFirst n is 0, so 0/2 + 1 = 1 so the loop would run 1 time. Now the n is updated to 1, so 1/2 +1 = 1 so the loop would run 1 time again. Now the n is updated to 2, so 2/2+1 = 2 so the loop would run 2 times. Now the n is updated to 3, so 3/2+1 = 2 so the loop would run 2 times again . So as we go on , the sequence `1,1,2,2,3,3,4,4,5,5...` would be generated. \n\n\n- **Spiral Simulation** : \n\n```\nx, y= 0, 1\n"""inside the loop""" rStart, cStart = rStart + x, cStart + y\n"""outside the loop""" x,y = y,-x\n```\nSo, first the start coordinates are appended into list. The initial change values are x=0 and y=1 which basically means moving right, so the position moves right the number of times the loop runs according to the generated sequence. Suppose we started with the index (1,4) and we now reached (1,5). Now the changes are updated outside the loop as y = -x and x = y, so the change becomes (1,0) and the index now goes to (2,5) which basically means it goes down. Next the change becomes (0,-1) and the index becomes (2,4) which means basically left. Now change becomes (-1,0) and the index becomes (1,4) which means it basically means going up. These movements combined to the sequence generated follow givves the total spiral simulation. \n\n\n# Complexity\n- Time complexity:\n`O(rows*cols)`\n\n- Space complexity:\n`O(rows*cols)`\n\n\n# Code\n\n```python []\nclass Solution(object):\n    def spiralMatrixIII(self, rows, cols, rStart, cStart):\n        res = []\n        x, y, n = 0, 1, 0\n        while len(res) < rows * cols:\n            for i in xrange(n / 2 + 1):\n                if 0 <= rStart < rows and 0 <= cStart < cols:\n                    res.append([rStart, cStart])\n                rStart, cStart = rStart + x, cStart + y\n            x, y, n = y, -x, n + 1\n        return res\n        \n```\n\n![catupvote.jpeg](https://assets.leetcode.com/users/images/9fdfd95f-d159-43a4-9184-015afa9ecc1e_1723092574.865487.jpeg)\n
['Python']