-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.cpp
138 lines (128 loc) · 3.17 KB
/
Matrix.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
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
#include <cstdlib>
#include <iostream>
#include "Matrix.h"
#include "State.h"
State<int>* Matrix::getInitialState() {
auto s = this->matrix[0];
return s;
}
bool Matrix::isGoal(State<int>* state) {
return *state == State<int>(size * size - 1);
}
bool Matrix::operator==(Matrix &m) {
if (m.GetN() != this->size) {
return false;
}
int i, j;
for (i = 0; i < this->size; i++) {
for (j = 0; j < this->size; j++) {
if (this->getElement(i, j) != m.getElement(i, j)) {
return false;
}
}
}
return true;
}
Matrix::Matrix(double *mat,int n) {
this->size = n;
this->matrix = new State<int>*[size*size];
for (int kI = 0; kI < size; ++kI) {
int p = kI*size;
for (int kJ = 0; kJ < size ; ++kJ) {
auto s = new State<int>(p+kJ);
s->SetCost(mat[p+kJ]);
matrix[p+kJ] = s;
}
}
}
int Matrix::GetN() const {
return size;
}
State<int>* Matrix::getElement(int i, int j) {
if (i >= this->size || j >= this->size) {
return nullptr;
}
return this->matrix[i * size + j];
}
Matrix::~Matrix() {
//free(matrix);
//free(this);
}
std::list<State<int>*> Matrix::getNeighborStates(State<int>* s) {
std::list<State<int>*> list;
int position = s->GetState();
int last_pos = size*size-1;
// left
if (position%size >= 1){
auto state = this->matrix[position - 1];
if (state->GetCost() != -1){
list.push_back(state);
}
}
// right
if (position % size <= this->size - 2){
auto state = this->matrix[position + 1];
if (state->GetCost() != -1){
list.push_back(state);
}
}
// up
if (position % size != position){
auto state = this->matrix[position - size];
if (state->GetCost() != -1){
list.push_back(state);
}
}
//down
if (position + size <= last_pos){
auto state = this->matrix[position + size];
if (state->GetCost() != -1){
list.push_back(state);
}
}
/*if (s.GetState() % size != size - 1) {
State<int> state_ = State<int>(s.GetState() + 1);
state_.SetCost(matrix[s.GetState() + 1]);
if (state_.GetCost() != -1){
list.push_back(state_);
}
}
if (s.GetState() % size != 0) {
State<int> state_ = State<int>(s.GetState() - 1);
state_.SetCost(matrix[s.GetState() - 1]);
if (state_.GetCost() != -1){
list.push_back(state_);
}
}
if (s.GetState() / size != 0) {
State<int> state_ = State<int>(s.GetState() - size);
state_.SetCost(matrix[s.GetState() - size]);
if (state_.GetCost() != -1){
list.push_back(state_);
}
}
if (s.GetState() / size != size - 1) {
State<int> state_ = State<int>(s.GetState() + size);
state_.SetCost(matrix[s.GetState() + size]);
if (state_.GetCost() != -1){
list.push_back(state_);
}
}*/
return list;
}
void Matrix::reset() {
for (int kI = 0; kI < this->size; ++kI) {
int p = kI * this->size;
for (int kJ = 0; kJ < this->size; ++kJ) {
State<int>* s =this->matrix[p+kJ];
s->SetCameFrom(nullptr);
s->SetShortestPathCost(-1);
}
}
State<int>* start = this->getInitialState();
start->SetCameFrom(start);
start->SetShortestPathCost(0);
}
State<int> *Matrix::getGoalState() {
return getElement(size-1,size-1);
}