-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgreedy.html
More file actions
518 lines (462 loc) · 21.3 KB
/
Copy pathgreedy.html
File metadata and controls
518 lines (462 loc) · 21.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>贪心算法可视化 - 算法可视化工具</title>
<!-- Tailwind CSS -->
<script src="https://cdn.tailwindcss.com"></script>
<!-- Google Fonts -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@300;400;500;700;900&family=JetBrains+Mono:wght@400;500;600&display=swap" rel="stylesheet">
<!-- Core Libraries -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/echarts@5.4.3/dist/echarts.min.js"></script>
<style>
:root {
--bg-primary: #1a1a2e;
--bg-secondary: #16213e;
--accent-cyan: #00d4ff;
--accent-orange: #ff6b35;
--accent-green: #4ecdc4;
--text-primary: #e0e0e0;
--text-secondary: #b0b0b0;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Noto Sans SC', sans-serif;
background: var(--bg-primary);
color: var(--text-primary);
overflow-x: hidden;
}
.gradient-text {
background: linear-gradient(135deg, var(--accent-cyan), var(--accent-green));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
}
.code-font {
font-family: 'JetBrains Mono', monospace;
}
.nav-link {
position: relative;
transition: color 0.3s ease;
}
.nav-link::after {
content: '';
position: absolute;
bottom: -4px;
left: 0;
width: 0;
height: 2px;
background: var(--accent-cyan);
transition: width 0.3s ease;
}
.nav-link:hover::after {
width: 100%;
}
.activity-item {
display: flex;
align-items: center;
justify-content: space-between;
padding: 12px 16px;
margin: 8px 0;
background: var(--bg-secondary);
border: 2px solid #4b5563;
border-radius: 8px;
transition: all 0.3s ease;
position: relative;
}
.activity-item:hover {
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(0, 212, 255, 0.2);
}
.activity-item.selected {
background: var(--accent-green) !important;
border-color: var(--accent-green) !important;
color: white;
transform: scale(1.02);
}
.activity-item.current {
background: var(--accent-orange) !important;
border-color: var(--accent-orange) !important;
color: white;
transform: scale(1.05);
box-shadow: 0 0 20px rgba(255, 107, 53, 0.3);
}
.activity-item.conflict {
background: #ef4444 !important;
border-color: #ef4444 !important;
color: white;
opacity: 0.7;
}
.coin-item {
display: inline-block;
width: 60px;
height: 60px;
margin: 8px;
background: linear-gradient(135deg, #fbbf24, #f59e0b);
border: 3px solid #d97706;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-weight: bold;
font-size: 14px;
color: #92400e;
transition: all 0.3s ease;
cursor: pointer;
}
.coin-item:hover {
transform: scale(1.1);
box-shadow: 0 0 20px rgba(251, 191, 36, 0.5);
}
.coin-item.selected {
background: linear-gradient(135deg, var(--accent-green), #059669) !important;
border-color: #047857 !important;
color: white;
transform: scale(1.15);
}
.coin-item.used {
background: #6b7280 !important;
border-color: #4b5563 !important;
opacity: 0.5;
cursor: not-allowed;
}
.huffman-node {
display: inline-block;
padding: 8px 16px;
margin: 4px;
background: var(--bg-secondary);
border: 2px solid var(--accent-cyan);
border-radius: 8px;
font-weight: bold;
transition: all 0.3s ease;
position: relative;
}
.huffman-node.leaf {
background: var(--accent-green);
color: white;
}
.huffman-node.internal {
background: var(--accent-orange);
color: white;
}
.huffman-node.selected {
transform: scale(1.1);
box-shadow: 0 0 20px rgba(0, 212, 255, 0.5);
}
.control-button {
transition: all 0.3s ease;
}
.control-button:hover {
transform: translateY(-2px);
box-shadow: 0 8px 25px rgba(0, 212, 255, 0.3);
}
.slider {
-webkit-appearance: none;
appearance: none;
height: 6px;
border-radius: 3px;
background: #374151;
outline: none;
}
.slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 20px;
height: 20px;
border-radius: 50%;
background: var(--accent-cyan);
cursor: pointer;
box-shadow: 0 0 10px rgba(0, 212, 255, 0.5);
}
.performance-card {
background: rgba(22, 33, 62, 0.8);
backdrop-filter: blur(10px);
border: 1px solid rgba(0, 212, 255, 0.2);
}
.algorithm-select {
background: var(--bg-secondary);
border: 1px solid rgba(0, 212, 255, 0.3);
color: var(--text-primary);
}
.algorithm-select:focus {
border-color: var(--accent-cyan);
box-shadow: 0 0 0 2px rgba(0, 212, 255, 0.2);
}
.step-indicator {
display: flex;
align-items: center;
margin-bottom: 8px;
padding: 8px 12px;
background: rgba(22, 33, 62, 0.5);
border-radius: 8px;
border-left: 4px solid var(--accent-cyan);
}
.tree-container {
display: flex;
justify-content: center;
align-items: center;
min-height: 300px;
position: relative;
}
.tree-node {
position: absolute;
background: var(--bg-secondary);
border: 2px solid var(--accent-cyan);
border-radius: 8px;
padding: 8px 12px;
font-weight: bold;
transition: all 0.3s ease;
}
.tree-edge {
position: absolute;
background: #6b7280;
transition: all 0.3s ease;
}
.frequency-bar {
height: 20px;
background: linear-gradient(to right, var(--accent-cyan), var(--accent-green));
border-radius: 10px;
margin: 4px 0;
transition: all 0.3s ease;
position: relative;
}
.frequency-label {
position: absolute;
left: 8px;
top: 50%;
transform: translateY(-50%);
font-size: 12px;
font-weight: bold;
color: white;
}
</style>
</head>
<body>
<!-- Navigation -->
<nav class="fixed top-0 left-0 right-0 z-50 bg-gray-900/90 backdrop-blur-md border-b border-gray-800">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="flex items-center justify-between h-16">
<div class="flex items-center space-x-2">
<div class="w-8 h-8 bg-gradient-to-r from-cyan-400 to-green-400 rounded-lg flex items-center justify-center">
<span class="text-white font-bold text-sm">AV</span>
</div>
<span class="text-xl font-bold gradient-text">算法可视化</span>
</div>
<div class="hidden md:flex items-center space-x-8">
<a href="index.html" class="nav-link text-gray-300 hover:text-white">首页</a>
<a href="sorting.html" class="nav-link text-gray-300 hover:text-white">排序算法</a>
<a href="search.html" class="nav-link text-gray-300 hover:text-white">搜索算法</a>
<a href="graph.html" class="nav-link text-gray-300 hover:text-white">图论算法</a>
<a href="dp.html" class="nav-link text-gray-300 hover:text-white">动态规划</a>
<a href="string.html" class="nav-link text-gray-300 hover:text-white">字符串算法</a>
<a href="greedy.html" class="nav-link text-cyan-400 font-semibold">贪心算法</a>
</div>
<button class="md:hidden text-gray-300 hover:text-white">
<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"></path>
</svg>
</button>
</div>
</div>
</nav>
<!-- Main Content -->
<div class="pt-16 min-h-screen bg-gradient-to-br from-gray-900 via-gray-800 to-gray-900">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<!-- Header -->
<div class="text-center mb-8">
<h1 class="text-4xl font-black gradient-text mb-4">贪心算法可视化</h1>
<p class="text-xl text-gray-300 max-w-3xl mx-auto">
通过局部最优选择理解贪心算法的核心思想
</p>
</div>
<!-- Control Panel -->
<div class="bg-gray-800/50 backdrop-blur-sm rounded-2xl p-6 mb-8 border border-gray-700">
<div class="grid md:grid-cols-5 gap-6">
<!-- Algorithm Selection -->
<div>
<label class="block text-sm font-medium text-gray-300 mb-2">选择算法</label>
<select id="algorithmSelect" class="algorithm-select w-full px-4 py-2 rounded-lg focus:outline-none">
<option value="activity-selection">活动选择问题</option>
<option value="coin-change">硬币找零问题</option>
<option value="huffman">哈夫曼编码</option>
<option value="job-scheduling">任务调度问题</option>
</select>
</div>
<!-- Problem Size -->
<div>
<label class="block text-sm font-medium text-gray-300 mb-2">问题规模: <span id="problemSizeValue">8</span></label>
<input type="range" id="problemSize" min="5" max="15" value="8" class="slider w-full">
</div>
<!-- Additional Parameters -->
<div id="additionalParams">
<label class="block text-sm font-medium text-gray-300 mb-2">目标金额: <span id="targetAmountValue">47</span></label>
<input type="range" id="targetAmount" min="20" max="100" value="47" class="slider w-full">
</div>
<!-- Speed -->
<div>
<label class="block text-sm font-medium text-gray-300 mb-2">动画速度: <span id="speedValue">5</span></label>
<input type="range" id="speed" min="1" max="10" value="5" class="slider w-full">
</div>
<!-- Control Buttons -->
<div class="flex items-end space-x-2">
<button id="generateProblem" class="control-button px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg font-medium">
生成问题
</button>
<button id="startAlgorithm" class="control-button px-4 py-2 bg-green-600 hover:bg-green-700 text-white rounded-lg font-medium">
开始求解
</button>
<button id="stopAlgorithm" class="control-button px-4 py-2 bg-red-600 hover:bg-red-700 text-white rounded-lg font-medium">
停止
</button>
</div>
</div>
</div>
<!-- Visualization Area -->
<div class="grid lg:grid-cols-4 gap-8">
<!-- Main Visualization -->
<div class="lg:col-span-3">
<div class="bg-gray-800/50 backdrop-blur-sm rounded-2xl p-6 border border-gray-700">
<div class="flex items-center justify-between mb-4">
<h3 class="text-xl font-bold">贪心选择可视化</h3>
<div class="flex items-center space-x-4 text-sm">
<div class="flex items-center space-x-2">
<div class="w-4 h-4 bg-orange-500 rounded"></div>
<span>当前选择</span>
</div>
<div class="flex items-center space-x-2">
<div class="w-4 h-4 bg-green-500 rounded"></div>
<span>已选择</span>
</div>
<div class="flex items-center space-x-2">
<div class="w-4 h-4 bg-red-500 rounded"></div>
<span>冲突/跳过</span>
</div>
</div>
</div>
<!-- Problem Specific Visualization -->
<div id="problemVisualization" class="min-h-64 bg-gray-900/50 rounded-lg p-4 mb-4">
<!-- Problem-specific visualization will be shown here -->
</div>
<!-- Additional Visualization -->
<div id="additionalVisualization" class="bg-gray-900/50 rounded-lg p-4 mb-4">
<!-- Additional visualization elements -->
</div>
<!-- Algorithm Steps -->
<div class="bg-gray-900/50 rounded-lg p-4">
<h4 class="font-semibold mb-3">算法步骤</h4>
<div id="algorithmSteps" class="space-y-2 max-h-32 overflow-y-auto">
<div class="text-gray-400">点击"开始求解"查看详细的贪心选择步骤...</div>
</div>
</div>
</div>
</div>
<!-- Performance Panel -->
<div class="space-y-6">
<!-- Statistics -->
<div class="performance-card rounded-2xl p-6">
<h3 class="text-lg font-bold mb-4">统计信息</h3>
<div class="space-y-4">
<div class="flex justify-between items-center">
<span class="text-gray-300">选择次数</span>
<span id="selectionCount" class="text-cyan-400 font-bold">0</span>
</div>
<div class="flex justify-between items-center">
<span class="text-gray-300">最终结果</span>
<span id="finalResult" class="text-orange-400 font-bold">-</span>
</div>
<div class="flex justify-between items-center">
<span class="text-gray-300">执行时间</span>
<span id="executionTime" class="text-green-400 font-bold">0ms</span>
</div>
<div class="flex justify-between items-center">
<span class="text-gray-300">问题规模</span>
<span id="currentProblemSize" class="text-purple-400 font-bold">8</span>
</div>
</div>
</div>
<!-- Time Complexity -->
<div class="performance-card rounded-2xl p-6">
<h3 class="text-lg font-bold mb-4">时间复杂度</h3>
<div class="space-y-3">
<div class="flex justify-between items-center">
<span class="text-gray-300">当前算法</span>
<span id="currentComplexity" class="code-font text-cyan-400">O(n log n)</span>
</div>
<div class="flex justify-between items-center">
<span class="text-gray-300">空间复杂度</span>
<span id="spaceComplexity" class="code-font text-yellow-400">O(1)</span>
</div>
</div>
</div>
<!-- Algorithm Info -->
<div class="performance-card rounded-2xl p-6">
<h3 class="text-lg font-bold mb-4">算法信息</h3>
<div class="space-y-3">
<div class="flex justify-between items-center">
<span class="text-gray-300">求解状态</span>
<span id="solutionStatus" class="text-gray-400">就绪</span>
</div>
<div class="flex justify-between items-center">
<span class="text-gray-300">算法类型</span>
<span id="algorithmType" class="text-cyan-400">贪心算法</span>
</div>
</div>
</div>
<!-- Greedy Characteristics -->
<div class="performance-card rounded-2xl p-6">
<h3 class="text-lg font-bold mb-4">贪心特性</h3>
<div class="space-y-2 text-sm">
<div class="flex items-center space-x-2">
<div class="w-2 h-2 bg-green-400 rounded-full"></div>
<span class="text-gray-300">贪心选择</span>
</div>
<div class="flex items-center space-x-2">
<div class="w-2 h-2 bg-green-400 rounded-full"></div>
<span class="text-gray-300">最优子结构</span>
</div>
<div class="flex items-center space-x-2">
<div class="w-2 h-2 bg-green-400 rounded-full"></div>
<span class="text-gray-300">不可回溯</span>
</div>
</div>
</div>
</div>
</div>
<!-- Algorithm Code -->
<div class="mt-8 bg-gray-800/50 backdrop-blur-sm rounded-2xl p-6 border border-gray-700">
<h3 class="text-xl font-bold mb-4">算法代码</h3>
<div class="bg-gray-900/50 rounded-lg p-4 overflow-x-auto">
<pre id="algorithmCode" class="code-font text-sm text-gray-300"><code>// 选择算法查看代码实现
</code></pre>
</div>
</div>
</div>
</div>
<!-- Footer -->
<footer class="bg-gray-900 border-t border-gray-800 py-8">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="text-center">
<p class="text-gray-400">
© 2025 Kaleb. 让算法学习变得简单有趣.
</p>
</div>
</div>
</footer>
<script src="main.js"></script>
<script src="greedy-algorithms.js"></script>
<script>
// Initialize greedy page
document.addEventListener('DOMContentLoaded', function() {
initializeGreedyPage();
});
</script>
</body>
</html>