百炼POJ 1657 Distance on Chessboard 解题报告
2013-11-30 04:35
519 查看
分析:
王 ,是横坐标距离 和 纵坐标距离中的最大值。
皇后,如果是在一条横竖直线或者在斜线上,则输出1,否则就是输出2。
车 ,如果在一条横竖直线上,则输出1,否则输出2。
象,稍微麻烦一点,如果是在同一颜色之间移动,则肯定有值,是1或2。
不在同一颜色之间移动,就无法到达,输出Inf
还有就是要考虑到位置在不变的情况。
代码:
#include<iostream>
#include<cmath>
#include<stdlib.h>
using namespace std;
int main()
{
int t,n,x,y;
char p1[3],p2[3];
cin>>n;
while(n--){
cin>>p1>>p2;
x=abs(p1[0]-p2[0]);
y=abs(p1[1]-p2[1]);
if(x==0&&y==0){ //要考虑到位置在不变的情况
cout<<"0"<<" "<<"0"<<" "<<"0"<<" "<<"0"<<endl;
continue;
}
if(x>y)//输出王
t=x;
else
t=y;
cout<<t<<" ";
if(x==0||y==0||x==y) //输出后
cout<<"1"<<" ";
else
cout<<"2"<<" ";
if(x==0||y==0) //输出车
cout<<"1"<<" ";
else
cout<<"2"<<" ";
if(abs(x-y)%2!=0)//输出象 这里会单独解释
cout<<"Inf"<<endl;
else if(x==y)
cout<<"1"<<endl;
else
cout<<"2"<<endl;
}
return 0;
}
容易出现的错误:
compile error 995337.122487/Main.cc:32:19: error: invalid operands of types ‘__gnu_cxx::__enable_if<true, double>::__type’ and ‘int’ to binary ‘operator%’
出现这个错误是在if((x+y) % 2 != 0)时,如果这里的x+y换成abs(x-y)则会报错,原因是g++把abs()中的int转换成了double类型了。在g++中使用abs()函数总是会出错。解决办法是在头文件中加入“stdlib.h”,
王 ,是横坐标距离 和 纵坐标距离中的最大值。
皇后,如果是在一条横竖直线或者在斜线上,则输出1,否则就是输出2。
车 ,如果在一条横竖直线上,则输出1,否则输出2。
象,稍微麻烦一点,如果是在同一颜色之间移动,则肯定有值,是1或2。
不在同一颜色之间移动,就无法到达,输出Inf
还有就是要考虑到位置在不变的情况。
代码:
#include<iostream>
#include<cmath>
#include<stdlib.h>
using namespace std;
int main()
{
int t,n,x,y;
char p1[3],p2[3];
cin>>n;
while(n--){
cin>>p1>>p2;
x=abs(p1[0]-p2[0]);
y=abs(p1[1]-p2[1]);
if(x==0&&y==0){ //要考虑到位置在不变的情况
cout<<"0"<<" "<<"0"<<" "<<"0"<<" "<<"0"<<endl;
continue;
}
if(x>y)//输出王
t=x;
else
t=y;
cout<<t<<" ";
if(x==0||y==0||x==y) //输出后
cout<<"1"<<" ";
else
cout<<"2"<<" ";
if(x==0||y==0) //输出车
cout<<"1"<<" ";
else
cout<<"2"<<" ";
if(abs(x-y)%2!=0)//输出象 这里会单独解释
cout<<"Inf"<<endl;
else if(x==y)
cout<<"1"<<endl;
else
cout<<"2"<<endl;
}
return 0;
}
容易出现的错误:
compile error 995337.122487/Main.cc:32:19: error: invalid operands of types ‘__gnu_cxx::__enable_if<true, double>::__type’ and ‘int’ to binary ‘operator%’
出现这个错误是在if((x+y) % 2 != 0)时,如果这里的x+y换成abs(x-y)则会报错,原因是g++把abs()中的int转换成了double类型了。在g++中使用abs()函数总是会出错。解决办法是在头文件中加入“stdlib.h”,
相关文章推荐
- POJ 1657 Distance on Chessboard 解题报告
- POJ 1657 Distance on Chessboard 解题报告
- POJ 1657 (Distance on Chessboard)
- POJ 1657 Distance on Chessboard(搜索题)
- POJ 1657 Distance on Chessboard
- POJ 1657-Distance on Chessboard(BFS-多种方向不限步数)
- poj 1657 Distance on Chessboard(模拟贪心)
- poj 1657 Distance on Chessboard
- poj 1657 Distance on Chessboard
- poj 1657 Distance on Chessboard
- POJ 百练 1657: Distance on Chessboard
- poj 1657 Distance on Chessboard
- poj1657——Distance on Chessboard——简单题
- poj1657——Distance on Chessboard
- POJ 1657 Distance on Chessboard
- poj 1657 Distance on Chessboard
- poj1657——Distance on Chessboard
- POJ_1657_Distance on Chessboard
- Distance on Chessboard POJ - 1657 简单模拟
- 百炼 1657:Distance on Chessboard