You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rotten Oranges问题BFS实现输出不符预期,请求排查修正

Bug Analysis and Fix for Rotting Oranges BFS Implementation

Let's break down the issues in your code that are causing the incorrect output, and fix them step by step.

Key Issues Identified

1. Missing Initial Rotten Oranges in Queue

Your code only adds the first rotten orange it finds to the queue, then breaks out of the loops. This is wrong because there might be multiple rotten oranges at the start (all of which start spreading decay at minute 0). Ignoring additional initial rotten oranges delays the decay process and leads to incorrect timing.

2. Incorrect Direction Traversal

You're using a nested for loop for directions:

for(i=0;i<4;i++) {
    for(j=0;j<4;j++) {
        int rr = a + r[i];
        int cc = b + c[j];
        // ...
    }
}

This loops through 16 invalid combinations instead of the 4 valid adjacent directions. The r and c arrays are paired (each index corresponds to one direction: up, down, left, right), so you only need a single loop from 0 to 3 to iterate through all four valid neighbors.

3. Wrong Timing Logic

Your current code increments min every time a single orange is infected. But BFS for this problem should count each level of the BFS as one minute—meaning you process all rotten oranges present at the start of the minute, then increment the timer once after processing all of them. Your approach overcounts timing based on individual infections instead of full time steps.

Fixed Code

Here's the corrected implementation addressing all the above issues:

class Solution {
public:
    int orangesRotting(vector<vector<int>>& grid) {
        int m = grid.size();
        int n = grid[0].size();
        int minutes = 0;
        int freshCount = 0;
        // Direction arrays: up, down, left, right
        int r[4] = {-1, 1, 0, 0};
        int c[4] = {0, 0, -1, 1};
        queue<pair<int, int>> q;

        // Step 1: Count fresh oranges and enqueue all initial rotten oranges
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1) {
                    freshCount++;
                } else if (grid[i][j] == 2) {
                    q.push({i, j}); // Add all initial rotten oranges to queue
                }
            }
        }

        // Step 2: BFS to spread decay level by level
        while (!q.empty() && freshCount > 0) {
            int levelSize = q.size(); // Number of rotten oranges at current minute
            // Process all oranges in current level (one minute's work)
            for (int k = 0; k < levelSize; k++) {
                auto current = q.front();
                q.pop();
                int x = current.first;
                int y = current.second;

                // Check all four valid directions
                for (int dir = 0; dir < 4; dir++) {
                    int nx = x + r[dir];
                    int ny = y + c[dir];
                    // Validate neighbor and check if it's a fresh orange
                    if (nx >= 0 && nx < m && ny >=0 && ny < n && grid[nx][ny] == 1) {
                        grid[nx][ny] = 2; // Mark as rotten
                        q.push({nx, ny});
                        freshCount--;
                    }
                }
            }
            // Increment minute only after processing all oranges in current time step
            minutes++;
        }

        // If fresh oranges remain, return -1; else return total minutes
        return freshCount == 0 ? minutes : -1;
    }
};

Explanation of Fixes

  • Initial Queue Setup: We now enqueue all initial rotten oranges, ensuring all sources of decay start spreading simultaneously.
  • Direction Handling: A single loop over the 4 direction pairs ensures we only check valid adjacent cells, avoiding redundant or invalid checks.
  • Level-Based Timing: By processing all nodes in the queue at once (each level represents one minute), we correctly count the time taken for decay to spread. The timer increments only after all oranges from the current time step have finished infecting their neighbors.

Test Case Verification

For your input [[2,1,1],[1,1,0],[0,1,1]]:

  1. Initial queue has (0,0), fresh count is 5.
  2. Minute 1: Process (0,0), infect (0,1) and (1,0). Fresh count becomes 3. Queue now holds (0,1), (1,0).
  3. Minute 2: Process (0,1) → infect (0,2); process (1,0) → infect (1,1). Fresh count becomes 1. Queue now holds (0,2), (1,1).
  4. Minute 3: Process (0,2) (no valid fresh neighbors); process (1,1) → infect (2,1). Fresh count becomes 0. Queue now holds (2,1).
  5. Loop exits since fresh count is 0. Total minutes is 4, matching the expected output.

内容的提问来源于stack exchange,提问作者Nikita

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:11:03