-
Notifications
You must be signed in to change notification settings - Fork 0
/
puzzle_heur.cpp
64 lines (57 loc) · 1.31 KB
/
puzzle_heur.cpp
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
#include <iostream>
#include <cstdlib>
#include <math.h>
#include "puzzle_heur.h"
using namespace std;
/** Out Of Place Heuristic Computation
* Computes the heuristic score OutOfPlace Style
* @param *tiles Derefences the tiles array you want to use
* @param size The size of the tiles array */
int OutOfPlaceHeuristic::compute(int *tiles, int size)
{
int heuristic=0;
for(int i=0; i<size; i++)
{
//if there is a tile out of place, add one to heuristic
if(tiles[i] != i)
{
heuristic++;
}
}
return heuristic;
}
/** Manhattan Heuristic Computation
* Computes the heuristic score Manhattan Style
* @param *tiles Derefences the tiles array you want to use
* @param size The size of the tiles array */
int ManhattanHeuristic::compute(int *tiles, int size)
{
int iRow;
int iColumn;
int jRow;
int jColumn;
int dim = sqrt(size);
int heuristic=0;
//Searching through array looking for any number out of place
for(int i=0; i<size; i++)
{
if(tiles[i] != i)
{
//Calculate current row and column of incorrect place
iRow = i/dim;
iColumn = i%dim;
//Find correct row and column
for(int j=0; j<size; j++)
{
if(j == tiles[i])
{
jRow = j/dim;
jColumn = j%dim;
//adding heuristic up
heuristic+= abs(iRow-jRow) + abs(iColumn-jColumn);
}
}
}
}
return heuristic;
}