okay4869's blog

By okay4869, 10 months ago, In English

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

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
10 months ago, # |
  Vote: I like it +30 Vote: I do not like it

Are you sure you didn't change anything else? I couldn't understand the reason for this either, could you perhaps send the link/code for both submissions?

»
10 months ago, # |
Rev. 4   Vote: I like it -7 Vote: I do not like it

$$$wrong$$$