aboutsummaryrefslogtreecommitdiff
path: root/spiral_matrix3.c
blob: b0e1a08ee2c1d8ec3acf16f5a6f9a033fb7b60c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// 885. Spiral Matrix III
#include "leetcode.h"

/*
 * you start at the cell '(rStart, cStart)' of an 'rows * cols' grid facing
 * east. the northwest corner is at the first row and column in the grid, and
 * the southeast corner is at the last row and column. you will walk in a
 * clockwise spiral shape to visit every position in this gid. whenever you move
 * outside the grid's boundary, we continue our walk outside the grid (but may
 * return to the gid boundary later). eventually we reach all 'rows * cols'
 * spaces of the grid. return an array of coordinates representing the positions
 * of the grid in the order visited.
 */

int **spiralMatrixIII(int rows, int cols, int rStart, int cStart,
                      int *returnSize, int **returnColumnSizes) {
  *returnSize = rows * cols;
  *returnColumnSizes = (int *)malloc((*returnSize) * sizeof(int));
  int **ans = (int **)malloc((*returnSize) * sizeof(int *));
  for (int i = 0; i < (*returnSize); i++) {
    ans[i] = (int *)calloc(2, sizeof(int));
    (*returnColumnSizes)[i] = 2;
  }
  ans[0][0] = rStart;
  ans[0][1] = cStart;
  int up = 1, right = 1, cnt_rl = 0, cnt_ud = 0, idx = 1;
  while (idx < (*returnSize)) {
    for (int shift = 1; shift <= right; shift++) {
      cStart = !(cnt_rl % 2) ? cStart + 1 : cStart - 1;
      if (rStart < rows && cStart < cols && rStart > -1 && cStart > -1) {
        ans[idx][0] = rStart;
        ans[idx][1] = cStart;
        idx++;
      }
    }
    cnt_rl++, right++;
    for (int shift = 1; shift <= up; shift++) {
      rStart = !(cnt_ud % 2) ? rStart + 1 : rStart - 1;
      if (rStart < rows && cStart < cols && rStart > -1 && cStart > -1) {
        ans[idx][0] = rStart;
        ans[idx][1] = cStart;
      }
    }
    cnt_ud++, up++;
  }
  return ans;
}

int main() {
  int rs1, rs2, **rcs1, **rcs2;
  int **sm31 = spiralMatrixIII(1, 4, 0, 0, &rs1, rcs1);
  int **sm32 = spiralMatrixIII(5, 6, 1, 4, &rs1, rcs2);
  for (int i = 0; i < rs1; i++)
    for (int j = 0; j < rcs1[i][j]; j++)
      printf("%d ", sm31[i][j]);
  printf("\n");
  for (int i = 0; i < rs2; i++)
    for (int j = 0; j < rcs2[i][j]; j++)
      printf("%d ", sm32[i][j]);
  printf("\n");
  for (int i = 0; i < rs1; i++)
    free(sm31[i]);
  for (int i = 0; i < rs2; i++)
    free(sm32[i]);
  free(sm31), free(sm32);
}