Doubt for the question here:
https://leetcode.com/problems/rotting-oranges/description/
How I initialized my queue: queue<tuple<int, int, int>> q;
WA :
while(!q.empty()) {
auto [x, y, curr] = q.front(); q.pop();
steps = max(steps, curr);
for(int i = 0; i < 4; ++i) {
int X = x+dx[i], Y = y+dy[i];
if(X >= 0 && X < N && Y >= 0 && Y < M && grid[X][Y] == 1 && vis[X][Y] != 2) {
q.push({X, Y, curr+1});
vis[X][Y] = 2;
}
}
}
AC :
while(!q.empty()) {
int x, y, curr; tie(x, y, curr) = q.front(); q.pop();
steps = max(steps, curr);
for(int i = 0; i < 4; ++i) {
int X = x+dx[i], Y = y+dy[i];
if(X >= 0 && X < N && Y >= 0 && Y < M && grid[X][Y] == 1 && vis[X][Y] != 2) {
q.push({X, Y, curr+1});
vis[X][Y] = 2;
}
}
}
The WA one passes all but one testcase, which is:
grid = [[0,0,0,0],[0,1,1,2],[1,1,1,0],[1,1,1,2],[2,0,1,1],[2,1,2,1],[1,2,0,1],[2,2,1,2],[2,1,1,0],[1,0,2,2]];
This is the first time I am experiencing this. What is the reason behind this? Thank you