问题描述
国际象棋的棋盘是黑白相间的 8 * 8 的方格,棋子放在格子中间。
王、后、车、象的走子规则如下:
王:横、直、斜都可以走,但每步限走一格。
后:横、直、斜都可以走,每步格数不受限制。
车:横、竖均可以走,不能斜走,格数不限。
象:只能斜走,格数不限。
写一个程序,给定起始位置和目标位置,计算王、后、车、象从起始位置走到目标位置所需的最少步数。
相信国际象棋格子大家都见过,我这里就不赘述了。王,后,车的步数计算还是比较简单的,直接计算两个格子之间的绝对值,通过比较就能打出结果。然而象的走法让我想了一阵子。我平时也是喜欢下国际象棋,知道象分黑白,好吧,暂且我是这么叫的。黑象只能走黑格,白象只能走白格。我发现,如果象能到达的终点,那么终点格子和起点格子,他们横向差与纵向差的和肯定为偶数。以此来判断象是否能够到达。
我这里如果不能走到,我给的步数是-1
第一次写的代码,没有想到其实字符可以直接相减,绕了一大圈,好多冗余代码。
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
int sKing(int col,int row){ //王的步数计算
if(col == 0 && row == 0)
return 0;
if(col == row)
return col;
if(col > row)
return col;
else
return row;
}
int sQueen(int col,int row){ //后的步数计算
if(col == 0)
if(row == 0)
return 0;
else return 1;
if(col == row)
return 1;
if(col != 0 && row == 0)
return 1;
return 2;
}
int sCar(int col,int row){ //车的步数计算
if(col == 0)
if(row == 0)
return 0;
else return 1;
if(col != 0 && row == 0)
return 1;
return 2;
}
int sElephent(int col,int row){ //象的步数计算
if((col + row) % 2 != 0)
return -1;
if(col == row)
if(col == 0)
return 0;
return 1;
return 2;
}
void Judgement(string s,int &col,int &row){ //识别格子位置
col = s[0] - 96;
row = s[1] - 48;
}
int main()
{
int King,Queen,Car,Elephent; //定义王、后、车、象
string Beginning,Ending; //起始格子和终点格子
int colB,rowB,colE,rowE; //起始格子和终点格子的横纵坐标
int col,row; //起点与终点的横纵差
while(cin >> Beginning && cin >> Ending){
if(Beginning[0] < 97 ||Beginning[0] > 104){
cout << "wrong!" << endl;
continue;
}else if(Ending[0] < 97 || Ending[0] > 104){
cout << "wrong!" << endl;
continue;
}else if(Beginning[1] < 49 || Beginning[1] > 56){
cout << "wrong!" << endl;
continue;
}else if(Ending[1] < 49 || Ending[1] > 56){
cout << "wrong!" << endl;
continue;
}
Judgement(Beginning,colB,rowB);
Judgement(Ending,colE,rowE);
col = abs(colE - colB);
row = abs(rowE - rowB);
King = sKing(col,row);
Queen = sQueen(col,row);
Car = sCar(col,row);
Elephent = sElephent(col,row);
cout << "王:" << King << " 后:" << Queen << " 车:" << Car << " 象:" << Elephent << endl;
}
return 0;
}
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
int sKing(int col,int row){ //王的步数计算
if(col == 0 && row == 0)
return 0;
if(col == row)
return col;
if(col > row)
return col;
else
return row;
}
int sQueen(int col,int row){ //后的步数计算
if(col == 0)
if(row == 0)
return 0;
else return 1;
if(col == row)
return 1;
if(col != 0 && row == 0)
return 1;
return 2;
}
int sCar(int col,int row){ //车的步数计算
if(col == 0)
if(row == 0)
return 0;
else return 1;
if(col != 0 && row == 0)
return 1;
return 2;
}
int sElephent(int col,int row){ //象的步数计算
if((col + row) % 2 != 0)
return -1;
if(col == row)
if(col == 0)
return 0;
return 1;
return 2;
}
int main()
{
int King,Queen,Car,Elephent; //定义王、后、车、象
string Beginning,Ending; //起始格子和终点格子
int col,row; //起点与终点的横纵差
while(cin >> Beginning && cin >> Ending){
if(Beginning[0] < 97 ||Beginning[0] > 104){
cout << "wrong!" << endl;
continue;
}else if(Ending[0] < 97 || Ending[0] > 104){
cout << "wrong!" << endl;
continue;
}else if(Beginning[1] < 49 || Beginning[1] > 56){
cout << "wrong!" << endl;
continue;
}else if(Ending[1] < 49 || Ending[1] > 56){
cout << "wrong!" << endl;
continue;
}
col = abs(Ending[0] - Beginning[0]);
row = abs(Ending[1] - Beginning[1]);
King = sKing(col,row);
Queen = sQueen(col,row);
Car = sCar(col,row);
Elephent = sElephent(col,row);
cout << "王:" << King << " 后:" << Queen << " 车:" << Car << " 象:" << Elephent << endl;
}
return 0;
}
书上给的C语言代码:
#include <stdio.h>
#include <math.h>
void main( )
{
int nCases, i;
scanf("%d", &nCases);
for(i = 0; i < Casesn; i++){
char begin[5], end[5]; //用begin 和end 分别存储棋子的起止位置。
scanf("%s %s", begin, end);
int x, y; //用x 和y 分别存储起止位置之间x 方向和y 方向上的距离。
x = abs(begin[0] - end[0]);
y = abs(begin[1] - end[1]);
if(x == 0 && y == 0)
printf("0 0 0 0\n"); //起止位置相同,所有棋子都走0 步。
else{
if(x < y)
printf("%d", y); // 王的步数
else
printf("%d", x);
if(x == y || x == 0 || y == 0)
printf(" 1");// 后的步数
else
printf(" 2");
if(x == 0 || y == 0)
printf(" 1"); // 车的步数
else
printf(" 2");
if(abs(x - y) % 2 != 0)
printf(" Inf\n"); // 象的步数
else if(x == y)
printf(" 1\n");
else
printf(" 2\n");
}
}
}