csansoon

P8.12 P59112 Spirals

Nov 29th, 2018
648
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int main() {
  6.     int n;
  7.     while (cin>>n) {
  8.         if (n>0) {
  9.         vector <vector <bool> > screen(n,(vector<bool>(n,1)));
  10.         int x = 0;
  11.         int y = 0;
  12.         if (n>1) screen[y][x]=0;
  13.         int i=n-1;
  14.         while (i>0) {
  15.             //Down
  16.             while (y+2<n && screen[y+2][x]==1) {
  17.                 ++y;
  18.                 screen[y][x]=0;
  19.             }
  20.             --i;
  21.             //Right
  22.             while (x+2<n && screen[y][x+2]==1) {
  23.                 ++x;
  24.                 screen[y][x]=0;
  25.             }
  26.             --i;
  27.             //Up
  28.             while (y-2>=0 && screen[y-2][x]==1) {
  29.                 --y;
  30.                 screen[y][x]=0;
  31.             }
  32.             --i;
  33.             //Left
  34.             while (x-2>=0 && screen[y][x-2]==1) {
  35.                 --x;
  36.                 screen[y][x]=0;
  37.             }
  38.             --i;
  39.         }
  40.         for (i=0;i<n;++i) {
  41.             for (int j=0; j<n; ++j) {
  42.                 if (screen[i][j]==0) cout<<".";
  43.                 else cout<<"X";
  44.             }
  45.         cout<<endl;
  46.         }
  47.         cout<<endl;
  48.         }
  49.     }
  50. }
  51.  
  52. // (c) Carlos Sansón (Best pro1 delegate ever for sure) @csansoon
Advertisement
Add Comment
Please, Sign In to add comment