注意起点与终点相同的情况 /* 1657 Distance on Chessboard */ #include <iostream> #include <cstdio> #include <cstdlib> #include <string> #include <cmath> using namespace std; int max(int a, int b) { return a > b ? a : b; } int main() { int x1, y1, x2, y2; int jx, jy, j; // abs bool b; char str[6]; int cas; scanf("%d", &cas); getchar(); while (cas --) { gets(str); x1 = str[0] - 'a' + 1; y1 = str[1] - '0'; x2 = str[3] - 'a' + 1; y2 = str[4] - '0'; if (x1 == x2 && y1 == y2) { cout << "0 0 0 0" << endl; continue; } jx = abs(x1 - x2); jy = abs(y1 - y2); j = (jx + jy) % 2; b = (x1 == x2 || y1 == y2); cout << max(jx, jy) << " "; if (b || !j) cout << "1 "; else cout << "2 "; if (b) cout << "1 "; else cout << "2 "; if (j) cout << "Inf" << endl; else if (jx == jy) cout << "1" << endl; else cout << "2" << endl; } }