Witam, mam pewien problem bo mam program dla wieży Hanoi dla k słupków i n krążków. Działa to dobrze ale jak jest nie za dużo słupków i krążków. Jak jest ich więcej to na wynik czeka się co najmniej 30 minut.
#include <iostream>
#include <stdio.h>
#include <list>
using namespace std;
const int RODS = 4;
const int DISKS = 9;
typedef int State[DISKS+1];
typedef State* PState;
typedef list<PState> LPStates;
class Hanoi
{
public:
PState start;
PState finish;
LPStates fifo;
LPStates uniques;
Hanoi() {
this->start = (PState) new State;
this->finish = (PState) new State;
}
void ClearState(PState _state) { memset(_state, 0, DISKS*sizeof(int)); }
bool IsTheSame(PState _stateA, PState _stateB) { return 0 == memcmp(_stateA, _stateB, sizeof(State) - sizeof(int)); }
bool AddUnique(PState _state) {
for(LPStates::iterator i = this->uniques.begin(); i != this->uniques.end(); i++) {
if (this->IsTheSame(*i, _state)) return false;
}
this->uniques.push_back(_state);
return true;
}
int GetTopFromRod(int _rod, PState _state) {
for(int i=0; i < DISKS; i++)
if ((*_state)[i] == _rod) return i;
return -1;
}
bool CanIPut(int _disk, int _rod, PState _state) {
for(int i=0; i < _disk; i++)
if ((*_state)[i] == _rod) return false;
return true;
}
void Print(PState _state) {
cout << "{\n";
for(int i=0; i < DISKS; i++ ) {
cout << "\t#" << i << " @" << (*_state)[i] << "\n";
}
cout << "}\n";
}
bool FindRecursive(PState s) {
if (!s) s = this->start;
if (this->IsTheSame(s, this->finish)) {
cout << "mam!\n";
return true;
}
for(int r=0; r < RODS; r++) {
int id = this->GetTopFromRod(r, s);
if (-1 == id) continue;
for(int newr=0; newr < RODS; newr++) {
if (newr == r) continue;
if (this->CanIPut(id, newr, s)) {
PState p = (PState) new State();
memcpy(*p, *s, DISKS*sizeof(int));
(*p)[id] = newr;
if (this->AddUnique(p)){
if (this->FindRecursive(p)) {
cout << "#" << id << "z" << r << " na " << newr << "\n";
return true;
}
}
else delete p;
}
}
}
return false;
}
bool FindFifo() {
this->fifo.clear();
(*(this->start))[DISKS] = 0;
this->fifo.push_back(this->start);
PState s;
while (!this->fifo.empty()) {
s = this->fifo.front();
this->fifo.pop_front();
if (this->IsTheSame(s, this->finish)) {
this->fifo.clear();
while (0 != (*s)[DISKS]) {
this->fifo.push_front(s);
s = (PState) (*s)[DISKS];
}
this->fifo.push_front(this->start);
int steps = -1;
for(LPStates::iterator it = this->fifo.begin(); it != this->fifo.end(); it++) { steps++; this->Print(*it); }
cout << "mam w " << steps << " krokach.\n";
return true;
}
for(int r=0; r < RODS; r++) {
int id = this->GetTopFromRod(r, s);
if (-1 == id) continue;
for(int newr=0; newr < RODS; newr++) {
if (newr == r) continue;
if (this->CanIPut(id, newr, s)) {
PState p = (PState) new State();
memcpy(*p, *s, DISKS*sizeof(int));
(*p)[id] = newr;
if (this->AddUnique(p)) {
(*p)[DISKS] = (int)s;
this->fifo.push_back(p);
}
else delete p;
}
}
}
}
return false;
}
};
int main()
{
Hanoi hanoi;
hanoi.ClearState(hanoi.start);
hanoi.ClearState(hanoi.finish);
for(int i=0; i < DISKS; i++) {
(*(hanoi.start))[i] = 0;
(*(hanoi.finish))[i] = 1;
}
//hanoi.FindRecursive(NULL);
hanoi.FindFifo();
cout << "\n";
system("pause");
}
Potrzebuje ten program do testów chociaż dla 10 słupków i 10 krążków. Czy wie ktoś może jak ten program zoptymalizować?
Jak tak to prosiłbym o napisanie jak taką optymalizację zrobić?