#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; }