Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- std::vector<std::vector<int>> readSize() {
- std::vector<std::vector<int>> vec;
- int x, y;
- std::cin >> x >> y;
- for (int i = 0; i < x; ++i) {
- std::vector<int> row(y);
- vec.push_back(row);
- }
- return vec;
- }
- std::pair<int, int> readStartingCoord() {
- int x, y;
- std::cin >> x >> y;
- return std::make_pair(x, y);
- }
- void makeMove(std::vector<std::vector<int>>& field, int& x, int& y, int& target) {
- if (x - 1 >= 0) {
- if (field[x - 1][y] == 0) field[x - 1][y] = target;
- if (y - 1 >= 0 && field[x - 1][y - 1] == 0) field[x - 1][y - 1] = target;
- if (y + 1 < field[x].size() && field[x - 1][y + 1] == 0) field[x - 1][y + 1] = target;
- }
- if (x + 1 < field.size()) {
- if (field[x + 1][y] == 0) field[x + 1][y] = target;
- if (y - 1 >= 0 && field[x + 1][y - 1] == 0) field[x + 1][y - 1] = target;
- if (y + 1 < field[x].size() && field[x + 1][y + 1] == 0) field[x + 1][y + 1] = target;
- }
- if (y - 1 >= 0) {
- if (field[x][y - 1] == 0) field[x][y - 1] = target;
- }
- if (y + 1 < field[x].size()) {
- if (field[x][y + 1] == 0) field[x][y + 1] = target;
- }
- }
- void processSolution(std::vector<std::vector<int>>& field, std::pair<int, int>& coords) {
- field[coords.first][coords.second] = 1;
- int target = 2;
- for (int k = 0; k < field.size(); ++k) {
- while (std::find(field[k].begin(), field[k].end(), 0) != field[k].end()) {
- for (int i = 0; i < field.size(); ++i) {
- for (int j = 0; j < field[i].size(); ++j) {
- if (field[i][j] == target - 1) {
- makeMove(field, i, j, target);
- }
- }
- }
- target++;
- }
- }
- }
- void printSolution(std::vector<std::vector<int>>& field) {
- for (auto& x : field) {
- for (auto& y : x) {
- std::cout << y << ' ';
- }
- std::cout << std::endl;
- }
- }
- int main() {
- auto field = readSize();
- auto startingCoords = readStartingCoord();
- processSolution(field, startingCoords);
- printSolution(field);
- }
Advertisement
Add Comment
Please, Sign In to add comment