-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.cpp
214 lines (189 loc) · 5.22 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
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
//
// Created by tomer on 1/13/19.
//
#include "Matrix.h"
Matrix::Matrix(int **matrix, int row, int col, Node *start, Node *goal) {
this->matrix=matrix;
this->row=row;
this->col=col;
this->start=start;
this->goal=goal;
}
Matrix::Matrix(int **matrix, int row, int col, int i_start, int j_start, int i_goal, int j_goal) {
this->matrix=matrix;
this->row=row;
this->col=col;
start= new Node(i_start,j_start);
goal= new Node(i_goal,j_goal);
}
Matrix::Matrix(int **matrix, int row, int col) {
this->matrix=matrix;
this->row=row;
this->col=col;
start = new Node(0,0);
goal = new Node (row-1,col-1);
}
State<Node> *Matrix::stateGenarator(int i, int j, State<Node> *pre) {
//first of all we check if there is an edge
if(matrix[i][j]==-1){
return nullptr;
}
Node *myNode= new Node(i,j);
double weight;
//double pre_weight=0;
if(pre== nullptr){
weight= matrix[i][j];
}
else{
weight= pre->getWeight() + matrix[i][j];
}
State<Node>* ans= new State<Node>(myNode,weight,pre);
return ans;
}
Matrix::~Matrix() {
delete (start);
delete (goal);
}
State<Node> *Matrix::getInitialState() {
return stateGenarator(start->i,start->j, nullptr);
}
set<State<Node> *> Matrix::getAllPossibleStates(State<Node>* currState) {
//current state
Node *location= currState->getState();
int i= location->i;
int j= location->j;
//node that will be inserted to set
State<Node>* insert_toSet;
//this is the set that will be returned with all possible states
set<State<Node>*> toReturn;
if(i!=row-1){
insert_toSet = stateGenarator(i+1,j,currState);
if(insert_toSet != nullptr)
toReturn.insert(insert_toSet);
}
if(j!=col-1){
insert_toSet = stateGenarator(i, j+1, currState);
if(insert_toSet != nullptr)
toReturn.insert(insert_toSet);
}
if(i!=0){
insert_toSet = stateGenarator(i-1, j, currState);
if(insert_toSet != nullptr)
toReturn.insert(insert_toSet);
}
if(j!=0){
insert_toSet=(i,j-1,currState);
if(insert_toSet != nullptr)
toReturn.insert(insert_toSet);
}
return toReturn;
}
bool Matrix::isGoal(State<Node>* currState) {
bool ans= false;
//get current state
Node *location= currState->getState();
if(*location == *goal)
ans=true;
return ans;
}
string Matrix::getStrRepresentation() {
string rep;
for (int i = 0; i < this->row; i++) {
for (int j = 0; j < this->col; j++) {
rep += this->matrix[i][j];
rep += " ";
rep += ",";
}
rep += "\n";
}
rep += "\n";
rep += to_string(this->start->i) += to_string(this->start->j) += "\n";
rep += to_string(this->goal->i) += to_string(this->goal->j) += "\n";
return rep;
}
/*
Matrix::Matrix(int **matrix, int row, int col, Node *start, Node *goal) {
this->matrix=matrix;
this->row=row;
this->col=col;
this->start=start;
this->goal=goal;
}
Matrix::Matrix(int **matrix, int row, int col, int i_start, int j_start, int i_goal, int j_goal) {
this->matrix=matrix;
this->row=row;
this->col=col;
start= new Node(i_start,j_start);
goal= new Node(i_goal,j_goal);
}
Matrix::Matrix(int **matrix, int row, int col) {
this->matrix=matrix;
this->row=row;
this->col=col;
start = new Node(0,0);
goal = new Node (row-1,col-1);
}
State<Node> *Matrix::stateGenarator(int i, int j, State<Node> *pre) {
//first of all we check if there is an edge
if(matrix[i][j]==-1){
return nullptr;
}
Node *myNode= new Node(i,j);
double weight;
//double pre_weight=0;
if(pre== nullptr){
weight= matrix[i][j];
}
else{
weight= pre->getWeight() + matrix[i][j];
}
State<Node>* ans= new State<Node>(myNode,weight,pre);
return ans;
}
Matrix::~Matrix() {
delete (start);
delete (goal);
}
State<Node> *Matrix::getInitialState() {
return stateGenarator(start->i,start->j, nullptr);
}
set<State<Node> *> Matrix::getAllPossibleStates(State<Node>* currState) {
//current state
Node *location= currState->getState();
int i= location->i;
int j= location->j;
//node that will be inserted to set
State<Node>* insert_toSet;
//this is the set that will be returned with all possible states
set<State<Node>*> toReturn;
if(i!=row-1){
insert_toSet = stateGenarator(i+1,j,currState);
if(insert_toSet != nullptr)
toReturn.insert(insert_toSet);
}
if(j!=col-1){
insert_toSet = stateGenarator(i, j+1, currState);
if(insert_toSet != nullptr)
toReturn.insert(insert_toSet);
}
if(i!=0){
insert_toSet = stateGenarator(i-1, j, currState);
if(insert_toSet != nullptr)
toReturn.insert(insert_toSet);
}
if(j!=0){
insert_toSet=(i,j-1,currState);
if(insert_toSet != nullptr)
toReturn.insert(insert_toSet);
}
return toReturn;
}
bool Matrix::isGoal(State<Node>* currState) {
bool ans= false;
//get current state
Node *location= currState->getState();
if(*location == *goal)
ans=true;
return ans;
}
*/