Website Summary Files Commits Branches @commit @diff @stats
path: root / Seminar03 / task02.cpp
diff options
context:
space:
mode:
Commit7eee5a78005a9a569186d2c8b4b645a818ab9bf1 (patch)
Author
 Stoycho Kyosev <skyosev@192.168.100.17>
2024-10-06 07:04:44 -0400
Committer
 Stoycho Kyosev <skyosev@192.168.100.17>
2024-10-06 07:04:44 -0400
Files84594e0af29cefa5aae76ace5f8eb81484400e31 /Seminar03/task02.cpp
Data-structures-and-algorithms-7eee5a78005a9a569186d2c8b4b645a818ab9bf1.tar
Data-structures-and-algorithms-7eee5a78005a9a569186d2c8b4b645a818ab9bf1.tar.gz
Data-structures-and-algorithms-7eee5a78005a9a569186d2c8b4b645a818ab9bf1.zip
Parent0841635826cb03c0e74cd75ec3c97c4c3b594d8b (diff)
Cleanup
1 files changed, 0 insertions, 93 deletions
-rw-r--r--Seminar03/task02.cpp93
diff --git a/Seminar03/task02.cpp b/Seminar03/task02.cpp
deleted file mode 100644
index ffc6a30..0000000
--- a/Seminar03/task02.cpp
+++ /dev/null
@@ -1,93 +0,0 @@
-#include <iostream>
-#include <array>
-#include <queue>
-
-template<unsigned boardSize = 8>
-class minMoves {
- using position = std::pair<int, int>;
- using positionAndSteps = std::pair<position, int>;
- using board = std::array<std::array<bool, boardSize>, boardSize>;
-
- board playground;
-
- void cleanPlayground() {
- for (auto &row : playground)
- for (auto &pos : row)
- pos = false;
- }
-
- static bool equalPositions(const position &fst, const position &snd) {
- return fst.first == snd.first && fst.second == snd.second;
- }
-
- static bool isValidPos(const position &p) {
- return p.first >= 0 && p.second >= 0 && p.first < boardSize && p.second < boardSize;
- }
-
- void mark(const position &toMark) {
- playground[toMark.first][toMark.second] = true;
- }
-
- bool isMarked(const position &pos) const {
- return playground[pos.first][pos.second];
- }
-
- void pushAndMarkIfValid(std::queue<positionAndSteps> &q, const position &toPush, int steps) {
- if (isValidPos(toPush) && !isMarked(toPush)) {
- q.push({toPush, steps});
- mark(toPush);
- }
- }
-
- int process(const position &startPos, const position &endPos) {
- std::queue<positionAndSteps> q;
-
- q.push({startPos, 0});
-
- while (!q.empty()) {
- positionAndSteps currentPosition = q.front();
-
- q.pop();
-
- if (equalPositions(currentPosition.first, endPos))
- return currentPosition.second;
-
- int x = currentPosition.first.first;
- int y = currentPosition.first.second;
- int moves = currentPosition.second + 1;
-
- pushAndMarkIfValid(q, {x + 1, y + 2}, moves);
- pushAndMarkIfValid(q, {x - 1, y - 2}, moves);
- pushAndMarkIfValid(q, {x + 1, y - 2}, moves);
- pushAndMarkIfValid(q, {x - 1, y + 2}, moves);
- pushAndMarkIfValid(q, {x + 2, y - 1}, moves);
- pushAndMarkIfValid(q, {x + 2, y + 1}, moves);
- pushAndMarkIfValid(q, {x - 2, y + 1}, moves);
- pushAndMarkIfValid(q, {x - 2, y - 1}, moves);
- }
- return -1;
- }
-
-public:
- minMoves() { cleanPlayground(); }
-
- int getMinMoves(const position &start, const position &end) {
- cleanPlayground();
-
- return process(start, end);
- }
-};
-
-int main() {
- minMoves m;
-
- std::cout << m.getMinMoves({0, 0}, {5, 3}) << std::endl;
-
- minMoves<12> ms;
-
- std::cout << ms.getMinMoves({0, 0}, {11, 11}) << std::endl;
-
- minMoves<2> mt;
-
- std::cout << mt.getMinMoves({0, 0}, {12, 212});
-}