首页 > 专题系列 > Java解POJ > POJ 1870 Bee Breeding [解题报告] Java
2013
11-10

POJ 1870 Bee Breeding [解题报告] Java

Bee Breeding

问题描述 :

Professor B. Heif is conducting experiments with a species of South American bees that he found during an expedition to the Brazilian rain forest. The honey produced by these bees is of superior quality compared to the honey from European and North American honey bees. Unfortunately, the bees do not breed well in captivity. Professor Heif thinks the reason is that the placement of the different maggots (for workers, queens, etc.) within the honeycomb depends on environmental conditions, which are different in his laboratory and the rain forest.

As a first step to validate his theory, Professor Heif wants to quantify the difference in maggot placement. For this he measures the distance between the cells of the comb into which the maggots are placed. To this end, the professor has labeled the cells by marking an arbitrary cell as number 1, and then labeling the remaining cells in a clockwise fashion, as shown in the following figure.



__ __ __ __
__/ \__/ \__/ \__/ \__
__/ \__/ \__/53\__/ \__/ \__
/ \__/ \__/52\__/54\__/ \__/ \
\__/ \__/51\__/31\__/55\__/ \__/
/ \__/50\__/30\__/32\__/56\__/ \
\__/49\__/29\__/15\__/33\__/57\__/
/ \__/28\__/14\__/16\__/34\__/ \
\__/48\__/13\__/ 5\__/17\__/58\__/
/..\__/27\__/ 4\__/ 6\__/35\__/ \
\__/47\__/12\__/ 1\__/18\__/59\__/
/..\__/26\__/ 3\__/ 7\__/36\__/ \
\__/46\__/11\__/ 2\__/19\__/60\__/
/..\__/25\__/10\__/ 8\__/37\__/ \
\__/45\__/24\__/ 9\__/20\__/61\__/
/..\__/44\__/23\__/21\__/38\__/ \
\__/70\__/43\__/22\__/39\__/62\__/
/ \__/69\__/42\__/40\__/63\__/ \
\__/ \__/68\__/41\__/64\__/ \__/
/ \__/ \__/67\__/65\__/ \__/ \
\__/ \__/ \__/66\__/ \__/ \__/
\__/ \__/ \__/ \__/ \__/
\__/ \__/ \__/ \__/



For example, two maggots in cells 19 and 30 are 5 cells apart. One of the shortest paths connecting the two cells is via the cells 19 – 7 – 6 – 5 – 15 – 30, so you must move five times to adjacent cells to get from 19 to 30.

Professor Heif needs your help to write a program that computes the distance, defined as the number of cells in a shortest path, between any pair of cells.

输入:

The input consists of several lines, each containing two integers a and b (a,b <= 10000), denoting numbers of cells. The integers are always positive, except in the last line where a=b=0 holds. This last line terminates the input and should not be processed.

输出:

For each pair of numbers (a,b) in the input file, output the distance between the cells labeled a and b. The distance is the minimum number of moves to get from a to b.

样例输入:

19 30
0 0

样例输出:

The distance between cells 19 and 30 is 5.

解题代码:

import java.util.Scanner;  
      
    public class Main{  
      
          
        public static void main(String[] args) {  
              
            Scanner scan = new Scanner(System.in);  
              
            while(true){  
                int a = scan.nextInt();  
                int b = scan.nextInt();  
                if(a==0&&b==0)  
                    break;  
                A3 p = new A3(0,0);  
                p = cal(a);  
                A3 p1 = new A3(p.x,p.y);  
                p = cal(b);  
                A3 p2 = new A3(p.x,p.y);  
                int x = p1.x-p2.x;  
                int y = p1.y-p2.y;  
                int ans;  
                if(x*y<=0)  
                    ans = Math.max(Math.abs(x), Math.abs(y));  
                else  
                    ans = Math.abs(x+y);  
                System.out.printf("The distance between cells %d and %d is %d.",a,b,ans);  
                System.out.println();  
            }  
      
        }  
      
        public static A3 cal(int a) {  
            if(a == 1){  
                return new A3(0,0);  
            }  
            int x;  
            int y;  
            int n = 0;  
            while(3*(n-1)*n+1< a)     // 3*(n-1)*n+1  为第n圈最后一个数  
                n++;  
            n--;  
            a -= 3*(n-1)*n+1;  
              
            if(a<=n){               // 详细注释见我的   poj 2265  
                x = n;  
                y = -a;  
            }else if(a>n&&a<=2*n){  
                x = 2*n-a;  
                y = -n;  
            }else if(a>2*n&&a<=3*n){  
                x = 2*n-a;  
                y = -n-x;  
            }else if(a>3*n&&a<=4*n){  
                x = -n;  
                y = a-3*n;  
            }else if(a>4*n&&a<=5*n){  
                x = a-5*n;  
                y = n;  
            }else{  
                x = a-5*n;  
                y = n-x;  
            }  
            A3 p = new A3(x,y);  
            return p;  
        }  
      
    }  
      
    class A3{  
        int x;  
        int y;  
        public A3(int x, int y) {  
            super();  
            this.x = x;  
            this.y = y;  
        }  
          
    }

  1. I like your publish. It is great to see you verbalize from the coronary heart and clarity on this essential subject matter can be easily noticed.

  2. “可以发现,树将是满二叉树,”这句话不对吧,构造的树应该是“完全二叉树”,而非“满二叉树”。