-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGreedy.cs
More file actions
52 lines (50 loc) · 1.86 KB
/
Greedy.cs
File metadata and controls
52 lines (50 loc) · 1.86 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
using hascode;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace hascode
{
class Greedy : IStrategy
{
public Slideshow execute(PictureStore pictureStore)
{
var slides = createSlides(pictureStore);
var orderedSlides = new LinkedList<ISlide>(slides.OrderByDescending(s1 => s1.GetTags().Count));
Slideshow result = new Slideshow() { Slides = orderedSlides.Take(1).ToList() };
orderedSlides.RemoveFirst();
while (orderedSlides.Any())
{
var prev = result.Slides.Last();
LinkedListNode<ISlide> bestNode = orderedSlides.First;
LinkedListNode<ISlide> node = orderedSlides.First;
var bestScoreSoFar = prev.GetScoreWith(bestNode.Value);
int ahead = 0;
while (node != null)
{
if (ahead++ > 50) break;
if (node.Value.GetTags().Count / 2 <= bestScoreSoFar) break;
var nextNode = node.Next;
var score = prev.GetScoreWith(node.Value);
if (score > bestScoreSoFar)
{
bestScoreSoFar = score;
bestNode = node;
}
node = nextNode;
}
result.Slides.Add(bestNode.Value);
orderedSlides.Remove(bestNode);
}
return result;
}
public List<ISlide> createSlides(PictureStore store)
{
var ss = new List<ISlide>();
foreach(var pic in store.HorizontalPictures)
ss.Add( new HorizontalSlide() {Picture=pic});
ss.AddRange(new VerticalCombiner().MatchVerticalPictures(store.VerticalPictures));
return ss;
}
}
}