September 24, 2021

生成n位全部二进制数,数组形式:蚂蚁爬杆问题

本文首发于CSDN
发表日期:2021.9.24

OOAD第一次项目,以OOAD思想实现蚂蚁爬杆问题解法。

问题

先重述一下蚂蚁爬杆问题:

有一根300 厘米的细木杆,在第30厘米、80厘米、110厘米、160厘米、250厘米这五个位置上各有一只蚂蚁。木杆很细,不能同时通过两只蚂蚁。
开始时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前走或调头,但不会后退。 当任意两只蚂蚁碰头时,两只蚂蚁会同时调头朝相反方向走。假设蚂蚁们每秒钟可以走5厘米的距离。
注:不允许穿越对方身体继续直行。
请编写一个程序,计算各种可能情形下所有蚂蚁都离开木杆的最小时间和最大时间。

思路

在编写过程中设置了一个GameRoom类用于模拟运行全部蚂蚁的爬行情况,其中需要一个函数来生成所有可能的方向组合。

我在写几个类的头文件最开始定义了一个游戏数据结构体,用于存放每一种方向组合的具体蚂蚁初试方向与游戏的结束时间:

1
2
3
4
5
struct GameData
{
bool antDirection[DefaultAnt.antcnt];
int gameTime;
};

同时在蚂蚁类中以bool值定义了蚂蚁行进的方向,以右为正:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
The Ant class.
*/
class Ant
{
private:
int antId;
double velocity;
bool direction; // True stands for right.
double location;
bool isAlive; // True stands for alive.

public:

};

GameRoom类如下构造:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
The GameRoom class.
*/
class GameRoom
{
private:
int incTime;
GameData stGameData[DefaultAnt.GroupCnt];

public:
GameRoom ();

void buildDirections ();
void playGames ();
void start ();

};

初步思想如下:

将结构体数组中按用于存放方向的数组展开,组合成行,列的矩阵,其中第列的项为stGameData[i].antDirection[j]

第一列自上而下依次赋值

第二列自上而下依次赋值

第三列自上而下依次赋值

stGameData[i].antDirection[j] = (i >> j) % 2

因而如下构造生产函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
Post: Every situation is generalized.
*/
void GameRoom::buildDirections ()
{
for (int divis = 0; divis < DefaultAnt.antcnt; divis ++)
{
int tmpdivi = pow (2, divis);
for (int i = 0; i < DefaultAnt.GroupCnt; i += 2 * tmpdivi)
{
for (int j = i; j < tmpdivi; j ++)
stGameData[j].antDirection[divis] = true;
for (int p = i + tmpdivi; p < i + 2 * tmpdivi; p ++)
stGameData[p].antDirection[divis] = false;
}
}
}

第一层for循环用于设置计算次,第二层for循环用于计算每一个结构题内方向数组应从第几位开始赋值,最内层的两个for循环用于赋值

算法时间复杂度可能稍高······

完整代码

最后贴上OOAD思想写的爬杆问题解法:

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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//
// Ant.h
// Ant
//
//

#ifndef Ant_h
#define Ant_h

#include <math.h>

struct DefaultStickData
{
const double leftEnd = 0;
const double rightEnd = 300;
};

struct DefaultAntData
{
const int antcnt = 5;
const int GroupCnt = 32;
const double speed = 5;
const bool direct = true;
const double loc[5] = {30, 80, 110, 160, 250};
};

const DefaultStickData DefaultStick;
const DefaultAntData DefaultAnt;



struct GameData
{
bool antDirection[DefaultAnt.antcnt];
int gameTime;
};


/*
The Ant class.
*/
class Ant
{
private:
int antId;
double velocity;
bool direction; // True stands for right.
double location;
bool isAlive; // True stands for alive.

public:
Ant ();
void setAnt (int antID, double vel, bool dir, double loc);

void changeDirection ();
void creeping ();
bool isColllision (bool isCol);
bool isLive ();
void changeLive ();

double getLocation ();
double getVelocity ();
bool getDirection ();

};

/*
Post: Ant class is initialized.
*/
Ant::Ant ()
{

}

void Ant::setAnt (int antID, double vel, bool dir, double loc)
{
antId = antID;
velocity = vel;
direction = dir;
location = loc;
isAlive = true;
}

/*
Post: The ant's direction is changed.
*/
void Ant::changeDirection ()
{
if (isAlive)
direction = !direction;
}

/*
Post: The function keeps the ant creeping by 1 second.
*/
void Ant::creeping ()
{
if (isAlive)
if (direction == true)
location += velocity;
else
location -= velocity;
}

/*
Post: This function returns a bool value on the basis of isCol given by the detectCollision function in CreepingGame.
*/
bool Ant::isColllision (bool isCol)
{
return isCol;
}

/*
Post: The status of the ant is retuned.
*/
bool Ant::isLive ()
{
return isAlive;
}

/*
Post: The status of the ant is changed.
*/
void Ant::changeLive ()
{
if (isAlive)
isAlive = !isAlive;
}

/*
Post: The function prints the ant's location.
*/
double Ant::getLocation ()
{
return location;
}

/*
Post: The function prints the ant's velocity.
*/
double Ant::getVelocity ()
{
return velocity;
}

/*
Post: The function prints the ant's direction.
*/
bool Ant::getDirection()
{
return direction;
}


/*
The Stick class.
*/
class Stick
{
private:
double leftLocation;
double rightLocation;
int livingAnts;

public:
Stick ();
void setStick (double leftLoc, double rightLoc);

bool isOut (Ant tmpAnt);

void killAnt ();
int getLivAntCnt ();

};

/*
Post: The stick is initialized, giving the value of leftLocation is smaller than that of rightLocation.
*/
Stick::Stick ()
{

}

void Stick::setStick (double leftLoc, double rightLoc)
{
leftLocation = leftLoc;
rightLocation = rightLoc;
livingAnts = DefaultAnt.antcnt;
}

/*
Post: The function examines whether the given ant is out of the stick, true stands for out.
*/
bool Stick::isOut (Ant tmpAnt)
{
if (tmpAnt.getLocation () <= leftLocation || tmpAnt.getLocation () >= rightLocation)
return true;
return false;
}

/*
Post: The amount of ants decreases by 1.
*/
void Stick::killAnt ()
{
livingAnts --;
}

/*
Post: The amount of living ants is returned.
*/
int Stick::getLivAntCnt ()
{
return livingAnts;
}


/*
The CreepingGame class.
*/
class CreepingGame
{
private:
int gameTime;
bool isGameOver; // True stands for not over.
double incTim;
Ant livant[DefaultAnt.antcnt];
Stick curstick;

public:
CreepingGame (int gameTim = 0, bool isGameOve = false, double incT = 1);

void drivingGame ();
void startGame (GameData gData);
void detectCollision ();
bool isAllDead ();

bool getGameStatus ();
int getGameTime ();
};

/*
Post: The Game is initialized.
*/
CreepingGame::CreepingGame (int gameTim, bool isGameOve, double incT)
{
gameTime = gameTim;
isGameOver = isGameOve;
incTim = incT;
}

/*
Post: The game's time is increased by 1 second;
*/
void CreepingGame::drivingGame ()
{
while (1)
{
gameTime += incTim;

detectCollision ();

for (int i = 0; i < DefaultAnt.antcnt; i++)
if (livant[i].isLive ())
livant[i].creeping ();

for (int i = 0; i < DefaultAnt.antcnt; i++)
{
if (livant[i].isLive ())
if (curstick.isOut (livant[i]))
{
livant[i].changeLive ();
curstick.killAnt ();
}
}

if (isAllDead () == true)
break;
}

isGameOver = true;
}

/*
Post: The game is started and the stick and ants are initialized.
*/
void CreepingGame::startGame (GameData gData)
{
curstick.setStick (DefaultStick.leftEnd, DefaultStick.rightEnd);

for (int i = 0; i < DefaultAnt.antcnt; i ++)
livant[i].setAnt (i, DefaultAnt.speed, gData.antDirection[i], DefaultAnt.loc[i]);

}

/*
Post: Collision are detected and ants' directions are changed.
*/
void CreepingGame::detectCollision ()
{
int tmpantid = -1;
for (int i = 0; i < DefaultAnt.antcnt; i ++)
{
tmpantid = -1;
if (livant[i].isLive ())
{
if (livant[i].getDirection () == true)
{
for (int j = 0; j < DefaultAnt.antcnt; j ++)
if (livant[j].isLive ())
{
if (tmpantid == -1)
{
if (livant[j].getLocation () > livant[i].getLocation ())
tmpantid = j;
}
else
{
if (livant[j].getLocation () > livant[i].getLocation () && livant[j].getLocation () < livant[tmpantid].getLocation ())
tmpantid = j;
}
}
if (tmpantid != -1)
if (abs (livant[i].getLocation () - livant[tmpantid].getLocation ()) <= (livant[i].getVelocity () + livant[tmpantid].getVelocity ()))
{
livant[i].changeDirection ();
livant[tmpantid].changeDirection ();
}
}
else
{
for (int j = 0; j < DefaultAnt.antcnt; j ++)
if (livant[j].isLive ())
{
if (tmpantid == -1)
{
if (livant[j].getLocation () < livant[i].getLocation ())
tmpantid = j;
}
else
{
if (livant[j].getLocation () < livant[i].getLocation () && livant[j].getLocation () > livant[tmpantid].getLocation ())
tmpantid = j;
}
}
if (tmpantid != -1)
if (abs (livant[i].getLocation () - livant[tmpantid].getLocation ()) <= (livant[i].getVelocity () + livant[tmpantid].getVelocity ()))
{
livant[i].changeDirection ();
livant[tmpantid].changeDirection ();
}
}
}
}
}

/*
Post: The function judges whether all the ants are dead.
*/
bool CreepingGame::isAllDead ()
{
if (curstick.getLivAntCnt () <= 0)
return true;
return false;
}

/*
Post: The status of the game is returned.
*/
bool CreepingGame::getGameStatus ()
{
return isGameOver;
}

/*
Post: The time of the game is returned.
*/
int CreepingGame::getGameTime ()
{
return gameTime;
}


/*
The GameRoom class.
*/
class GameRoom
{
private:
int incTime;
GameData stGameData[DefaultAnt.GroupCnt];

public:
GameRoom ();

void buildDirections ();
void playGames ();
void start ();

};

/*
Post: The function initializes the class.
*/
GameRoom::GameRoom ()
{
incTime = 1;

for (int i = 0; i < DefaultAnt.GroupCnt; i ++)
stGameData[i].gameTime = 0;
}

/*
Post: Every situation is generalized.
*/
void GameRoom::buildDirections ()
{
for (int divis = 0; divis < DefaultAnt.antcnt; divis ++)
{
int tmpdivi = pow (2, divis);
for (int i = 0; i < DefaultAnt.GroupCnt; i += 2 * tmpdivi)
{
for (int j = i; j < tmpdivi; j ++)
stGameData[j].antDirection[divis] = true;
for (int p = i + tmpdivi; p < i + 2 * tmpdivi; p ++)
stGameData[p].antDirection[divis] = false;
}
}
}

/*
Post: Games are played.
*/
void GameRoom::playGames ()
{
CreepingGame creep[DefaultAnt.GroupCnt];
for (int i = 0; i < DefaultAnt.GroupCnt; i ++)
{
creep[i].startGame (stGameData[i]);
creep[i].drivingGame();
stGameData[i].gameTime = creep[i].getGameTime ();
}

int maxtime = 0;
int mintime = stGameData[0].gameTime;

for (int j = 0; j < DefaultAnt.GroupCnt; j ++)
{
if (stGameData[j].gameTime > maxtime)
maxtime = stGameData[j].gameTime;
if (stGameData[j].gameTime < mintime)
mintime = stGameData[j].gameTime;
}

std::cout << "Max Time is " << maxtime << std::endl;
std::cout << "Min Time is " << mintime << std::endl;
}

/*
Post: Game starts.
*/
void GameRoom::start ()
{
std::cout << "Game started." << std::endl;
buildDirections ();
playGames ();
}

#endif /* Ant_h */
1
2
3
4
5
6
7
8
9
10
11
12
13
//
// main.cpp
// Ant
//
//

#include <iostream>
#include "Ant.h"

int main() {
GameRoom room;
room.start ();
}

About this Post

This post is written by 李亦杨 / リエキヨウ / Jeff Li, all rights reserved.

#中文#开发