帮忙修改一下程序
#include<iostream.h> #include<stdlib.h>
#define null NULL
struct node //定义了一个结构体,用来记录横纵坐标信息与已经走过的相临的格子
{
int x;
int y;
node *next;
};
int chessboard[8][8]; //棋盘,用来记录总的情况
int counter=1; //用来记录路线数
class stack //一个堆栈的类
{
private:
int flag;
node *array[65];
public:
void makenull()
{
flag=0;
}
bool isempty()
{
if(flag) return 0;
else return 1;
}
bool isfull()
{
if(flag==64) return 1;
else return 0;
}
void push(node *a )
{
if(!isfull())
{
array[++flag]=a;
chessboard[a->x][a->y]=flag;
}
}
void pop()
{
if(!isempty())
{
node *temp,*temp2;
temp=array[flag];
chessboard[temp->x][temp->y]=0;
while(temp!=null) {
temp2=temp->next;
free(temp);
temp=temp2;
}
flag--;
}
}
node *top()
{
if(!isempty())
{
return array[flag];
}
}
};
bool compare(node *a, node *b); //比较是否已经走过
void add(node *a,int m,int n); //将选择的路线记录下来,以免重复
void show_routes(); //打印结果
void main()
{
stack s; //定义了一个栈
s.makenull(); //初始化栈
int abscissa=0; //马的开始的横坐标
int ordinate=0; //马的开始的纵坐标
node *first; //记录马的初始位置
node *temp; //备用的空指针
node *a;
for(int i=0; i<8; i++) //初始化棋盘
for(int j=0; j<8; j++)
chessboard[i][j]=0;
cout<<"please input the original location of the horse in the chessboard :"<<endl;
cout<<"please input the x : ";
cin>>abscissa;
cout<<endl<<"please input the y : "; //输入初始位置
cin>>ordinate;
cout<<endl;
chessboard[abscissa][ordinate]=1;
first=new node;
first->x=abscissa;
first->y=ordinate;
first->next=null;
s.push(first);
while(!s.isempty())
{
a=s.top();
temp=new node;
int m=a->x;
int n=a->y;
int measure=0;
if(((m-2)>=0)&&((n-1)>=0)) {
temp->x=m-2;
temp->y=n-1;
temp->next=null;
if(compare(a,temp)&&(chessboard[temp->x][temp->y]==0)) {
m=-100;
n=-100;
measure=1;
}
}
if(((m-2)>=0)&&((n+1)>=0)&&((n+1)<8)) {
temp->x=m-2;
temp->y=n+1;
temp->next=null;
if(compare(a,temp)&&(chessboard[temp->x][temp->y]==0)) {
m=-100;
n=-100;
measure=1;
}
}
if(((m-1)>=0)&&((n-2)>=0)) {
temp->x=m-1;
temp->y=n-2;
temp->next=null;
if(compare(a,temp)&&(chessboard[temp->x][temp->y]==0)) {
m=-100;
n=-100;
measure=1;
}
}
if(((m-1)>=0)&&((n+2)>=0)&&((n+2)<8)) {
temp->x=m-1;
temp->y=n+2;
temp->next=null;
if(compare(a,temp)&&(chessboard[temp->x][temp->y]==0)) {
m=-100;
n=-100;
measure=1;
}
}
if(((m+1)>=0)&&((m+1)<8)&&((n-2)>=0)) {
temp->x=m+1;
temp->y=n-2;
temp->next=null;
if(compare(a,temp)&&(chessboard[temp->x][temp->y]==0)){
m=-100;
n=-100;
measure=1;
}
}
if(((m+1)>=0)&&((n+2)>=0)&&((m+1)<8)&&((n+2)<8)) {
temp->x=m+1;
temp->y=n+2;
temp->next=null;
if(compare(a,temp)&&(chessboard[temp->x][temp->y]==0)){
m=-100;
n=-100;
measure=1;
}
}
if(((m+2)>=0)&&((n-1)>=0)&&((m+2)<8)) {
temp->x=m+2;
temp->y=n-1;
temp->next=null;
if(compare(a,temp)&&(chessboard[temp->x][temp->y]==0)) {
m=-100;
n=-100;
measure=1;
}
}
if(((m+2)>=0)&&((n+1)>=0)&&((m+2)<8)&&((n+1)<8)) {
temp->x=m+2;
temp->y=n+1;
temp->next=null;
if(compare(a,temp)&&(chessboard[temp->x][temp->y]==0)) {
m=-100;
n=-100;
measure=1;
}
}
if(measure==1)
{
add(s.top(),temp->x,temp->y);
s.push(temp);
}
else {
if(s.isfull()) show_routes();
free(temp);
s.pop();
}
}
free(first); //释放指针
free(temp);
cin>>ordinate;
}
bool compare(node *a, node *b) //查看该路线是否以已经走过
{
node *temp;
temp=a->next;
while(temp!=null) {
if((temp->x==b->x)&&(temp->y==b->y)) return 0;
temp=temp->next;
}
return 1;
}
void add(node *a,int m,int n) //记录下某一个动作已经走过
{
node *temp;
temp=new node;
temp->x=m;
temp->y=n;
temp->next=null;
while(a->next!=null) a=a->next;
a->next=temp;
}
void show_routes()
{
cout<<endl<<endl;
cout<<"第 "<<counter++<<" 条路线: "<<endl;
for(int i=0;i<8;i++)
{
cout<<endl;
for(int j=0;j<8;j++)
cout<<chessboard[i][j]<<" ";
}
错误原因
h:\马踏棋盘\1.cpp(54) : error C2065: 'null' : undeclared identifier
h:\马踏棋盘\1.cpp(54) : error C2446: '!=' : no conversion from 'int' to 'struct node *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
h:\马踏棋盘\1.cpp(54) : error C2040: '!=' : 'struct node *' differs in levels of indirection from 'int'
h:\马踏棋盘\1.cpp(101) : error C2440: '=' : cannot convert from 'int' to 'struct node *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
h:\马踏棋盘\1.cpp(116) : error C2440: '=' : cannot convert from 'int' to 'struct node *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
h:\马踏棋盘\1.cpp(127) : error C2440: '=' : cannot convert from 'int' to 'struct node *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
h:\马踏棋盘\1.cpp(138) : error C2440: '=' : cannot convert from 'int' to 'struct node *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
h:\马踏棋盘\1.cpp(149) : error C2440: '=' : cannot convert from 'int' to 'struct node *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
h:\马踏棋盘\1.cpp(160) : error C2440: '=' : cannot convert from 'int' to 'struct node *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
h:\马踏棋盘\1.cpp(171) : error C2440: '=' : cannot convert from 'int' to 'struct node *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
h:\马踏棋盘\1.cpp(182) : error C2440: '=' : cannot convert from 'int' to 'struct node *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
h:\马踏棋盘\1.cpp(193) : error C2440: '=' : cannot convert from 'int' to 'struct node *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
h:\马踏棋盘\1.cpp(228) : error C2446: '!=' : no conversion from 'int' to 'struct node *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
h:\马踏棋盘\1.cpp(228) : error C2040: '!=' : 'struct node *' differs in levels of indirection from 'int'
h:\马踏棋盘\1.cpp(241) : error C2440: '=' : cannot convert from 'int' to 'struct node *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
h:\马踏棋盘\1.cpp(242) : error C2446: '!=' : no conversion from 'int' to 'struct node *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
h:\马踏棋盘\1.cpp(242) : error C2040: '!=' : 'struct node *' differs in levels of indirection from 'int'
h:\马踏棋盘\1.cpp(256) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
马踏棋盘.exe - 18 error(s), 0 warning(s)