-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid.cpp
More file actions
70 lines (47 loc) · 1.74 KB
/
grid.cpp
File metadata and controls
70 lines (47 loc) · 1.74 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
// Dehowe Feng, Nathan Suh
#include "grid.h"
#include "PowerMain.h"
#include <iostream>
#include <cmath>
#include "QueueAr.h"
//Notes:
// blocks is the array that is being stored in.
// adjBlocks is the array of blocks that are adjacent
// losses is the array of losses of connections.
// previous holds "previous" block of a block, i.e what block will point to that block.
//Nathan: check, put number in heap
using namespace std;
Grid::Grid(int gridSize, const Block *blocks, int previous[])
{
Blockdata *blocksdata = new Blockdata[gridSize];
Queue<Blockdata> PowerLine(gridSize*10);
for(int i = 0; i < gridSize ; i++)
{
blocksdata[i].blocks = blocks[i];
blocksdata[i].distance = INFINITY;
blocksdata[i].identity = i;
}
for (int i = 9; i > 0 ; i--)
{
blocksdata[i].distance = 0;
PowerLine.enqueue(blocksdata[i]);
}
while(!PowerLine.isEmpty())
{
Blockdata b = PowerLine.dequeue();
for(int i = 0; i < b.blocks.edgeCount; i++)
{
if(b.distance + b.blocks.losses[i] < blocksdata[b.blocks.adjBlocks[i]].distance)
{
if(blocksdata[b.blocks.adjBlocks[i]].identity > 9)
{
blocksdata[b.blocks.adjBlocks[i]].distance = b.distance + b.blocks.losses[i];
previous[b.blocks.adjBlocks[i]] = b.identity;
}
else
blocksdata[b.blocks.adjBlocks[i]].distance = 0;
PowerLine.enqueue(blocksdata[b.blocks.adjBlocks[i]]);
}
}
}
} // Grid()