Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <stdlib.h>
- #include <ctime>
- using namespace std;
- //1 - вверх
- //2 - вправо
- //3 - вниз
- //4 - влево
- bool isEmpty(int direction, int n, int m, int x, int y,vector<vector<int>> field) {
- switch (direction)
- {
- case 1:
- return (y - 1 >= 1) && (field[y - 2][x - 1] == 0);
- break;
- case 2:
- return (x + 1 <= m) && (field[y - 1][x] == 0);
- break;
- case 3:
- return (y + 1 <= n) && (field[y][x - 1] == 0);
- break;
- case 4:
- return (x - 1 >= 1) && (field[y - 1][x - 2] == 0);
- break;
- }
- }
- int newDirection(int direction) {
- switch (direction)
- {
- case 1:
- return 2;
- break;
- case 2:
- return 3;
- break;
- case 3:
- return 4;
- break;
- case 4:
- return 1;
- break;
- }
- }
- int main()
- {
- srand(time(NULL));
- int n;
- int m;
- cin >> n;
- cin >> m;
- int number = 1;
- int direction = 2;
- vector <vector<int>> field;
- field.resize(n);
- for (int i = 0; i < n; i++) {
- field[i].resize(m);
- for (int j = 0; j < m; j++) {
- field[i][j] = 0;
- }
- }
- int x = (rand() % (m - 1)) + 1;
- int y = (rand() % (n - 1)) + 1;
- while (number <= n * m) {
- field[y-1][x-1] = number;
- if (!isEmpty(direction, n, m, x, y, field)) {
- direction = newDirection(direction);
- }
- switch (direction) {
- case 1:
- y--;
- break;
- case 2:
- x++;
- break;
- case 3:
- y++;
- break;
- case 4:
- x--;
- break;
- }
- number++;
- }
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- cout << field[i][j] << ' ';
- }
- cout << endl;
- }
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement