-
Notifications
You must be signed in to change notification settings - Fork 2
/
MazeWanderingEasy.cpp
223 lines (210 loc) · 5.16 KB
/
MazeWanderingEasy.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
215
216
217
218
219
220
221
222
223
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define x first
#define y second
#define LL long long
using namespace std;
typedef pair<int, int> per;
typedef vector<int>::iterator it;
typedef set<int>::iterator is;
queue<per> c;
const int dx[]={ 0,1, 0,-1},
dy[]={ 1,0,-1, 0};
int nrd[55][55];
struct MazeWanderingEasy {
int decisions(vector <string> mt) {
int rez,n=mt.size(),m=mt[0].size();
per start,goal;
for(int i=0; i<n; ++i) for(int j=0; j<m; ++j) {
nrd[i][j]=m*n;
if(mt[i][j]=='M') start=make_pair(i,j);
if(mt[i][j]=='*') goal=make_pair(i,j);
}
nrd[start.x][start.y]=0;
for(c.push(start);c.size();c.pop()) {
per fr=c.front();
int nrv=0;
for(int d=0; d<4; ++d) {
int ii=fr.x+dx[d],jj=fr.y+dy[d];
if(ii>=0 && jj>=0 && ii<n && jj<m &&
'.'==mt[ii][jj] && n*m==nrd[ii][jj]) ++nrv;
}
for(int d=0; d<4; ++d) {
int ii=fr.x+dx[d],jj=fr.y+dy[d];
if(ii>=0 && jj>=0 && ii<n && jj<m &&
'.'==mt[ii][jj] && n*m==nrd[ii][jj]) {
nrd[ii][jj]=nrd[fr.x][fr.y]+(nrv>1);
c.push(make_pair(ii,jj));
}
}
}
return nrd[goal.x][goal.y];
}
};
double test0() {
string t0[] = {"*.M"};
vector <string> p0(t0, t0+sizeof(t0)/sizeof(string));
MazeWanderingEasy * obj = new MazeWanderingEasy();
clock_t start = clock();
int my_answer = obj->decisions(p0);
clock_t end = clock();
delete obj;
cout <<"Time: " <<(double)(end-start)/CLOCKS_PER_SEC <<" seconds" <<endl;
int p1 = 0;
cout <<"Desired answer: " <<endl;
cout <<"\t" << p1 <<endl;
cout <<"Your answer: " <<endl;
cout <<"\t" << my_answer <<endl;
if (p1 != my_answer) {
cout <<"DOESN'T MATCH!!!!" <<endl <<endl;
return -1;
}
else {
cout <<"Match :-)" <<endl <<endl;
return (double)(end-start)/CLOCKS_PER_SEC;
}
}
double test1() {
string t0[] = {"*.M",
".X."};
vector <string> p0(t0, t0+sizeof(t0)/sizeof(string));
MazeWanderingEasy * obj = new MazeWanderingEasy();
clock_t start = clock();
int my_answer = obj->decisions(p0);
clock_t end = clock();
delete obj;
cout <<"Time: " <<(double)(end-start)/CLOCKS_PER_SEC <<" seconds" <<endl;
int p1 = 1;
cout <<"Desired answer: " <<endl;
cout <<"\t" << p1 <<endl;
cout <<"Your answer: " <<endl;
cout <<"\t" << my_answer <<endl;
if (p1 != my_answer) {
cout <<"DOESN'T MATCH!!!!" <<endl <<endl;
return -1;
}
else {
cout <<"Match :-)" <<endl <<endl;
return (double)(end-start)/CLOCKS_PER_SEC;
}
}
double test2() {
string t0[] = {"...",
"XMX",
"..*"};
vector <string> p0(t0, t0+sizeof(t0)/sizeof(string));
MazeWanderingEasy * obj = new MazeWanderingEasy();
clock_t start = clock();
int my_answer = obj->decisions(p0);
clock_t end = clock();
delete obj;
cout <<"Time: " <<(double)(end-start)/CLOCKS_PER_SEC <<" seconds" <<endl;
int p1 = 2;
cout <<"Desired answer: " <<endl;
cout <<"\t" << p1 <<endl;
cout <<"Your answer: " <<endl;
cout <<"\t" << my_answer <<endl;
if (p1 != my_answer) {
cout <<"DOESN'T MATCH!!!!" <<endl <<endl;
return -1;
}
else {
cout <<"Match :-)" <<endl <<endl;
return (double)(end-start)/CLOCKS_PER_SEC;
}
}
double test3() {
string t0[] = {".X.X......X",
".X*.X.XXX.X",
".XX.X.XM...",
"......XXXX."};
vector <string> p0(t0, t0+sizeof(t0)/sizeof(string));
MazeWanderingEasy * obj = new MazeWanderingEasy();
clock_t start = clock();
int my_answer = obj->decisions(p0);
clock_t end = clock();
delete obj;
cout <<"Time: " <<(double)(end-start)/CLOCKS_PER_SEC <<" seconds" <<endl;
int p1 = 3;
cout <<"Desired answer: " <<endl;
cout <<"\t" << p1 <<endl;
cout <<"Your answer: " <<endl;
cout <<"\t" << my_answer <<endl;
if (p1 != my_answer) {
cout <<"DOESN'T MATCH!!!!" <<endl <<endl;
return -1;
}
else {
cout <<"Match :-)" <<endl <<endl;
return (double)(end-start)/CLOCKS_PER_SEC;
}
}
double test4() {
string t0[] = {"..........*",
".XXXXXXXXXX",
"...........",
"XXXXXXXXXX.",
"M.........."};
vector <string> p0(t0, t0+sizeof(t0)/sizeof(string));
MazeWanderingEasy * obj = new MazeWanderingEasy();
clock_t start = clock();
int my_answer = obj->decisions(p0);
clock_t end = clock();
delete obj;
cout <<"Time: " <<(double)(end-start)/CLOCKS_PER_SEC <<" seconds" <<endl;
int p1 = 0;
cout <<"Desired answer: " <<endl;
cout <<"\t" << p1 <<endl;
cout <<"Your answer: " <<endl;
cout <<"\t" << my_answer <<endl;
if (p1 != my_answer) {
cout <<"DOESN'T MATCH!!!!" <<endl <<endl;
return -1;
}
else {
cout <<"Match :-)" <<endl <<endl;
return (double)(end-start)/CLOCKS_PER_SEC;
}
}
int main() {
int time;
bool errors = false;
time = test0();
if (time < 0)
errors = true;
time = test1();
if (time < 0)
errors = true;
time = test2();
if (time < 0)
errors = true;
time = test3();
if (time < 0)
errors = true;
time = test4();
if (time < 0)
errors = true;
if (!errors)
cout <<"You're a stud (at least on the example cases)!" <<endl;
else
cout <<"Some of the test cases had errors." <<endl;
}
//Powered by [KawigiEdit] 2.0!