Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
671 B
1
Indexable
Never
#include <iostream>
#include <vector>

std::vector<std::vector<int>> matrix = {
    {1, 2, 5, 6},
    {3, 4, 7, 8},
    {9, 10, 13, 14},
    {11, 12, 15, 16}
};

void doPrint(std::vector<std::vector<int>> &matrix, int row, int col){
    if(row<0|| row>=matrix.size() || col<0 || col>=matrix[0].size()) return;
    std::cout<<matrix[row][col]<<" ";
    if(!(col&1)) doPrint(matrix, row, col+1);
    else if(!(row&1) && col&1) doPrint(matrix, row+1, col-1);
    else if(col==matrix[0].size()-1 && row&1) doPrint(matrix, row+1, 0);
    else if(row&1 && col&1) doPrint(matrix, row-1, col+1);
}

int main(){

    doPrint(matrix, 0, 0);
    return 0;
}