3
vector<vector<int>> spiralMatrixIII(int rows, int cols, int rStart, int cStart) {4
vector<vector<int>> ans;5
ans.push_back({rStart, cStart}); // Pushing the starting point in answer6
int topRow = rStart - 1; // Row above starting point7
int bottomRow = rStart + 1; // Row below starting point8
int leftCol = cStart - 1; // Col left to starting point9
int rightCol = cStart + 1; // Col right to starting point11
while (topRow != -1 || bottomRow != rows || leftCol != -1 ||12
rightCol != cols) { // Untill all rows and columns are exhausted14
if (rightCol != cols) { // Checking if this col is exhausted15
for (int i = topRow + 1; i < bottomRow; i++)16
ans.push_back({i, rightCol}); // Running loop from one bottom of top17
// row till bottom row in right col18
rightCol++; // Incrementing the col forward20
// Similar things are done below with different cols and rows21
if (bottomRow != rows) {22
for (int j = rightCol - 1; j > leftCol; j--) ans.push_back({bottomRow, j});27
for (int i = bottomRow - 1; i > topRow; i--) ans.push_back({i, leftCol});32
for (int j = leftCol + 1; j < rightCol; j++) ans.push_back({topRow, j});