第十三届蓝桥杯【蜂巢】最简单的解法

第十三届蓝桥杯【蜂巢】最简单的解法

  • 题目:
    在这里插入图片描述
  • 思路
  1. 总共有六个方向如下,可以按如下图方式作为x轴(这里的方向标反了,标的方向是x轴的负方向)和y轴:
    在这里插入图片描述
  2. 此时六个方向 0 − 5 0-5 05 可以用如下数组操作:
    i n t [ ] [ ] d i r s = n e w i n t [ ] [ ] − 1 , 0 , − 1 , 1 , 0 , 1 , 1 , 0 , 1 , − 1 , 0 , − 1 ; int[][] dirs = new int[][]{{-1, 0}, {-1, 1}, {0, 1}, {1, 0}, {1, -1}, {0, -1}}; int[][]dirs=newint[][]1,0,1,1,0,1,1,0,1,1,0,1;
    0 0 0方向,走x坐标减一;往 1 1 1 方向走,x坐标减一,y坐标加一;往 2 2 2方向走,y坐标加一;往 3 3 3 方向走,x坐标加1;往 4 4 4 方向走,则x坐标加一,y坐标减一;往 5 5 5方向走则y坐标减一。
  3. 然后,我们现在只关心让第一个点 ( x 1 , y 1 ) (x1,y1) (x1,y1) 作为左边的点(例如图中 B B B 点),让第二点 ( x 2 , y 2 ) (x2,y2) (x2,y2) 为在右边的点(例如图中 C C C 点),然后我们只要考虑两种情况:
  1. 当第一个点在第二点左上方时,途中 B B B C C C 点的位置关系,此时 B B B 点在坐标中的位置是 ( − 5 , 3 ) (-5,3) (5,3),而 C C C点位置为 ( 2 , 1 ) (2,1) (2,1) ,从左上往右下走,x坐标在加,y坐标在减,故此时最短距离为 B B B从左上向右下走2步到达 ( − 3 , 1 ) (-3,1) (3,1) ,再向右走5步就能到达 C C C点,此时有 a = M a t h . a b s ( − 5 − 2 ) = 7 , b = M a t h . a b s ( 3 − 1 ) = 2 a = Math.abs(-5-2)=7,b=Math.abs(3-1)=2 a=Math.abs(52)=7b=Math.abs(31)=2,故此时距离为 d i s = M a t h . m a x ( a , b ) = 7 dis = Math.max(a,b)=7 dis=Math.max(a,b)=7 ,即能得到此时两点的距离公式为 d i s t = M a t h . m a x ( M a t h . a b s ( x 1 − x 2 ) , M a t h . a b s ( y 1 − y 2 ) ) dist = Math.max(Math.abs(x1-x2), Math.abs(y1-y2)) dist=Math.max(Math.abs(x1x2),Math.abs(y1y2))
  2. 当第一个点在第二个点左下方时,图中 O O O 点和 C C C 点的位置关系,此时 O O O 点坐标为 ( 0 , 0 ) (0, 0) (0,0) C C C 点坐标为 ( 2 , 1 ) (2, 1) (2,1),此时 O O O 点到 C C C 点最短距离为 O O O 先向右走两步,再向上(Y)走一步到达 C C C ,此时有 a = M a t h . a b s ( 0 − 2 ) = 2 , b = M a t h . a b s ( 0 − 1 ) = 1 a = Math.abs(0-2)=2, b = Math.abs(0-1)=1 a=Math.abs(02)=2,b=Math.abs(01)=1,故此时距离为 d i s = a + b = 3 dis = a + b = 3 dis=a+b=3,即此时距离公式为 d i s t = M a t h . a b s ( x 1 − x 2 ) + M a t h . a b s ( y 1 − y 2 ) dist = Math.abs(x1-x2) + Math.abs(y1-y2) dist=Math.abs(x1x2)+Math.abs(y1y2)
  3. 当第一个点在第二点的左方时放过来即可(比如 B B B C C C 的右上方等价于 C C C B B B 的左下方, B B B C C C 的右下方等价于 C C C B B B 的左上方)。
  • AC代码如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 65536);
    static StringTokenizer tokenizer = new StringTokenizer("");

    public static void main(String[] args) {
        int[][] dirs = new int[][]{{-1, 0}, {-1, 1}, {0, 1}, {1, 0}, {1, -1}, {0, -1}};
        int d1 = nextInt(); long p1 = nextLong(), q1 = nextLong();
        int d2 = nextInt(); long p2 = nextLong(), q2 = nextLong();
        long x1 = 0, y1 = 0, x2 = 0, y2 = 0;
        x1 += p1 * dirs[d1][0] + q1 * dirs[(d1 + 2) % 6][0];
        y1 += p1 * dirs[d1][1] + q1 * dirs[(d1 + 2) % 6][1];
        x2 += p2 * dirs[d2][0] + q2 * dirs[(d2 + 2) % 6][0];
        y2 += p2 * dirs[d2][1] + q2 * dirs[(d2 + 2) % 6][1];
        System.out.println(getDistance(x1, y1, x2, y2));
    }

    public static long getDistance(long x1, long y1, long x2, long y2) {
        if (x1 > x2) return getDistance(x2, y2, x1, y1); // 如果 (x1, y1)在右边,则翻转一下。
        long a = Math.abs(x1 - x2), b = Math.abs(y1 - y2); 
        if (y1 >= y2) return Math.max(a, b); // 当第一个点在左上方时
        return a + b; // 当第一个点在左下方时
    }

    private static String next() {
        while (!tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return tokenizer.nextToken();
    }

    public static int nextInt() { return Integer.parseInt(next()); }

    public static long nextLong() { return Long.parseLong(next()); }
}
  • 20
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值