There are two types of movements (move types, not directions) in the problem, and 4 different directions I can move in, my question is not the whole thing but something specific
bool solveRec(int move, vector<vll> &vis, int i, int j, int n, vector<vector<char>> &pth)
{
if (i == 1 && j == n - 1)
{
return true;
}
// even move, wherever you want
if (i < 0 || i > 1 || j >= n || j < 0)
return false;
if (vis[i][j] == true)
return false;
bool ans = false;
if (move == 0)
{
ans |= solveRec(1, vis, i + 1, j, n, pth);
ans |= solveRec(1, vis, i - 1, j, n, pth);
ans |= solveRec(1, vis, i, j + 1, n, pth);
ans |= solveRec(1, vis, i, j + 1, n, pth);
}
else
{
if (pth[i][j] == '>')
ans |= solveRec(0, vis, i, j + 1, n, pth);
else
ans |= solveRec(0, vis, i, j - 1, n, pth);
}
return ans;
}
When this piece of code :
if (move == 0)
{
ans |= solveRec(1, vis, i + 1, j, n, pth);
ans |= solveRec(1, vis, i - 1, j, n, pth);
ans |= solveRec(1, vis, i, j + 1, n, pth);
ans |= solveRec(1, vis, i, j + 1, n, pth);
}
is changed to :
if (move == 0)
{
ans = solveRec(1, vis, i + 1, j, n, pth) || solveRec(1, vis, i - 1, j, n, pth) ||
solveRec(1, vis, i, j + 1, n, pth) || solveRec(1, vis, i + 1, j - 1, n, pth);
}
to see if moving in any of the directions give a positive (i.e. result in destination to be reached) and if so, ans
stores 1
else it stores 0
. But earlier while using bitwise OR |
nothing happens, but when it is changed to logical OR ||
the program works as intended. Why is it so?
pkra2 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.