Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool hit(int x, int n) {
- return x == n ? true : false;
- }
- std::vector<std::vector<int>> spiralNumbers(int n) {
- int count = 0;
- int direction = 0;
- int x, y = 0;
- std::vector<std::vector<int>> matrix;
- while(count < (n*n)+1) {
- std::cout << count << std::endl;
- bool flag = 0;
- switch(direction) {
- case 0 :
- std::cout << "right" << std::endl;
- x++; // right
- if(hit(x, n)) {
- x--;
- direction++;
- flag = 1;
- }
- break;
- case 1 :
- std::cout << "down" << std::endl;
- y++; // down
- if(hit(y, n)) {
- y--;
- direction++;
- flag = 1;
- }
- break;
- case 2 :
- std::cout << "left" << std::endl;
- x--; // left
- if(hit(x, n)) {
- x++;
- direction++;
- flag = 1;
- }
- break;
- case 3 :
- std::cout << "up" << std::endl;
- y--; // up
- if(hit(y, n)) {
- y++;
- direction++;
- flag = 1;
- }
- break;
- }
- if(!flag) {
- matrix[x][y] = count;
- count++;
- }
- std::cout << "direction: " << direction << " x: " << x << " y: " << y << std::endl;
- if(direction == 4) direction = 0;
- }
- return matrix;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement