此题看似很简单,但实际上有不少细节,WA点不少。分情况处理即可。
#include<cmath> #include<cstdio> #include<string> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> using namespace std; bool inMap(int x, int y){ return x > 0 && x < 9 && y > 0 && y < 9; } int main(){ int r1, c1, r2, c2, ans1, ans2, ans3; //freopen("in.cpp", "r", stdin); while(~scanf("%d%d%d%d", &r1, &c1, &r2, &c2)){ if(r1 == r2 && c1 == c2) printf("0 0 0 "); else{ if(r1 == r2 || c1 == c2) ans1 = 1; else ans1 = 2; if(abs(r1 - r2) == abs(c1 - c2)) ans2 = 1; else if(r1 == r2 || c1 == c2){ int tmp1 = abs(r1 - r2); int tmp2 = abs(c1 - c2); if((tmp1 % 2 == 0 && tmp1) || (tmp2 % 2 == 0 && tmp2)) ans2 = 2; else ans2 = 0; } else{ int flag = 0, x[4], y[4]; for(int i = 1;i < 8;i ++){ x[0] = r1 + i, y[0] = c1 + i; x[1] = r1 - i, y[1] = c1 + i; x[2] = r1 + i, y[2] = c1 - i; x[3] = r1 - i, y[3] = c1 - i; for(int j = 0;j < 4;j ++){ if(inMap(x[j], y[j]) && abs(x[j] - r2) == abs(y[j] - c2)){ flag = 1; break; } } if(flag) break; } if(flag) ans2 = 2; else ans2 = 0; } if(abs(r1 - r2) == abs(c1 - c2)) ans3 = abs(r1 - r2); else{ int tmp1 = abs(r1 - r2) + abs(abs(c1 - c2) - abs(r1 - r2)); int tmp2 = abs(c1 - c2) + abs(abs(r1 - r2) - abs(c1 - c2)); ans3 = min(tmp1, tmp2); } printf("%d %d %d ", ans1, ans2, ans3); } } return 0; }