基本算法思维——走方格(给定步数是否可达)

问题:在一方格中,从源点出发,能否通过指定步数达到另一个点,在到达指定位置之前可以任意移动(上,下,左,右)。

解决:

(1)方格中两点之间的最小距离是固定的的。

(2)多余最小距离的值,一定是偶数倍。

(3)考虑为负数的情况。

code:

 1 import java.util.Scanner;
 2 public class Main{
 3     public static void main(String args[]) {
 4         Scanner s = new Scanner(System.in);
 5         long x = s.nextLong();
 6         x = Math.abs(x);
 7         long y = s.nextLong();
 8         y = Math.abs(y);
 9         long sum = s.nextLong();
10         long min = x+y;
11         if(sum<(min)) {
12             System.out.println("No");
13         }else {
14             if((sum-min)%2!=0) {
15                 System.out.println("No");
16             }else {
17                 System.out.println("Yes");
18             }
19         }
20         s.close();
21     }
22 }
原文地址:https://www.cnblogs.com/dream-flying/p/12780714.html