Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <iostream>
- #include <cassert>
- #include <iomanip>
- void print_mat(int** mat, size_t size)
- {
- for (size_t i = 0; i < size; i++)
- {
- for (size_t j = 0; j < size; j++)
- {
- std::cout.width(3);
- std::cout << mat[i][j] << ' ';
- }
- std::cout << "\n";
- }
- }
- void fill_line(int** spiral, size_t posx, size_t posy, int len, bool byX, int& num, bool upright = false)
- {
- // limiting bound of array to [0; inf]
- print_mat(spiral, 7);
- std::cout << '\n';
- //assert(!upright || (posx >= len) || !byX);
- //assert(!upright || (posy >= len) || byX);
- if (byX)
- {
- for (int i = 0; i < len; i++)
- {
- int offset = upright ? -i : i;
- spiral[posy][posx + offset] = num;
- num++;
- }
- }
- else
- {
- for (int i = 0; i < len; i++)
- {
- int offset = upright ? -i : i;
- spiral[posy + offset][posx] = num;
- num++;
- }
- }
- }
- static int num = 2;
- void fill_ulam(int** spiral, size_t posx, size_t posy, size_t depth = 1)
- {
- // fill up
- fill_line(spiral, posx, posy, depth * 2, false, num, true);
- posy -= depth * 2 - 1;
- posx -= 1;
- // fill left
- fill_line(spiral, posx, posy, depth * 2, true, num, true);
- posx -= depth * 2 - 1;
- posy += 1;
- // fill down
- fill_line(spiral, posx, posy, depth * 2, false, num, false);
- posy += depth * 2 - 1;
- posx += 1;
- // fill right
- fill_line(spiral, posx, posy, depth * 2, true, num, false);
- posx += depth * 2;
- if (depth == 10) return; // recursive end (limit of size of our matrix)
- // recursive continue
- fill_ulam(spiral, posx, posy, depth + 1);
- }
- int main()
- {
- constexpr size_t size = 2*10 + 1;
- int** spiral = new int* [size];
- for (size_t i = 0; i < size; i++) {
- spiral[i] = new int[size] {};
- }
- spiral[size / 2][size / 2] = 1;
- fill_ulam(spiral, size / 2 + 1, size / 2);
- print_mat(spiral, size);
- for (size_t i = 0; i < size; i++) {
- delete[] spiral[i];
- }
- delete[] spiral;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement