From f6e65e88059ed533db2e83598da5b42bf9ce8a09 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Mon, 11 Apr 2022 18:50:57 +0430 Subject: update --- 1260/main.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 1260/main.cpp (limited to '1260/main.cpp') diff --git a/1260/main.cpp b/1260/main.cpp new file mode 100644 index 0000000..c340c79 --- /dev/null +++ b/1260/main.cpp @@ -0,0 +1,64 @@ +#include "header.hpp" +#include + +class Solution { +public: + static std::vector> + shiftGrid(std::vector> &grid, int k) { + if (k == 0) { + return grid; + } + + auto row_count = grid.size(); + auto col_count = grid[0].size(); + std::vector> result; + result.resize(row_count); + for (auto &iter : result) { + iter.resize(col_count); + } + + /* if (k % grid.size() == 0) { */ + /* for (auto &iter : grid) { */ + /* std::copy(iter.begin(), iter.end(), */ + /* result[((k % grid[0].size()) - 1) % grid.size()].begin()); + */ + /* } */ + /* } */ + + for (int i = 0; i < row_count; ++i) { + for (int j = 0; j < col_count; ++j) { + auto index = ((col_count * i + j + k)) % (col_count * row_count) + 1; + auto new_row = std::ceil(static_cast(index) / + static_cast(col_count)); + auto new_col = index - ((new_row - 1) * col_count); + result[new_row - 1][new_col - 1] = grid[i][j]; + std::cout << "index:" << index << " new:" << new_row << "," << new_col + << "\n"; + } + } + return result; + } +}; + +void pprint(std::vector> &&grid) { + for (auto &iter : grid) { + for (auto &ya_iter : iter) { + std::cout << ya_iter << " "; + } + std::cout << "\n"; + } + std::cout << "\n"; +} + +int main(int argc, char **argv) { + std::vector> grid1 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; + std::vector> grid2 = { + {3, 8, 1, 9}, {19, 7, 2, 5}, {4, 6, 11, 10}, {12, 0, 21, 13}}; + std::vector> grid3 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; + + pprint(Solution::shiftGrid(grid1, 1)); + pprint(Solution::shiftGrid(grid2, 4)); + pprint(Solution::shiftGrid(grid3, 9)); + + return 0; +} -- cgit v1.2.3