马踏棋盘C++代码
2010-04-27 19:47
169 查看
闲来无事,写了一下马踏棋盘的算法,高手莫笑,有问题多指教。
马踏棋盘:
在8×8方格的棋盘上,从任意指定方格出发,为马寻找一条走遍棋盘每一格并且只经过一次的一条路径。
马踏棋盘:
在8×8方格的棋盘上,从任意指定方格出发,为马寻找一条走遍棋盘每一格并且只经过一次的一条路径。
#include <iostream> #include <vector> #include <iomanip> using namespace std; class Point { public: Point(size_t x, size_t y):x_(x), y_(y) { } friend Point operator +(const Point &lhs, const Point &rhs) { return Point(lhs.x_ + rhs.x_, lhs.y_ + rhs.y_); } friend bool operator !=(const Point &lhs, const Point &rhs) { return (lhs.x_ != rhs.x_) || (lhs.y_ != rhs.y_); } size_t x_; size_t y_; }; class IChess { public: virtual size_t GetWidth() = 0; virtual size_t GetHight() = 0; virtual bool IsVisited(const Point &p) = 0; virtual bool IsPosInChess(const Point &p) = 0; virtual void MoveOn(const Point &p, size_t index) = 0; virtual Point GetNextPos(const Point &curPos) = 0; virtual void Clear() = 0; virtual void DisaplyFootPrint() = 0; }; class Chess:public IChess { public: Chess() { memset(matrix_, -1, sizeof(matrix_)); } virtual size_t GetWidth() { return WIDTH; } virtual size_t GetHight() { return HIGHT; } virtual bool IsVisited(const Point &p) { return matrix_[p.y_][p.x_] != -1; } virtual bool IsPosInChess(const Point &p) { return p.x_ >=0 && p.x_ < GetWidth() && p.y_ >=0 && p.y_ < GetHight(); } virtual void MoveOn(const Point &p, size_t index) { matrix_[p.y_][p.x_] = index; } size_t GetNextPosNum(const Point &curPos) { vector<Point> posVector; GetNextPosVector(curPos, posVector); return posVector.size(); } virtual Point GetNextPos(const Point &curPos) { vector<Point> posVector; GetNextPosVector(curPos, posVector); if (posVector.size() == 0) { return Point(-1, -1); } vector<Point>::const_iterator itLeastNextPos = posVector.begin(); for (vector<Point>::const_iterator it = itLeastNextPos + 1; it != posVector.end(); ++it) { if (GetNextPosNum(*it) < GetNextPosNum(*itLeastNextPos)) { itLeastNextPos = it; } } return *itLeastNextPos; } virtual void GetNextPosVector(const Point &curPos, vector<Point>&nextPosVector) { static const Point offset[8] = { Point(-2, -1), Point(-1, -2), Point(1, -2), Point(2, -1), Point(2, 1), Point(1, 2), Point(-1, 2), Point(-2, 1) }; const size_t posNum = sizeof(offset)/ sizeof(offset[0]); for (size_t i=0; i<posNum; ++i) { Point p = curPos + offset[i]; if (IsPosInChess(p) && !IsVisited(p)) { nextPosVector.push_back(p); } } } virtual void DisaplyFootPrint() { for (int i=0; i<WIDTH; ++i) { for (int j=0; j<HIGHT; ++j) { cout << setw(4) << matrix_[i][j]; } cout << endl; } } virtual void Clear() { for (int i=0; i<WIDTH; ++i) { for (int j=0; j<HIGHT; ++j) { matrix_[i][j] = -1; } } } private: static const int WIDTH = 8; static const int HIGHT = 8; size_t matrix_[WIDTH][HIGHT]; }; class Horse { public: Horse(const Point &p): pos_(p), index_(0), chess_(NULL) { } void OnChess(IChess &chess) { chess_ = &chess; chess_->MoveOn(pos_, index_++); } void Move() { while ((pos_ = chess_->GetNextPos(pos_)) != Point(-1, -1)) { chess_->MoveOn(pos_, index_++); } } private: Point pos_; size_t index_; IChess * chess_; }; int main(int argc, char* argv[]) { Chess ch; for (int i=0; i<8; ++i) { for (int j=0; j<8; ++j) { Horse h(Point(i, j)); h.OnChess(ch); h.Move(); ch.DisaplyFootPrint(); ch.Clear(); system("pause"); } } return 0; }
相关文章推荐
- 棋盘的完美覆盖问题,c++代码实现
- 棋盘覆盖问题C++代码
- 棋盘的完美覆盖问题,c++代码实现
- 棋盘的完美覆盖问题,c++代码实现
- 【poj 1321】 棋盘问题 题解&题意&代码(C++)
- Eclipse+CDT自动生成的代码 C++ 头文件重名问题
- 【积累】C/C++中明明该用函数实现的功能,为啥非要自己写代码!
- C++反汇编代码分析--函数调用
- 在C/C++代码中使用SSE等指令集的指令(3)SSE指令集基础
- C++正则表达式使用实例--实现一个markdown代码标记转换工具
- 转载一份C++线程池的代码,非常实用(转)
- 最简单的递归遍历CTreeCtrl树C++代码
- C++ 是C 的超集吗?可以用C++ 编译器来编译C 代码吗?
- Win32|一个超级喜欢的自定义MessageBox打印代码|c++
- 代码简洁之道:C++ 11 之auto+ for_each + lamda表达式
- C++ 代码优化
- 使用Java中的JNI技术将C/C++程序嵌入到Java代码中实现Hello World
- HOG特征显示C++ OpenCV代码
- c++ 代码的执行顺序
- C语言代码中调用C++代码的方法示例