maycod23

Untitled

Feb 20th, 2022 (edited)
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.13 KB | None | 0 0
  1. # include   <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4.  
  5. typedef pair<ll, ll> pl;
  6.  
  7. typedef vector<ll> vl;
  8. typedef vector<pl> vpl;
  9.  
  10. ///these are some Macros which you can use in your code too
  11.  
  12. # define    fast                ios_base::sync_with_stdio(false);cin.tie(NULL);
  13. # define    MOD                 1000000007
  14. # define    endl                '\n'
  15. # define    INF                 9e18
  16. # define    PI                  3.14159265358979323846
  17. # define    lb                  lower_bound
  18. # define    ub                  upper_bound
  19. # define    mp                  make_pair
  20. # define    pb                  push_back
  21. # define    fi                  first
  22. # define    se                  second
  23. # define    all(a)              a.begin(), a.end()
  24. # define    iceil(n, x)         (((n) + (x) - 1) / (x))
  25.  
  26.  
  27. //5.>map<int,int> m;
  28. // m[1]++;
  29. map<pair<ll, ll>, ll> m;
  30.  
  31. void fun(ll x, ll y, bool ns, bool ew, ll n, vector<pair<ll, ll>>& ans)
  32. {
  33.     if (n == 0)//base case
  34.     {
  35.         pair<ll, ll> temp; temp.first = x, temp.second = y;
  36.  
  37.         //distinct points to be added in the answer
  38.         if (m[temp] >= 1)
  39.             return;
  40.         else
  41.         {
  42.             ans.push_back(temp);
  43.             m[temp]++;//(x,y) is visited
  44.             return;
  45.         }
  46.     }
  47.  
  48. ////we need to make the choices
  49.     if (ns)//chance to go in north south
  50.     {
  51.         fun(x, y + 1, false, true, n - 1, ans);//north
  52.         fun(x, y - 1, false, true, n - 1, ans);//south
  53.     }
  54.     else//chance to in east west
  55.     {
  56.         fun(x + 1, y, true, false, n - 1, ans);//east
  57.         fun(x - 1, y, true, false, n - 1, ans);//west
  58.     }
  59. }
  60. int main()
  61. {
  62.     fast;
  63.     ll t = 1;
  64.     // cin >> t;
  65.     while (t--)
  66.     {
  67.         ll n; cin >> n;//number of steps
  68.         //1.>vector<pair<ll,ll>>
  69.         //pair<int,int> temp={1,2};
  70.         //stl :
  71.         // pair<int,int> p;
  72.         // p.first=1,p.second=2;
  73.  
  74.         // 2.>vector<pair<ll,ll>> ans
  75.  
  76.         vector<pair<ll, ll>> ans;
  77.  
  78. ///////recursive calls
  79.         fun(0, 0, true, false, n, ans);
  80.         fun(0, 0, false, true, n, ans);
  81.  
  82.         // //3.>stl sort function
  83.         // // sort(a, a + n)
  84.         // // (0,0) (-1,0) (-1,2);
  85.         // //(-1,0) (-1,2) (0,0)
  86.         // cout << ans.size() << endl;
  87.         sort(ans.begin(), ans.end());
  88.  
  89.         // //4.>for(int i=0;i<n;i++)
  90.         for (auto i : ans)//i will be treated as iterator of pair
  91.         {
  92.             //auto keyword** to remember
  93.             cout << i.first << " " << i.second << " " << endl;
  94.         }
  95.  
  96.         // vector<ll> v; v.pb(1); v.pb(2), v.pb(3);
  97.         // // v->1 , 2, 3
  98.         // for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
  99.         // cout << endl;
  100.         // for (auto i : v)
  101.         // {
  102.         //     cout << i << " ";
  103.         // }
  104.  
  105.         //time complexity->O(2^n)//exponential
  106.         //space complexity->O(n*n)
  107.  
  108.     }
  109.     return 0;
  110. }
  111. /* stuff you should look for
  112. 1.>pair<int,int> p; (access as p.first and p.second)
  113. 2.>vector<pair<int,int>> ans; (vector of pairs)
  114. 3.>map<pair<int,int>,int> mp;
  115. 4.>sort(a.begin(),a.end())-> by default small will come first
  116. */
Add Comment
Please, Sign In to add comment