您的位置:首页 > 其它

百炼 1657:Distance on Chessboard

2017-07-04 21:06 260 查看
出题人丧尽天良还加个起始相同的测例。。。

//@auther zhou
//@Number 201408070203
//@start time:
//@finish time:
/*@此处注意:

*/
/* 测试数据

*/
#include<iostream>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
int elephant(int from1,int from2,int to1,int to2);
int check(int x,int y);
int king(int from1,int from2,int to1,int to2){
if (elephant(from1,from2,to1,to2)==1){
return abs(from1-to1);
}
else if(from1==to1) return abs(from2-to2);
else if(from2==to2) return abs(from1-to1);
else{
int x=abs(from1-to1);
int y=abs(from2-to2);
//int m=min(x,y);
return max(x,y);
}
}
int queen(int from1,int from2,int to1,int to2){
if((from1==to1)&(from2==to2)) return 0;
if((from1==to1)||(from2==to2)) return 1;
else if (elephant(from1,from2,to1,to2)==1) return 1;
else return 2;
}
int car(int from1,int from2,int to1,int to2){
if((from1==to1)&(from2==to2)) return 0;
if((from1==to1)||(from2==to2)) return 1;

else return 2;
}
int elephant(int from1,int from2,int to1,int to2){
if((from1==to1)&(from2==to2)) return 0;
if(abs(to1-from1)==abs(from2-to2)){
return 1;
}
else{
int worb1=check(from1,from2);
int worb2=check(to1,to2);
//cout<<"\nwORb1:"<<worb1<<"wORb2:"<<worb2<<endl;
if(worb1==worb2) {
return 2;
}
return 3;
}

}

int check(int x,int y){//白0 黑1
if(x%2==1){//奇行
if(y%2==1){
return 1;
}
else return 0;
}
else{//偶行
if(y%2==1){
return 0;
}
else return 1;
}
}

void judge(int from1,int from2,int to1,int to2){
cout<<king(from1,from2,to1,to2)<<" ";
cout<<queen(from1,from2,to1,to2)<<" ";
cout<<car(from1,from2,to1,to2)<<" ";

if(elephant(from1,from2,to1,to2)<3) cout<<elephant(from1,from2,to1,to2)<<endl;
else cout<<"Inf"<<endl;

}
int main(){
int k;
cin>>k;
getchar();
char from,to;
int f,t;
string temp;

for(int i=0;i<k;i++){
getline(cin,temp);
//cout<<temp<<endl;

//处理,化到对应的行和列里,然后调用函数。
int from1,from2,to1,to2;
from1=8-temp[0]+'a';
from2=temp[1]-'0';
to1=8-temp[3]+'a';
to2=temp[4]-'0';
//cout<<from2<<from1<<" "<<to2<<to1<<endl;
judge(from2,from1,to2,to1);
}
return 0;
}


总时间限制: 
1000ms 内存限制: 65536kB

描述国际象棋的棋盘是黑白相间的8 * 8的方格,棋子放在格子中间。如下图所示:



王、后、车、象的走子规则如下:
王:横、直、斜都可以走,但每步限走一格。

后:横、直、斜都可以走,每步格数不受限制。

车:横、竖均可以走,不能斜走,格数不限。

象:只能斜走,格数不限。

写一个程序,给定起始位置和目标位置,计算王、后、车、象从起始位置走到目标位置所需的最少步数。

输入第一行是测试数据的组数t(0 <= t <= 20)。以下每行是一组测试数据,每组包括棋盘上的两个位置,第一个是起始位置,第二个是目标位置。位置用"字母-数字"的形式表示,字母从"a"到"h",数字从"1"到"8"。

输出对输入的每组测试数据,输出王、后、车、象所需的最少步数。如果无法到达,就输出"Inf".
样例输入
2
a1 c3
f5 f8


样例输出
2 1 2 1
3 1 1 Inf


来源
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: