Advertisement
ElenaR1

Stack and tree 3 zad

Nov 21st, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.91 KB | None | 0 0
  1. #include <iostream>
  2. #include<iomanip>
  3.  
  4. #include <stack>
  5. using namespace std;
  6. #define maxsize 8
  7.  
  8. bool labirint[maxsize][maxsize] = { false };
  9.  
  10. void displayLabirint(int size)
  11. {
  12.     for (int r = 0; r<size; r++)
  13.     {
  14.         for (int c = 0; c<size; c++)
  15.             cout << setw(4) << !labirint[r][c];
  16.  
  17.         cout << endl;
  18.     }
  19.  
  20.  
  21. }
  22. bool possibleMovement(int x, int y, int size)
  23. {
  24.     return (0 <= x) &&
  25.         (0 <= y) &&
  26.         (x <size) &&
  27.         (y <= size) &&
  28.         (!labirint[x][y]);
  29.  
  30.  
  31. }
  32.  
  33. bool downstairs(int sx, int sy, int tx, int ty, int size)
  34. {
  35.     labirint[sx][sy] = true;
  36.     if (sx == tx&&sy == ty)
  37.     {
  38.         return true;
  39.     }
  40.  
  41.     if (possibleMovement(sx, (sy + 1), size) && possibleMovement(sx + 1, sy, size))
  42.         return downstairs(sx + 1, sy, tx, ty, size) || downstairs(sx, sy + 1, tx, ty, size);
  43.     if (possibleMovement(sx, sy + 1, size))
  44.         //labirint[sx][sy + 1] = true;
  45.         return downstairs(sx, sy + 1, tx, ty, size);
  46.     if (possibleMovement(sx + 1, sy, size))
  47.         //labirint[sx+1][sy] = true;
  48.         return downstairs(sx + 1, sy, tx, ty, size);
  49.     else
  50.         return false;
  51.  
  52.  
  53.  
  54. }
  55.  
  56.  
  57. struct Operation
  58. {
  59.  
  60.     int r;
  61.     int c;
  62.  
  63.     Operation(int _r, int _c)
  64.         : r(_r), c(_c) {}
  65.     //Operation (){}
  66. };
  67.  
  68. bool downstairsStack(int xs, int ys, int xt, int yt, int size)
  69.  
  70. {
  71.     stack<Operation> s;
  72.     s.push(Operation(xs, ys));
  73.  
  74.     while (!s.empty())
  75.  
  76.     {
  77.         Operation top = s.top();
  78.         int xs = top.r;
  79.         int ys = top.c;
  80.  
  81.         s.pop();
  82.  
  83.         if (xs == xt&&ys == yt)//ili ako ne pishem che xs=top.r tuk shte e (top.r==xs&&top.c==yt)
  84.             return true;
  85.  
  86.         else if (possibleMovement(xs + 1, top.c, size) && possibleMovement(top.r, ys + 1, size))
  87.         {
  88.             s.push(Operation((xs + 1), ys));
  89.             s.push(Operation(xs, ys + 1));
  90.         }
  91.  
  92.         else if (possibleMovement(xs + 1, ys, size))
  93.             s.push(Operation((xs + 1), ys));
  94.  
  95.  
  96.         else if (possibleMovement(xs, ys + 1, size))
  97.             s.push(Operation(xs, ys + 1));
  98.     }
  99.     if (s.empty())
  100.         return false;
  101. }
  102.  
  103. //ako pisha int xs=top.r
  104. /*bool downstairsStack(int xs,int ys,int xt,int yt)
  105.  
  106. {
  107. stack<Operation> s;
  108. s.push(Operation(   xs,ys));
  109.  
  110. while(!s .empty())
  111.  
  112. {
  113. Operation top=s.top();
  114. s.pop();
  115.  
  116. if(top.r==xt&&top.c==yt)
  117. return 1;
  118.  
  119. else if (possibleMovement(top.r+1,top.c)&&possibleMovement(top.r,top.c+1))
  120. {
  121. s.push(Operation((top.r+1),top.c));
  122. s.push(Operation( top.r ,top.c+1));
  123. }
  124.  
  125. else if (possibleMovement(top.r+1,top.c) )
  126. s.push(Operation((top.r+1),top.c));
  127.  
  128.  
  129. else if ( possibleMovement(top.r,top.c+1))
  130. s.push(Operation( top.r ,top.c+1));
  131. }
  132. if(s.empty())
  133. return false;
  134.  
  135.  
  136.  
  137. }*/
  138.  
  139. int main()
  140. {
  141.     displayLabirint(4);
  142.     //cout << downstairs(2, 0, 3, 3, 4) << endl;//Pechatat se pravilno no ako naprimer stane taka che labirint[1][1]=true
  143.     //posle ne sme go slojili da stava false i zatova ne moga da izprobvam vsichki navednuj
  144.     //displayLabirint(4);
  145.     //cout << downstairs(1, 3, 0, 0, 4) << endl;
  146.     //displayLabirint(4);
  147.     cout << downstairsStack(2, 0, 3, 3, 4) << endl;
  148.     //cout << downstairsStack(1, 3, 0, 0, 4) << endl;
  149.     return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement