# A编程题--马走日

A编程题--马走日

图1 马走日                                                      图2 蹩马腿

图3 蹩马腿                                                  图4 将军

图5棋子存储

//可以参考的代码如下，主函数已给定，不需要提交，其它代码可自由编写。

#include <iostream>
#include <cmath>
#include <string>
using namespace std;
class ChessPoint
{
public:
ChessPoint(int row=-1, int col=-1){SetPoint(row,col);}
ChessPoint(const ChessPoint &src);
ChessPoint& operator=(const ChessPoint &src){ SetPoint(src.Getrow(),src.Getcol());return *this;}
bool operator==(const ChessPoint &src) const { return (src.Getrow() == Getrow() && src.Getcol() == Getcol()); }
void Setrow(int row){this->row=row;}
void Setcol(int col){this->col=col;}
void SetPoint(int row, int col){Setrow(row);Setcol(col);}
int Getrow() const {return row;}
int Getcol() const {return col;}
private:
int row,col;
};
class ChessBoard
{
public:
ChessBoard(){num=0;}
void AddPiece(int row,int col,string role,string name);  //增加一个棋子
bool RedMaCanCheck(int row,int col);//红方马(row,col) 是否对黑方存在将军
private:
int num;    //目前棋子数量
ChessPoint point[32]; //棋子坐标
string role[32];    //红方或黑方
string name[32];    //棋子名称
};
void ChessBoard::AddPiece(int row,int col,string role,string name)
{
point[num]=ChessPoint(row,col);
this->role[num]=role;
this->name[num]=name;
num++;
}
int main()
{
ChessBoard CB;
int n,row,col;
string role,name;
cin>>n;
while(n--)
{
cin>>row>>col>>role>>name;
CB.AddPiece(row,col,role,name);
}
cin>>row>>col;
if(CB.RedMaCanCheck(row,col))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
return 0;
}

n个棋子和n棋子的信息（位置，标识），红方马的坐标。

``````5
9 4 黑 将
7 7 黑 炮
6 4 黑 卒
3 4 红 兵
1 3 红 帅
2 6``````

``No``

``````#include <iostream>
#include <cmath>
#include <string>
using namespace std;
class ChessPoint
{
public:
ChessPoint(int row=-1, int col=-1){SetPoint(row,col);}
ChessPoint(const ChessPoint &src);
ChessPoint& operator=(const ChessPoint &src){ SetPoint(src.Getrow(),src.Getcol());return *this;}
bool operator==(const ChessPoint &src) const { return (src.Getrow() == Getrow() && src.Getcol() == Getcol()); }
void Setrow(int row){this->row=row;}
void Setcol(int col){this->col=col;}
void SetPoint(int row, int col){Setrow(row);Setcol(col);}
int Getrow() const {return row;}
int Getcol() const {return col;}
private:
int row,col;
};
class ChessBoard
{
public:
ChessBoard(){num=0;}
void AddPiece(int row,int col,string role,string name);  //增加一个棋子
bool RedMaCanCheck(int row,int col);//红方马(row,col) 是否对黑方存在将军
private:
int num;    //目前棋子数量
ChessPoint point[32]; //棋子坐标
string role[32];    //红方或黑方
string name[32];    //棋子名称
};
void ChessBoard::AddPiece(int row,int col,string role,string name)
{
point[num]=ChessPoint(row,col);
this->role[num]=role;
this->name[num]=name;
num++;
}

bool ChessBoard::RedMaCanCheck(int row,int col){
int jx,jy,cun=0;
for(int i=0;i<num;i++)
{if(name[i]=="将"&&role[i]=="黑")
{

jx=point[i].Getrow();
jy=point[i].Getcol();
cun=1;
}
}
if(cun==0||jx==row||col==jy)
return 0;
int sum=0;
if(jx>row)sum+=jx-row;
else sum+=row-jx;
if(jy>col)sum+=jy-col;
else sum+=col-jy;
if(sum!=3)return 0;
char s;

if(jx>row)
{
if(jy>col){
if(jx-row==2)s='c';
else s='b';
}
else{
if(jx-row==2)s='c';
else s='d';
}
}
else{
if(jy>col){
if(row-jx==2)s='a';
else s='b';
}
else{
if(row-jx==2)s='a';
else s='d';
}
}
int n=num;

jx=row;jy=col;
if(s=='a'){
for(int i=0;i<n;i++)
if(jx-1==point[i].Getrow()&&jy==point[i].Getcol())
return 0;
}
if(s=='b'){
for(int i=0;i<n;i++)
if(jx==point[i].Getrow()&&jy+1==point[i].Getcol())
return 0;
}
if(s=='c'){
for(int i=0;i<n;i++)
if(jx+1==point[i].Getrow()&&jy==point[i].Getcol())
return 0;
}
if(s=='d'){
for(int i=0;i<n;i++)
if(jx==point[i].Getrow()&&jy-1==point[i].Getcol())
return 0;
}

return 1;
}
int main()
{
ChessBoard CB;
int n,row,col;
string role,name;
cin>>n;
while(n--)
{
cin>>row>>col>>role>>name;
CB.AddPiece(row,col,role,name);
}
cin>>row>>col;
if(CB.RedMaCanCheck(row,col))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
return 0;
}
``````

``````#include <iostream>
#include <cmath>
#include <string>
using namespace std;
class ChessPoint
{
public:
ChessPoint(int row=-1, int col=-1){SetPoint(row,col);}
ChessPoint(const ChessPoint &src);
ChessPoint& operator=(const ChessPoint &src){ SetPoint(src.Getrow(),src.Getcol());return *this;}
bool operator==(const ChessPoint &src) const { return (src.Getrow() == Getrow() && src.Getcol() == Getcol()); }
void Setrow(int row){this->row=row;}
void Setcol(int col){this->col=col;}
void SetPoint(int row, int col){Setrow(row);Setcol(col);}
int Getrow() const {return row;}
int Getcol() const {return col;}
private:
int row,col;
};
class ChessBoard
{
public:
ChessBoard(){num=0;}
void AddPiece(int row,int col,string role,string name);  //增加一个棋子
bool RedMaCanCheck(int row,int col);//红方马(row,col) 是否对黑方存在将军
private:
int num;    //目前棋子数量
ChessPoint point[32]; //棋子坐标
string role[32];    //红方或黑方
string name[32];    //棋子名称
};
void ChessBoard::AddPiece(int row,int col,string role,string name)
{
point[num]=ChessPoint(row,col);
this->role[num]=role;
this->name[num]=name;
num++;
}

bool ChessBoard::RedMaCanCheck(int row,int col){
int jx,jy,cun=0;
for(int i=0;i<num;i++)
{if(name[i]=="将"&&role[i]=="黑")
{

jx=point[i].Getrow();
jy=point[i].Getcol();
cun=1;
}
}
if(cun==0||jx==row||col==jy)
return 0;
int sum=0;
if(jx>row)sum+=jx-row;
else sum+=row-jx;
if(jy>col)sum+=jy-col;
else sum+=col-jy;
if(sum!=3)return 0;
char s;

if(jx>row)
{
if(jy>col){
if(jx-row==2)s='c';
else s='b';
}
else{
if(jx-row==2)s='c';
else s='d';
}
}
else{
if(jy>col){
if(row-jx==2)s='a';
else s='b';
}
else{
if(row-jx==2)s='a';
else s='d';
}
}
int n=num;

jx=row;jy=col;
if(s=='a'){
for(int i=0;i<n;i++)
if(jx-1==point[i].Getrow()&&jy==point[i].Getcol())
return 0;
}
if(s=='b'){
for(int i=0;i<n;i++)
if(jx==point[i].Getrow()&&jy+1==point[i].Getcol())
return 0;
}
if(s=='c'){
for(int i=0;i<n;i++)
if(jx+1==point[i].Getrow()&&jy==point[i].Getcol())
return 0;
}
if(s=='d'){
for(int i=0;i<n;i++)
if(jx==point[i].Getrow()&&jy-1==point[i].Getcol())
return 0;
}

return 1;
}
int main()
{
ChessBoard CB;
int n,row,col;
string role,name;
cin>>n;
while(n--)
{
cin>>row>>col>>role>>name;
CB.AddPiece(row,col,role,name);
}
cin>>row>>col;
if(CB.RedMaCanCheck(row,col))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
return 0;
}
``````