Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<iomanip>
- #include <stack>
- using namespace std;
- #define maxsize 8
- bool labirint[maxsize][maxsize] = { false };
- void displayLabirint(int size)
- {
- for (int r = 0; r<size; r++)
- {
- for (int c = 0; c<size; c++)
- cout << setw(4) << !labirint[r][c];
- cout << endl;
- }
- }
- bool possibleMovement(int x, int y, int size)
- {
- return (0 <= x) &&
- (0 <= y) &&
- (x <size) &&
- (y <= size) &&
- (!labirint[x][y]);
- }
- bool downstairs(int sx, int sy, int tx, int ty, int size)
- {
- labirint[sx][sy] = true;
- if (sx == tx&&sy == ty)
- {
- return true;
- }
- if (possibleMovement(sx, (sy + 1), size) && possibleMovement(sx + 1, sy, size))
- return downstairs(sx + 1, sy, tx, ty, size) || downstairs(sx, sy + 1, tx, ty, size);
- if (possibleMovement(sx, sy + 1, size))
- //labirint[sx][sy + 1] = true;
- return downstairs(sx, sy + 1, tx, ty, size);
- if (possibleMovement(sx + 1, sy, size))
- //labirint[sx+1][sy] = true;
- return downstairs(sx + 1, sy, tx, ty, size);
- else
- return false;
- }
- struct Operation
- {
- int r;
- int c;
- Operation(int _r, int _c)
- : r(_r), c(_c) {}
- //Operation (){}
- };
- bool downstairsStack(int xs, int ys, int xt, int yt, int size)
- {
- stack<Operation> s;
- s.push(Operation(xs, ys));
- while (!s.empty())
- {
- Operation top = s.top();
- int xs = top.r;
- int ys = top.c;
- s.pop();
- if (xs == xt&&ys == yt)//ili ako ne pishem che xs=top.r tuk shte e (top.r==xs&&top.c==yt)
- return true;
- else if (possibleMovement(xs + 1, top.c, size) && possibleMovement(top.r, ys + 1, size))
- {
- s.push(Operation((xs + 1), ys));
- s.push(Operation(xs, ys + 1));
- }
- else if (possibleMovement(xs + 1, ys, size))
- s.push(Operation((xs + 1), ys));
- else if (possibleMovement(xs, ys + 1, size))
- s.push(Operation(xs, ys + 1));
- }
- if (s.empty())
- return false;
- }
- //ako pisha int xs=top.r
- /*bool downstairsStack(int xs,int ys,int xt,int yt)
- {
- stack<Operation> s;
- s.push(Operation( xs,ys));
- while(!s .empty())
- {
- Operation top=s.top();
- s.pop();
- if(top.r==xt&&top.c==yt)
- return 1;
- else if (possibleMovement(top.r+1,top.c)&&possibleMovement(top.r,top.c+1))
- {
- s.push(Operation((top.r+1),top.c));
- s.push(Operation( top.r ,top.c+1));
- }
- else if (possibleMovement(top.r+1,top.c) )
- s.push(Operation((top.r+1),top.c));
- else if ( possibleMovement(top.r,top.c+1))
- s.push(Operation( top.r ,top.c+1));
- }
- if(s.empty())
- return false;
- }*/
- int main()
- {
- displayLabirint(4);
- //cout << downstairs(2, 0, 3, 3, 4) << endl;//Pechatat se pravilno no ako naprimer stane taka che labirint[1][1]=true
- //posle ne sme go slojili da stava false i zatova ne moga da izprobvam vsichki navednuj
- //displayLabirint(4);
- //cout << downstairs(1, 3, 0, 0, 4) << endl;
- //displayLabirint(4);
- cout << downstairsStack(2, 0, 3, 3, 4) << endl;
- //cout << downstairsStack(1, 3, 0, 0, 4) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement