Beats 98% || Easy to understand C++ Code || Very Intuitive and Detailed Approach
Intuition\n Describe your first thoughts on how to solve this problem. \nIn Spiral Matrix I and Spiral Matrix II we were enclosing/decreasing the looping area o
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIn Spiral Matrix I and Spiral Matrix II we were enclosing/decreasing the looping area of the matrix but in this we will try to expand/increase the looping area of the matrix.\n\n**1.Spiral Movement:**\nThe traversal starts at the initial position (rStart, cStart) and moves right, down, left, and up in a cyclic manner, expanding the boundaries of the spiral as necessary.\n\n**2.Validity Check:** \nAt each step, before visiting a cell, the code checks if the cell is within the bounds of the matrix and if it has already been visited. This ensures that only valid and unvisited cells are added to the result.\n\n**3.Stopping Condition:**\nThe loop continues until all cells in the matrix have been visited, at which point the traversal is complete.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n**1.Initialization:**\nn and m store the number of rows and columns of the matrix.\nans is a vector that will store the result as a list of cell coordinates.\nvis is a boolean matrix of the same size as the input matrix, used to keep track of visited cells.\n\n**2.Defining Boundaries:**\nThe initial position is (rStart, cStart).\nThe boundaries of the spiral are defined by row, endRow, col, and endCol.\nInitially, row and col represent the starting point, while endRow and endCol represent the next boundary to be expanded as the spiral grows.\n\n**3.Simulating the Spiral:**\nThe spiral traversal is simulated using a while loop that continues indefinitely until all cells are visited.\nFor each direction (right, down, left, up), the loop iterates over the boundary cells:\n1. Right: Moves along the row from col to endCol.\n2. Down: Moves down the column from row to endRow.\n3. Left: Moves along the row from endCol to col.\n4. Up: Moves up the column from endRow to row.\n\nAfter visiting the cells in a given direction, the corresponding boundary is adjusted (row--, endCol++, endRow++, col--) to allow for the next expansion of the spiral.\n\n**4.Termination:**\nThe traversal stops when the number of visited cells equals the total number of cells in the matrix (cnt == n*m).\n\n# Complexity\n- Time complexity: $$O(n*m)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n*m)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int n,m;\n    bool isValid(int i,int j){\n        if(i>=0 && i<n && j>=0 && j<m)\n            return true;\n        return false;\n    }\n    vector<vector<int>> spiralMatrixIII(int rows, int cols, int rStart, int cStart) {\n        n=rows,m=cols;\n        vector<vector<int>> ans;\n        vector<vector<bool>> vis(rows,vector<bool>(cols,false));\n        int row = rStart,endRow = rStart + 1;\n        int col = cStart,endCol = cStart + 1;\n        int cnt = 0;\n        while(1){\n            for(int j=col;j<endCol;j++){\n                if(isValid(row,j) && vis[row][j]==false){\n                    cnt++;\n                    vis[row][j]=true;\n                    ans.push_back({row,j});\n                }\n            }\n            if(cnt == n*m)\n                break;\n            row--;\n            for(int i=row+1;i<endRow;i++){\n                if(isValid(i,endCol) && vis[i][endCol]==false){\n                    cnt++;\n                    vis[i][endCol]=true;\n                    ans.push_back({i,endCol});\n                }\n            }\n            if(cnt == n*m)\n                break;\n            endCol++;\n            for(int j=endCol-1;j>=col-1;j--){\n                if(isValid(endRow,j) && vis[endRow][j]==false){\n                    cnt++;\n                    vis[endRow][j]=true;\n                    ans.push_back({endRow,j});\n                }\n            }\n            if(cnt == n*m)\n                break;\n            endRow++;\n            for(int i=endRow-1;i>=row;i--){\n                if(isValid(i,col-1) && vis[i][col-1] == false){\n                    cnt++;\n                    vis[i][col-1]=true;\n                    ans.push_back({i,col-1});\n                }\n            }\n            if(cnt == n*m)\n                break;\n            col--;\n        }\n        return ans;\n        //Do upvote if you like the solution.\n    }\n};\n```
['C++']