您的位置:首页 > 编程语言 > C语言/C++

马踏棋盘C++代码

2010-04-27 19:47 169 查看
闲来无事,写了一下马踏棋盘的算法,高手莫笑,有问题多指教。

马踏棋盘:

在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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: