Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <vector>
- enum class Direction { Left, Right };
- void shiftImage(std::vector<std::vector<int>> &mat, int shift, Direction dir)
- {
- if (dir == Direction::Left) {
- for (auto &row : mat) {
- std::rotate(row.rbegin(), row.rbegin() + shift, row.rend());
- }
- return;
- }
- for (auto &row : mat) {
- std::rotate(row.begin(), row.begin() + shift, row.end());
- }
- }
- void print(const std::vector<std::vector<int>> &mat)
- {
- for (const auto &y : mat) {
- for (const auto &j : y) {
- std::cout << j << " ";
- }
- std::cout << "\n";
- }
- std::cout << "\n";
- }
- int main()
- {
- std::vector<std::vector<int>> a{
- {1, 2, 3, 4, 5},
- {1, 2, 3, 4, 5},
- {1, 2, 3, 4, 5}
- };
- std::vector<std::vector<int>> b(a);
- print(a);
- shiftImage(a, 1, Direction::Left);
- print(a);
- shiftImage(b, 1, Direction::Right);
- print(b);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement