-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
82 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include "queue_horsewalker.hpp" | ||
|
||
HorseWalk const& QueueHorseWalker::findWalk(Position const& start, Position const& end) { | ||
if (!insideBoard(end)) | ||
return walk; | ||
horseWave.enqueue(start); | ||
while (!horseWave.empty()) { | ||
Position current = horseWave.dequeue(); | ||
if (current == end) { | ||
// успех | ||
LinkedStack<Position> reversedWalk; | ||
|
||
while (!steps.empty()) { | ||
Step step = steps.pop(); | ||
if (step.second == current) { | ||
reversedWalk.push(current); | ||
current = step.first; | ||
} | ||
} | ||
reversedWalk.push(start); | ||
while (!reversedWalk.empty()) | ||
walk.push_back(reversedWalk.pop()); | ||
return walk; | ||
} | ||
|
||
if (insideBoard(current) && !board[current.first][current.second]) { | ||
// стъпваме на тази позиция | ||
board[current.first][current.second] = true; | ||
// добавяме всички следващи стъпки | ||
for(int dx = -2; dx <= 2; dx++) | ||
if (dx != 0) | ||
for (int sign : {-1, 1}) { | ||
int dy = sign * (3 - std::abs(dx)); | ||
Position newPosition{current.first + dx, current.second + dy}; | ||
horseWave.enqueue(newPosition); | ||
steps.push({current, newPosition}); | ||
} | ||
} | ||
} | ||
// не успяхме да стигнем до края | ||
return walk; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#ifndef __QUEUE_HORSEWALKER_HPP | ||
#define __QUEUE_HORSEWALKER_HPP | ||
|
||
#include "horsewalker.hpp" | ||
#include "lqueue.hpp" | ||
#include "lstack.hpp" | ||
|
||
using Step = std::pair<Position, Position>; | ||
|
||
class QueueHorseWalker : public HorseWalker { | ||
LinkedQueue<Position> horseWave; | ||
HorseWalk walk; | ||
LinkedStack<Step> steps; | ||
public: | ||
QueueHorseWalker(size_t size) : HorseWalker(size) {} | ||
|
||
// намира разходка на коня от start до end | ||
// връща празна разходка, ако не съществува | ||
virtual HorseWalk const& findWalk(Position const& start, Position const& end); | ||
}; | ||
|
||
#endif |