From 9abc22dbd357374d1749f2c4aa8aeab1abcacce9 Mon Sep 17 00:00:00 2001 From: Tamas Bunth <tamas.bunth@collabora.co.uk> Date: Mon, 14 May 2018 06:10:01 +0200 Subject: [PATCH] =?UTF-8?q?Algoritmus=20sz=C3=A9p=C3=ADt=C3=A9se?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 16 ++++++++++++++++ element.cxx | 9 +++++---- element.hxx | 3 +++ matrix.cxx | 5 +++-- matrix.hxx | 1 + processor.cxx | 45 +++++++++++++++++++++++++-------------------- processor.hxx | 8 ++++++-- 7 files changed, 59 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 5bb111f..5678568 100644 --- a/README.md +++ b/README.md @@ -54,3 +54,19 @@ Az algoritmus a bactracking elvét követi. letett pálcikát, és folytatjuk a 2. lépésnél. 3. A feladvány akkor van kész, ha a tábla összes cellája foglalt. + +Program +------- + +A program első paramétereként egy szövegfájl elérési útvonalát várja. A +szövegfájl szerkezete a következő legyen: + +- Az első sor taralmazzon egy számot. Ez a szám a tábla méretét adja. + +- A szövegfájl többi sora a pálcikákat definiálja. Minden sorba egy pálcika + kerül, a pálcika szimbólumai számok lehetnek, szóközzel elválasztva. + +- Példa bemenet: input.txt + + Példa futtatás: ./program input.txt + + diff --git a/element.cxx b/element.cxx index 75d5273..2eeadd8 100644 --- a/element.cxx +++ b/element.cxx @@ -10,11 +10,12 @@ Element::Element(std::vector<Symbol> symbols) assert(symbols.size() > 0); } -/** - * Do not copy alignment and orientation - */ Element::Element(const Element& other) - : Element{other.symbols_} {} + : Element{other.symbols_} +{ + align = other.align; + dir = other.dir; +} Symbol Element::get(std::size_t pos) const { diff --git a/element.hxx b/element.hxx index 28d3af7..8d423f1 100644 --- a/element.hxx +++ b/element.hxx @@ -27,6 +27,9 @@ public: void print() const; + void setDir(Direction dir) { this->dir = dir; }; + void setAlign(Alignment align) { this->align = align; }; + /** * Sets the Element into a new state. State is determined by the alignment * and direction of the Element. diff --git a/matrix.cxx b/matrix.cxx index 8ac2138..cc88b29 100644 --- a/matrix.cxx +++ b/matrix.cxx @@ -100,7 +100,7 @@ bool Matrix::put(const Element& element, const Position& pos) return true; } -bool Matrix::putFirst(const Element& elem, Position& posIn) +bool Matrix::putFirst(const Element& elem, Position& posOut) { for( std::size_t i=0; i< board_.size(); ++i) for( std::size_t j=0; j<board_.size(); ++j) @@ -108,7 +108,7 @@ bool Matrix::putFirst(const Element& elem, Position& posIn) Position pos{i,j}; if( isFree(pos) ) { - posIn = pos; + posOut = pos; return put(elem, pos); } } @@ -158,3 +158,4 @@ void Matrix::print() const } std::cout << "--------------------" << std::endl; } + diff --git a/matrix.hxx b/matrix.hxx index abc0139..ef89096 100644 --- a/matrix.hxx +++ b/matrix.hxx @@ -2,6 +2,7 @@ #define _MATRIX_HXX_ #include <vector> +#include <list> #include "element.hxx" #include "utils.hxx" diff --git a/processor.cxx b/processor.cxx index 5c9770e..34247ef 100644 --- a/processor.cxx +++ b/processor.cxx @@ -2,30 +2,33 @@ #include <iostream> #include <algorithm> + +void Processor::onSuccess() const +{ + // TODO IMPL something more clever + rMatrix_.print(); + std::cout << "Elemek: " << std::endl; + for(const auto& elem : m_result) + { + elem.first.print(); + } + std::cout << "----------------------" << std::endl; +} + Processor::Processor(Matrix& matrix) : rMatrix_{matrix} {} -// Elemeket egy láncolt listába rakjuk. -// - Ha az elemet nem lehet behelyezni, akkor megváltoztatjuk az állapotát. -// + ha nincs több állapota, akkor kivesszük az elemet, és ÚJ elemet próbálok -// oda. -// * Ha nincs több elem, akkor kivesszük az azelőtti elemet, és új elemet -// próbálunk oda. (rekurzív gyanús, méghozzá ugyanazzal a listával, és -// egy indexxel, hogy épp hol tart az elempróba) - +// Store elements in a list. +// - If an element cannot be put in the matrix, than change its state. +// + If there are no more states, than try with a new element. +// * If there are no more elements, than pop out the last element and try +// to insert a new element there. void Processor::process(std::list<Element> list) { - + // found a solution if(list.size() == 0) { - rMatrix_.print(); - std::cout << "Elemek: " << std::endl; - for(const auto& elem : result) - { - elem.print(); - } - std::cout << "----------------------" << std::endl; - ready_ = true; + onSuccess(); return; } @@ -39,8 +42,10 @@ void Processor::process(std::list<Element> list) success = rMatrix_.putFirst(*it, pos); if(success) { - // rMatrix_.print(); - result.push_back(*it); + Element resultElem{*it}; + resultElem.setDir(it->getDir()); + resultElem.setAlign(it->getAlign()); + m_result.push_back(std::make_pair(*it, pos)); // copy everything except current std::list<Element> copy; @@ -49,7 +54,7 @@ void Processor::process(std::list<Element> list) process(std::move(copy)); rMatrix_.free(*it, pos); - result.pop_back(); + m_result.pop_back(); } hasMoreState = it->nextState(); diff --git a/processor.hxx b/processor.hxx index 8901bf1..6176de7 100644 --- a/processor.hxx +++ b/processor.hxx @@ -10,9 +10,13 @@ class Processor { private: - bool ready_ = false; Matrix& rMatrix_; - std::list<Element> result; + + // actual elements with position in matrix + std::list<std::pair<Element, Position>> m_result; + + protected: + void onSuccess() const; public: Processor(Matrix& matrix); -- GitLab