📄 1.txt
字号:
#include <iostream.h>
#include <iomanip.h>
template <class T>
class Stack
{
public:
Stack(int MaxSize=64);
~Stack () { delete [] stack; }
inline bool IsEmpty() const { return (top==-1); }
inline bool IsFull() const { return (top==MaxTop); }
inline T Top() const;
inline Stack<T>& Add(const T& x);
inline Stack<T>& Delete(T& x);
private:
int top;
int MaxTop;
T *stack;
};
template <class T>
Stack<T>::Stack(int MaxSize)
{
MaxTop=MaxSize-1;
stack=new T[MaxSize];
top=-1;
}
template <class T>
T Stack<T>::Top() const
{
return stack[top];
}
template<class T>
Stack<T>& Stack<T>::Add(const T& x)
{
stack[++top]=x;
return *this;
}
template<class T>
Stack<T>& Stack<T>::Delete(T& x)
{
x=stack[top--];
return *this;
}
class Position
{
friend void FindPath();
private:
int row;
int col;
};
void FindPath()
{
Stack<Position>* path;
int board[8][8];
path=new Stack<Position>(63);
Position offset[8];
offset[0].row=-2; offset[0].col=1;
offset[1].row=-1; offset[1].col=2;
offset[2].row=1; offset[2].col=2;
offset[3].row=2; offset[3].col=1;
offset[4].row=2; offset[4].col=-1;
offset[5].row=1; offset[5].col=-2;
offset[6].row=-1; offset[6].col=-2;
offset[7].row=-2; offset[7].col=-1;
Position here;
here.row=0; here.col=0;
int step=1;
int i,j;
for (i=0;i<8;i++)
for (j=0;j<8;j++)
board[i][j]=0;
board[0][0]=step;
int option=0;
int lastOption=7;
while (step!=64)
{
int r,c;
while (option<=lastOption)
{
r=here.row+offset[option].row;
c=here.col+offset[option].col;
if (r>=0 && r<=7 && c>=0 && c<=7)
if (board[r][c]==0) break;
option++;
}
if (option<=lastOption)
{
path->Add(here);
here.row=r; here.col=c;
board[r][c]=++step;
option=0;
}
else
{
do
{
Position next;
path->Delete(next);
int a=next.row-here.row; int b=next.col-here.col;
if (a==2)
{
if (b==1)
option=4;
else
option=5;
}
else if (a==1)
{
if (b==2)
option=3;
else
option=6;
}
else if (a==-1)
{
if (b==2)
option=2;
else
option=7;
}
else
{
if (b==1)
option=1;
else
option=8;
}
here=next;
}while (option==8);
}
}
for(i=0;i<=7;i++)
{
for(j=0;j<=7;j++)
cout<<setw(3)<<board[i][j];
cout<<endl;
}
}
void main(void)
{
FindPath();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -