This commit is contained in:
@@ -0,0 +1,196 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-Hans">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>052. 腐烂的橘子 – 图解</title>
|
||||
<link rel="stylesheet" href="../shared/style.css">
|
||||
<style>
|
||||
/* page-specific overrides */
|
||||
.vis-area { min-height: 120px; padding: 16px 0; }
|
||||
.code-section { margin-top: 16px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>🟡 052. 腐烂的橘子 <span class="badge medium">中等</span></h1>
|
||||
<p class="subtitle">分类:图论 | LeetCode Hot 100</p>
|
||||
|
||||
<!-- 控制面板 -->
|
||||
<div class="controls" id="controls">
|
||||
<label for="inputArea">输入:</label>
|
||||
<input type="text" id="inputArea" placeholder="默认示例,可自定义">
|
||||
<button id="applyBtn" class="primary">生成图解</button>
|
||||
<select id="exampleSelect"></select>
|
||||
<span style="flex:1"></span>
|
||||
<button id="prevBtn">◀ 上一步</button>
|
||||
<button id="nextBtn">下一步 ▶</button>
|
||||
<button id="jumpBtn">⏭ 跳到结果</button>
|
||||
<button id="autoBtn">自动播放</button>
|
||||
<button id="resetBtn">重置</button>
|
||||
</div>
|
||||
|
||||
<!-- 步骤流水线 -->
|
||||
<div class="pipeline" id="pipeline"></div>
|
||||
|
||||
<!-- 提示条 -->
|
||||
<div class="hint info" id="hintBox">
|
||||
<span id="stepInfo"></span><br>
|
||||
<span id="hintText"></span>
|
||||
</div>
|
||||
|
||||
<!-- 可视化面板 -->
|
||||
<div class="panels">
|
||||
<div class="panel" id="mainPanel">
|
||||
<h3>📊 可视化</h3>
|
||||
<div class="vis-area" id="vizArea">点击「生成图解」开始</div>
|
||||
</div>
|
||||
<div class="panel-grid">
|
||||
<div class="panel" id="detailPanel">
|
||||
<h3>📝 当前步骤详情</h3>
|
||||
<div id="detailContent">等待开始...</div>
|
||||
</div>
|
||||
<div class="panel" id="resultPanel">
|
||||
<h3>✅ 结果</h3>
|
||||
<div id="resultContent">等待完成...</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 代码 -->
|
||||
<div class="panel code-section">
|
||||
<h3>💻 参考代码(Python)</h3>
|
||||
<div id="codeArea"></div>
|
||||
</div>
|
||||
|
||||
<footer>Powered by QwenPaw · 图解算法 · LeetCode Hot 100</footer>
|
||||
</div>
|
||||
|
||||
<script src="../shared/algo-viz.js"></script>
|
||||
<script>
|
||||
"use strict";
|
||||
(function() {
|
||||
// ========== Algorithm Logic ==========
|
||||
|
||||
const examples = [
|
||||
{grid: [[2,1,1],[1,1,0],[0,1,1]], label: '示例1: 3×3'},
|
||||
{grid: [[2,1,1],[0,1,1],[1,0,1]], label: '示例2: 有不可达'},
|
||||
{grid: [[0,2]], label: '示例3: 无橘子'},
|
||||
];
|
||||
let grid, R, C, steps, stepCtrl;
|
||||
|
||||
function buildSteps(g) {
|
||||
grid = g.map(r=>[...r]); R = grid.length; C = grid[0].length;
|
||||
steps = [];
|
||||
const queue = [];
|
||||
let fresh = 0;
|
||||
for (let r=0; r<R; r++) for (let c=0; c<C; c++) {
|
||||
if (grid[r][c]===2) queue.push([r,c]);
|
||||
else if (grid[r][c]===1) fresh++;
|
||||
}
|
||||
steps.push({stage:'init', msg:`BFS多源最短路径:${queue.length}个腐烂源,${fresh}个新鲜橘子`, grid:grid.map(r=>[...r]), minutes:0, fresh});
|
||||
if (fresh === 0) { steps.push({stage:'done', msg:'没有新鲜橘子,返回0', grid:grid.map(r=>[...r]), minutes:0, fresh:0}); return; }
|
||||
let minutes = 0;
|
||||
const dirs = [[0,1],[0,-1],[1,0],[-1,0]];
|
||||
while (queue.length > 0 && fresh > 0) {
|
||||
const size = queue.length;
|
||||
const newlyRotten = [];
|
||||
for (let i=0; i<size; i++) {
|
||||
const [r,c] = queue.shift();
|
||||
for (const [dr,dc] of dirs) {
|
||||
const nr=r+dr, nc=c+dc;
|
||||
if (nr>=0 && nr<R && nc>=0 && nc<C && grid[nr][nc]===1) {
|
||||
grid[nr][nc] = 2;
|
||||
fresh--;
|
||||
queue.push([nr,nc]);
|
||||
newlyRotten.push([nr,nc]);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (newlyRotten.length > 0) {
|
||||
minutes++;
|
||||
steps.push({stage:'rot', msg:`第 ${minutes} 分钟:${newlyRotten.length}个橘子腐烂 (剩余${fresh}个新鲜)`, grid:grid.map(r=>[...r]), minutes, fresh, newlyRotten});
|
||||
}
|
||||
}
|
||||
if (fresh > 0) steps.push({stage:'impossible', msg:`仍有${fresh}个新鲜橘子无法腐烂`, grid:grid.map(r=>[...r]), minutes, fresh});
|
||||
else steps.push({stage:'done', msg:`所有橘子腐烂,用时 ${minutes} 分钟`, grid:grid.map(r=>[...r]), minutes, fresh:0});
|
||||
}
|
||||
|
||||
function render(step) {
|
||||
const s = steps[step];
|
||||
const hl = {};
|
||||
if (s.newlyRotten) s.newlyRotten.forEach(([r,c]) => { hl[r+','+c] = 'current'; });
|
||||
const cellStyle = (val,r,c) => {
|
||||
const key = r+','+c;
|
||||
if (hl[key]) return 'background:#fbbf24;color:#78350f;box-shadow:0 0 0 3px rgba(245,158,11,0.4);';
|
||||
if (val===2) return 'background:#fed7aa;color:#9a3412;';
|
||||
if (val===1) return 'background:#dcfce7;color:#166534;';
|
||||
return 'background:#f1f5f9;color:#94a3b8;';
|
||||
};
|
||||
let viz = renderGrid(s.grid, {highlights:hl, cellSize:52, cellStyle});
|
||||
viz += '<div style="margin-top:8px;display:flex;gap:16px;">';
|
||||
viz += '<span style="color:#9a3412;">🟠 腐烂</span>';
|
||||
viz += '<span style="color:#166534;">🟢 新鲜</span>';
|
||||
viz += '<span style="color:#94a3b8;">⬜ 空</span>';
|
||||
viz += `<span style="color:var(--blue);">⏱ 分钟=${s.minutes}</span>`;
|
||||
viz += '</div>';
|
||||
$('vizArea').innerHTML = viz;
|
||||
$('detailContent').innerHTML = '<div class="calc-block">' + s.msg + '</div>';
|
||||
if (s.stage==='done') $('resultContent').innerHTML = `<div class="final-answer">经过 <b>${s.minutes}</b> 分钟,所有橘子腐烂</div>`;
|
||||
if (s.stage==='impossible') $('resultContent').innerHTML = `<div class="final-answer" style="border-color:#f87171;background:#fef2f2;">返回 <b>-1</b>(有新鲜橘子无法腐烂)</div>`;
|
||||
$('hintText').textContent = s.msg;
|
||||
const stages = ['init→初始化','rot→腐烂传播','done→完成','impossible→不可能'];
|
||||
$('pipeline').innerHTML = stages.map(st => { const [k,l]=st.split('→'); return `<span class="pipe-step ${s.stage===k?'active':''}">${l}</span>`; }).join('<i>→</i>');
|
||||
}
|
||||
|
||||
function init() {
|
||||
const sel = $('exampleSelect');
|
||||
examples.forEach((e,i) => { sel.innerHTML += `<option value="${i}">${e.label}</option>`; });
|
||||
$('inputArea').value = '[[2,1,1],[1,1,0],[0,1,1]]';
|
||||
buildSteps(examples[0].grid);
|
||||
stepCtrl = new StepController({onStep: render, autoInterval:800});
|
||||
stepCtrl.setSteps(steps.map((_,i)=>i));
|
||||
$('stepInfo').textContent = '步骤 1 / ' + steps.length;
|
||||
stepCtrl.onStep = (idx) => { render(idx); $('stepInfo').textContent = `步骤 ${idx+1} / ${steps.length}`; };
|
||||
$('applyBtn').onclick = () => {
|
||||
try { const g = JSON.parse($('inputArea').value); buildSteps(g); stepCtrl.setSteps(steps.map((_,i)=>i)); render(0); $('stepInfo').textContent = '步骤 1 / ' + steps.length; }
|
||||
catch(e) { alert('请输入合法 JSON 二维数组'); }
|
||||
};
|
||||
$('exampleSelect').onchange = () => {
|
||||
const e = examples[parseInt($('exampleSelect').value)];
|
||||
buildSteps(e.grid); stepCtrl.setSteps(steps.map((_,i)=>i)); render(0);
|
||||
};
|
||||
$('prevBtn').onclick = () => stepCtrl.prev();
|
||||
$('nextBtn').onclick = () => stepCtrl.next();
|
||||
$('jumpBtn').onclick = () => stepCtrl.jumpToEnd();
|
||||
$('autoBtn').onclick = () => { const on = stepCtrl.toggleAuto(); $('autoBtn').textContent = on ? '暂停' : '自动播放'; };
|
||||
$('resetBtn').onclick = () => { stepCtrl.reset(); $('autoBtn').textContent = '自动播放'; };
|
||||
}
|
||||
init();
|
||||
$('codeArea').innerHTML = renderCode(`def orangesRotting(grid):
|
||||
rows, cols = len(grid), len(grid[0])
|
||||
queue = deque()
|
||||
fresh = 0
|
||||
for r in range(rows):
|
||||
for c in range(cols):
|
||||
if grid[r][c] == 2:
|
||||
queue.append((r, c))
|
||||
elif grid[r][c] == 1:
|
||||
fresh += 1
|
||||
minutes = 0
|
||||
while queue and fresh > 0:
|
||||
for _ in range(len(queue)):
|
||||
r, c = queue.popleft()
|
||||
for dr, dc in [(0,1),(0,-1),(1,0),(-1,0)]:
|
||||
nr, nc = r+dr, c+dc
|
||||
if 0<=nr<rows and 0<=nc<cols and grid[nr][nc]==1:
|
||||
grid[nr][nc] = 2
|
||||
fresh -= 1
|
||||
queue.append((nr, nc))
|
||||
minutes += 1
|
||||
return minutes if fresh == 0 else -1`, {lang:'Python'});
|
||||
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user