-
Notifications
You must be signed in to change notification settings - Fork 4
/
BunnyPuzzle.cpp
244 lines (231 loc) · 5.1 KB
/
BunnyPuzzle.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <bits/stdc++.h>
using namespace std;
class BunnyPuzzle
{
public:
int theCount(vector <int> bunnies);
};
int BunnyPuzzle::theCount (vector <int> bunnies)
{
int ret = 0;
int N = bunnies.size();
for(int i = 0; i < (int)N; ++i) {
if(i < N-1) {
int a = bunnies[i], b = bunnies[i+1];
if(i+2 >= N || bunnies[i+2] > 2*b-a)
ret++;
}
if(i > 0) {
int a = bunnies[i], b = bunnies[i-1];
if(i-2 < 0 || bunnies[i-2] < 2*b-a)
ret++;
}
}
return ret;
}
// BEGIN KAWIGIEDIT TESTING
// Generated by KawigiEdit-pf 2.3.0
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cmath>
using namespace std;
bool KawigiEdit_RunTest(int testNum, vector <int> p0, bool hasAnswer, int p1) {
cout << "Test " << testNum << ": [" << "{";
for (int i = 0; int(p0.size()) > i; ++i) {
if (i > 0) {
cout << ",";
}
cout << p0[i];
}
cout << "}";
cout << "]" << endl;
BunnyPuzzle *obj;
int answer;
obj = new BunnyPuzzle();
clock_t startTime = clock();
answer = obj->theCount(p0);
clock_t endTime = clock();
delete obj;
bool res;
res = true;
cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
if (hasAnswer) {
cout << "Desired answer:" << endl;
cout << "\t" << p1 << endl;
}
cout << "Your answer:" << endl;
cout << "\t" << answer << endl;
if (hasAnswer) {
res = answer == p1;
}
if (!res) {
cout << "DOESN'T MATCH!!!!" << endl;
} else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
cout << "FAIL the timeout" << endl;
res = false;
} else if (hasAnswer) {
cout << "Match :-)" << endl;
} else {
cout << "OK, but is it right?" << endl;
}
cout << "" << endl;
return res;
}
int main() {
bool all_right;
bool disabled;
bool tests_disabled;
all_right = true;
tests_disabled = false;
vector <int> p0;
int p1;
// ----- test 0 -----
disabled = false;
p0 = {5,8};
p1 = 2;
all_right = (disabled || KawigiEdit_RunTest(0, p0, true, p1) ) && all_right;
tests_disabled = tests_disabled || disabled;
// ------------------
// ----- test 1 -----
disabled = false;
p0 = {-1,0,1};
p1 = 2;
all_right = (disabled || KawigiEdit_RunTest(1, p0, true, p1) ) && all_right;
tests_disabled = tests_disabled || disabled;
// ------------------
// ----- test 2 -----
disabled = false;
p0 = {0,1,3};
p1 = 3;
all_right = (disabled || KawigiEdit_RunTest(2, p0, true, p1) ) && all_right;
tests_disabled = tests_disabled || disabled;
// ------------------
// ----- test 3 -----
disabled = false;
p0 = {-677,-45,-2,3,8,29,384,867};
p1 = 7;
all_right = (disabled || KawigiEdit_RunTest(3, p0, true, p1) ) && all_right;
tests_disabled = tests_disabled || disabled;
// ------------------
// ----- test 4 -----
disabled = false;
p0 = {0,1,2,3,4,5,6,7,8,9};
p1 = 2;
all_right = (disabled || KawigiEdit_RunTest(4, p0, true, p1) ) && all_right;
tests_disabled = tests_disabled || disabled;
// ------------------
if (all_right) {
if (tests_disabled) {
cout << "You're a stud (but some test cases were disabled)!" << endl;
} else {
cout << "You're a stud (at least on given cases)!" << endl;
}
} else {
cout << "Some of the test cases had errors." << endl;
}
return 0;
}
// PROBLEM STATEMENT
// NOTE: This problem statement contains images that may not display properly if viewed outside of the applet.
//
//
// Taro and Hanako are playing Bunny Puzzle. There are several bunnies standing on a line. You are given a vector <int> bunnies, where each element is the initial position of a single bunny.
//
//
// They must perform the following routine exactly once:
//
// Choose two different bunnies A and B, located at points a and b, respectively.
// A jumps over B and lands at point 2*b-a.
//
//
//
//
//
//
//
// The jump is not allowed if another bunny is already at point 2*b-a.
//
//
//
//
//
// The jump is also not allowed if A jumps over more than one bunny.
//
//
//
//
//
//
//
//
// Return the number of different ways in which Taro and Hanako can choose the pair of bunnies A and B. "A jumps over B" and "B jumps over A" are considered to be different.
//
//
//
// DEFINITION
// Class:BunnyPuzzle
// Method:theCount
// Parameters:vector <int>
// Returns:int
// Method signature:int theCount(vector <int> bunnies)
//
//
// CONSTRAINTS
// -bunnies will contain between 2 and 50 elements, inclusive.
// -Each element of bunnies will be between -10^6 and 10^6, inclusive.
// -bunnies will be sorted in strictly ascending order.
//
//
// EXAMPLES
//
// 0)
// {5, 8}
//
// Returns: 2
//
// There are two possible moves:
//
// A bunny jumps from 5 to 11
// A bunny jumps from 8 to 2
//
//
// 1)
// {-1, 0, 1}
//
// Returns: 2
//
// There are two possible moves:
//
// A bunny jumps from 0 to -2
// A bunny jumps from 0 to 2
//
//
// 2)
// {0, 1, 3}
//
// Returns: 3
//
// There are three possible moves:
//
// A bunny jumps from 0 to 2
// A bunny jumps from 1 to -1
// A bunny jumps from 1 to 5
//
//
// 3)
// {-677, -45, -2, 3, 8, 29, 384, 867}
//
// Returns: 7
//
//
//
// 4)
// {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
//
// Returns: 2
//
//
//
// END KAWIGIEDIT TESTING