python | Easy Simulation | Iterative approach | Notes
\n/*\ninitialize val to 1\ninitialize answer set with starting index\nAlgo\nGo in infinite loop\n   Go right val times, if the index is valid, add all those ind
```\n/*\ninitialize val to 1\ninitialize answer set with starting index\nAlgo\nGo in infinite loop\n   Go right val times, if the index is valid, add all those indexes to answer set\n   Go Down val times, if the index is valid, add all those indexes to answer set\n   Go Left val+1 times, if the index is valid, add all those indexes to answer set\n   Go Up val + 1 times, if the index is valid, add all those indexes to answer set\n   if the answer set has rows*cols entries, break out of loop and return the answer set\n*/\n```\n```\nclass Solution:\n    def spiralMatrixIII(self, rows: int, cols: int, rStart: int, cStart: int) -> List[List[int]]:\n        ans = [[rStart, cStart]]\n        val = 1\n        i, j = rStart, cStart\n        def is_valid(i, j):\n            if 0 <= i < rows and 0 <= j < cols:\n                return True\n            return False\n        \n        while True:\n            if len(ans) == rows * cols:\n                return ans\n            \n            # go right val times\n            for _ in range(val):\n                j+=1\n                if is_valid(i,j):\n                    ans.append([i,j])\n            # go bottom val times\n            for _ in range(val):\n                i+=1\n                if is_valid(i,j):\n                    ans.append([i,j])\n            # go left val+1 times\n            for _ in range(val+1):\n                j-=1\n                if is_valid(i,j):\n                    ans.append([i,j])\n            # go up val+1 times\n            for _ in range(val+1):\n                i-=1\n                if is_valid(i,j):\n                    ans.append([i,j])\n            val+=2\n```
['Simulation', 'Python']