int main() { LinkedQueue<string> frontmen; frontmen.enqueue("Dave Grohl"); frontmen.enqueue("Bono"); frontmen.enqueue("Chester Bennington"); while (!frontmen.empty()) { cout << frontmen.front() << endl; cout << frontmen.size() << endl; frontmen.dequeue(); } return 0; }
void horse_queue(Point start, Point end) { LinkedQueue<Position> q; q.enqueue(start); board[start.first][start.second] = true; Position current; LinkedStack<LinkedQueue<Position> > history; LinkedQueue<Position> level; int currentMove = 0; history.push(level); while (!q.empty() && (current = q.dequeue()).p != end) { if (current.move == currentMove) { history.peek().enqueue(current); } else { // current.move == currentMove + 1 currentMove = current.move; LinkedQueue<Position> level; level.enqueue(current); history.push(level); } for(int dx = -2; dx <= 2; dx++) if (dx != 0) for(int sign = -1; sign <= 1; sign += 2) { int dy = sign * (3 - abs(dx)); Point newstart(current.p.first + dx, current.p.second + dy); Position newposition(newstart, current.move + 1); if (inside_board(newstart) && !board[newstart.first][newstart.second]) { q.enqueue(newposition); clog << newposition << endl; board[newstart.first][newstart.second] = true; } } } // current == end -- успех // q.empty() -- неуспех clog << history; history.pop(); if (current.p == end) { cout << "Успех за " << current.move << " хода!" << endl; LinkedStack<Point> path; path.push(current.p); while(!history.empty()) { // в history.peek() търсим първата позиция position, // за която isValidMove(current.p,position.p) Position position; while (!isValidMove(current.p, (position = history.peek().dequeue()).p)); // знаем, че isValidMove(current.p,position.p) // добавим position в пътя path.push(position.p); history.pop(); current = position; } cout << path << endl; } else cout << "Неуспех!" << endl; }