class Solution:

    def spiralMatrixIII(self, R, C, r0, c0):
        move = [(0, 1), (1, 0), (0, -1), (-1, 0)]
        res = [[r0, c0]]
        step_size = 1
        dire = 0

        incre = 0
        while len(res) < R * C:
            for i in range(step_size):
                r0 = r0 + move[dire][0]
                c0 = c0 + move[dire][1]
                if 0 <= r0 < R and 0 <= c0 < C:
                    res.append([r0, c0])
            dire = (dire + 1) % 4
            incre = (incre + 1) % 2
            if incre == 0:
                step_size += 1
        return res


if __name__ == '__main__':
    print Solution().spiralMatrixIII(3, 3, 1, 1)
    print Solution().spiralMatrixIII(5, 5, 2, 2)
