1
#include <iostream>
#include <stack>
//构建堆栈

extern const int width = 8, height = 8;
int xCoo[8] = {-2,-1,1,2, 2, 1,-1,-2}; //xCoo 表示 x Coordinates,x坐标
int yCoo[8] = { 1, 2,2,1,-1,-2,-2,-1}; //y坐标

class Position
{
private:
    int x, y;
    int direction[8];
    int nextStepIndex;
public:
    bool availability;
    int getX(){return x;}
    int getY(){return y;}
    void setXY(int theX, int theY){x = theX;y = theY;}
    bool operator==(Position & a);
    void setAvailability(bool opt){ availability = opt;}
    Position(){}
    Position(int theX, int theY);
    int  getPresentNextStepIndex(){ return nextStepIndex;}
    int  getNextStepIndex(Position pos[width][height]);
    Position getStep(int theIndex);
    bool haveStep(Position pos[width][height]);
    void initCanGo();
    void setCanGo(int theIndex){direction[theIndex] = 1;}
    void setCanTGo(int theIndex){ direction[theIndex] = 0;}
    void setNextStepIndex(int theIndex){nextStepIndex = theIndex;}
};

Position::Position(int theX, int theY)
{
    x = theX;
    y = theY;
    for (int i = 0; i < 8; ++i)
    {
        if (
           ((getX() + xCoo[i]) < 0) 
        || ((getX() + xCoo[i]) >= width)
        || ((getY() + yCoo[i]) < 0)
        || ((getY() + yCoo[i]) >= height)
        )
        {
            direction[i] = 0;
        } else
        {
            direction[i] = 1;
        }
    }
    availability = true;
    nextStepIndex = -1;
}

void Position::initCanGo()
{
    for (int i = 0; i < 8; ++i)
    {
        if (
           ((getX() + xCoo[i]) < 0) 
        || ((getX() + xCoo[i]) >= width)
        || ((getY() + yCoo[i]) < 0)
        || ((getY() + yCoo[i]) >= height)
        )
        {
            direction[i] = 0;
        } else
        {
            direction[i] = 1;
        }
        availability =true;
        nextStepIndex = -1;
    }
}
int  Position::getNextStepIndex(Position pos[width][height])
{
    for (int i = 0; i < 8; ++i)
    {
        if ((direction[i] == 1) 
        && (pos[x+xCoo[i]][y+yCoo[i]].availability == true))
        {
            return i;
        }
    }
    return -1;
}

Position Position::getStep(int theIndex)
{
    int i, j;
    i = x + xCoo[theIndex];
    j = y + yCoo[theIndex];
    return Position(i, j);
}

bool Position::haveStep(Position pos[width][height])
{
    for (int i = 0; i < 8; ++i)
    {
        if ((direction[i] == 1)
        && (pos[x+xCoo[i]][y+yCoo[i]].availability == true))
        {
            return true;
        }
    }
    return false;
}


bool Position::operator==(Position & a)
{
    if ( (getX() == a.getX())
        && (getY() == a.getY()))
    {
        return true;
    }
    return false;
}


std::stack<Position*> thread;

int main()
{
    int theX, theY;
    int totalSize;

    totalSize = width * height;

    std::cout << "请输入初始坐标" << std::endl;
    std::cin >> theX >> theY;
    while (theX < 0 || theX >= width || theY < 0 || theY >= height)                     //确保输入合理坐标
    {
        std::cout << "坐标错误超出范围, 请重新输入.\n";
        std::cin >> theX >> theY;
    }
    Position location;
    Position pos[width][height];
    for (int i = 0; i < width; ++i)
    {
        for (int j = 0; j < height; ++j)
        {
            pos[i][j].setXY(i, j);
            pos[i][j].initCanGo();
            //pos[i][j].setAvailability(true);
        }
    }
    Position start(theX, theY);
    thread.push(&pos[theX][theY]);
    pos[thread.top()->getX()][thread.top()->getY()].setAvailability(false);
    std::cout << "The postion(" << thread.top()->getX() << ", " << thread.top()->getY() << ") is pushed into stack thread \n"; 
    std::cout << "Now the size of thread is : " << thread.size() << std::endl;

    //开始探索
    while(thread.size() != totalSize)
    {   
        while(!thread.empty())
        {
            std::cout << "Start Judge.\n";
            if (thread.top()->haveStep(pos))
            {
                std::cout << "(" << thread.top()->getX() << "," << thread.top()->getY() << ") have Step(s) to continue.\n";
                // pos[thread.top()->getX()][thread.top()->getY()].setNextStepIndex(thread.top().getNextStepIndex(pos));             //记录下一步的方向
                thread.top()->setNextStepIndex(thread.top()->getNextStepIndex(pos));
                Position temp = thread.top()->getStep(thread.top()->getNextStepIndex(pos));
                thread.push(&pos[temp.getX()][temp.getY()]);                                 //将下一步的Position 压入栈
                thread.top()->setAvailability(false);      
            } else
            {   
                // std::cout << "(" << thread.top()->getX() << "," << thread.top()->getY() << ") does't have step(s) to continue.\n";
                // std::cout << "(" << thread.top()->getX() << "," << thread.top()->getY() << ") is initialized and poped.\n";    //清除现有失败方向的记录
                std::cout << "(" << thread.top()->getX() << "," << thread.top()->getY() << ") has been poped.\n";
                thread.top()->initCanGo();
                thread.top()->setAvailability(true);
                thread.pop();                                                               //退步
                std::cout << "Now thread.top() is (" << thread.top()->getX() << ", " << thread.top()->getY() << ").\n"; 
                // pos[thread.top()->getX()][thread.top()->getY()].setCanTGo(thread.top()->getPresentNextStepIndex());             //设定之前的方向为失败方向
                thread.top()->setCanTGo(thread.top()->getPresentNextStepIndex());
                thread.top()->setNextStepIndex(thread.top()->getNextStepIndex(pos));
            }   
        }   
    }
    if (thread.size() == totalSize)                                                         //左右位置入栈后输出
    {
        while(!thread.empty())
        {
            std::cout << "("<< thread.top()->getX() <<", " << thread.top()->getY() << ") <--";
            thread.pop();
            if (thread.size() % 8 == 0)
            {
                std::cout << std::endl;
            }
        }
    }
    return 0;
}

VEIGHTZ
19 声望1 粉丝