1 条题解
-
0
方法思路
- 如果起点和终点相同,步数为0
- 象走一步的判断:只要满足|x1-x2| = |y1-y2|(即在同一对角线上),象可以一步到达
- 马走一步的判断:按照马的移动规则(8个可能方向)检查是否能一步到达
- 两步的情况有两种可能:
- 象走两步:如果(x1+y1)和(x2+y2)的奇偶性相同,可以用象走两步到达
- 马+象组合:先用马走一步,然后检查是否能用象走一步到达终点
- 其他情况都需要三步
代码实现
Java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); // 马的8个移动方向 int[][] dir = {{1, 2}, {1, -2}, {-1, 2}, {-1, -2}, {2, 1}, {2, -1}, {-2, 1}, {-2, -1}}; while (t-- > 0) { int x1 = sc.nextInt(); int y1 = sc.nextInt(); int x2 = sc.nextInt(); int y2 = sc.nextInt(); // 如果起点和终点相同 if (x1 == x2 && y1 == y2) { System.out.println(0); continue; } // 判断象走一步到达(在同一对角线上) if (Math.abs(x1 - x2) == Math.abs(y1 - y2)) { System.out.println(1); continue; } // 判断马走一步到达 boolean oneStep = false; for (int i = 0; i < 8; i++) { int nx = x1 + dir[i][0]; int ny = y1 + dir[i][1]; if (nx == x2 && ny == y2) { System.out.println(1); oneStep = true; break; } } if (oneStep) continue; // 判断是否可以两步到达 // 象走两步:如果x1+y1和x2+y2的奇偶性相同 if ((Math.abs(x1) + Math.abs(y1)) % 2 == (Math.abs(x2) + Math.abs(y2)) % 2) { System.out.println(2); continue; } // 判断马+象组合 boolean twoSteps = false; for (int i = 0; i < 8; i++) { int nx = x1 + dir[i][0]; int ny = y1 + dir[i][1]; // 检查从这个点是否可以用象一步到达终点 if (Math.abs(nx - x2) == Math.abs(ny - y2)) { System.out.println(2); twoSteps = true; break; } } if (twoSteps) continue; // 其他情况需要三步 System.out.println(3); } sc.close(); } }
Python
def min_steps(x1, y1, x2, y2): # 马的8个移动方向 knight_moves = [(1, 2), (1, -2), (-1, 2), (-1, -2), (2, 1), (2, -1), (-2, 1), (-2, -1)] # 如果起点和终点相同 if x1 == x2 and y1 == y2: return 0 # 判断象走一步到达(在同一对角线上) if abs(x1 - x2) == abs(y1 - y2): return 1 # 判断马走一步到达 for dx, dy in knight_moves: if x1 + dx == x2 and y1 + dy == y2: return 1 # 判断是否可以两步到达 # 象走两步:如果x1+y1和x2+y2的奇偶性相同 if (x1 + y1) % 2 == (x2 + y2) % 2: return 2 # 判断马+象组合 for dx, dy in knight_moves: nx, ny = x1 + dx, y1 + dy # 检查从这个点是否可以用象一步到达终点 if abs(nx - x2) == abs(ny - y2): return 2 # 其他情况需要三步 return 3 t = int(input()) for _ in range(t): x1, y1, x2, y2 = map(int, input().split()) print(min_steps(x1, y1, x2, y2))
C++
#include <iostream> #include <cmath> using namespace std; int main() { int t; cin >> t; // 马的8个移动方向 int dir[8][2] = {{1, 2}, {1, -2}, {-1, 2}, {-1, -2}, {2, 1}, {2, -1}, {-2, -1}, {-2, 1}}; while (t--) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; // 如果起点和终点相同 if (x1 == x2 && y1 == y2) { cout << 0 << endl; continue; } // 判断象走一步到达 int d = abs(x1 - x2) - abs(y1 - y2); if (d == 0) { cout << 1 << endl; continue; } // 判断马走一步到达 bool one = false; for (int i = 0; i < 8; ++i) { int dx = x1 + dir[i][0]; int dy = y1 + dir[i][1]; if (dx == x2 && dy == y2) { cout << 1 << endl; one = true; break; } } if (one) continue; // 接下来为两步的逻辑 int d2 = abs(x1 - x2) + abs(y1 - y2); if (d2 % 2 == 0) { cout << 2 << endl; continue; } // 接下来判断马 + 象的组合 bool two = false; for (int i = 0; i < 8; ++i) { int dx = x1 + dir[i][0]; int dy = y1 + dir[i][1]; int d3 = abs(dx - x2) - abs(dy - y2); if (d3 == 0) { cout << 2 << endl; two = true; break; } } if (two) continue; // 剩下的格子全都是三步到达的 cout << 3 << endl; } return 0; }
- 1
信息
- ID
- 79
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 5
- 标签
- 递交数
- 1
- 已通过
- 1
- 上传者