1.

/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, WS10/11
* Problem: 808 Bee breeding
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&page=show_problem&problem=749
*
* @author Patrick Bédat
* @version 1.0, 10/27/2010
*
* Method : Ad-Hoc
* Status : Accepted
* Runtime: 1.672
*/
import java.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class Main
{

private static Map<Integer, Point> fieldCoords = new HashMap<Integer, Point>();

public static void main(String... args) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

StringTokenizer st = new StringTokenizer(reader.readLine());

int field1 = Integer.parseInt(st.nextToken());
int field2 = Integer.parseInt(st.nextToken());

while (field1 != 0 && field2 != 0)
{
if (!fieldCoords.containsKey(Math.max(field1, field2)))
calculateCoords(Math.max(field1, field2));

int distance = distance(fieldCoords.get(field1), fieldCoords.get(field2));

System.out.printf("The distance between cells %d and %d is %d.%n", field1, field2, distance);

st = new StringTokenizer(reader.readLine());

field1 = Integer.parseInt(st.nextToken());
field2 = Integer.parseInt(st.nextToken());
}
}

private static void calculateCoords(int field2)
{
int radius = 1;
int x = 0;
int y = 0;

fieldCoords.put(1, new Point(0, 0));

int currentField = 2;
while (currentField <= field2)
{
x -= 2;
fieldCoords.put(currentField++, new Point(x, y));

for (int j = 0; j < radius - 1; j++)
{
x--;
y++;
fieldCoords.put(currentField++, new Point(x, y));
}

for (int j = 0; j < radius; j++)
{
x++;
y++;
fieldCoords.put(currentField++, new Point(x, y));
}

for (int j = 0; j < radius; j++)
{
x += 2;
fieldCoords.put(currentField++, new Point(x, y));
}
for (int j = 0; j < radius; j++)
{
x++;
y--;
fieldCoords.put(currentField++, new Point(x, y));
}
for (int j = 0; j < radius; j++)
{
y--;
x--;
fieldCoords.put(currentField++, new Point(x, y));
}

for (int j = 0; j < radius; j++)
{
x -= 2;
fieldCoords.put(currentField++, new Point(x, y));
}

radius++;
}
}

private static int distance(Point p1, Point p2)
{
int dx = Math.abs(p1.x - p2.x);
int dy = Math.abs(p1.y - p2.y);

if (dx > dy)
return (dx - dy) / 2 + dy;
else
return dy;
}
}


2.

package problemSetVolumes.volume008;

import java.util.Scanner;

/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, SS10
* Problem: 808 - Bee Breeding
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=10&page=show_problem&problem=749
*
* @author Siegfried Ippisch
* @version 1.0
*
* Method : -
* Status : Accepted
* Runtime: 0.992
*/
public class BeeBreeding {

static class Node{
int x,y;
Node(int x, int y){
this.x = x; this.y = y;
}

int distance(Node other){
int dx = Math.abs(this.x - other.x);
int dy = Math.abs(this.y - other.y);
dy -= dx;
if(dy < 0) dy=0;
return dx+dy/2;
}

public String toString(){
return "("+x+","+y+")";
}
}

static Node[] list;

static void init(int n){
list = new Node[n];

int i = 1;
int l = 1;
int x=0, y=0;

try{
while(true){

list[i++] = new Node(x,y);
y-=2;

for(int j=1; j<l; j++){
list[i++] = new Node(x,y);
x--; y--;
}

for(int j=0; j<l; j++){
list[i++] = new Node(x,y);
x--; y++;
}

for(int j=0; j<l; j++){
list[i++] = new Node(x,y);
y+=2;
}

for(int j=0; j<l; j++){
list[i++] = new Node(x,y);
x++; y++;
}

for(int j=0; j<l; j++){
list[i++] = new Node(x,y);
x++; y--;
}

for(int j=0; j<l; j++){
list[i++] = new Node(x,y);
y-=2;
}

l++;
}
} catch(ArrayIndexOutOfBoundsException e){}
}

public static void main(String[] args){
init(10001);
Scanner in = new Scanner(System.in);

int a = in.nextInt(), b = in.nextInt();
while( a != 0 && b != 0){
System.out.println("The distance between cells "+a+" and "+b+" is "+list[a].distance(list[b])+".");
a = in.nextInt();
b = in.nextInt();
}

in.close();
}

}



3.

/**
* link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=10&page=show_problem&problem=749
* ACM 808 Bee Breeding :)
* Status: Accepted
* Time: 0.640
* @author Christoph Miesel
*/

import java.io.*;
import java.util.*;
public class BeeBreeding
{
static int distance(int x, int y, int x2, int y2)
{
int dist = 0;
int deltaX = Math.abs(x2-x);
int deltaY = Math.abs(y2-y);
if(deltaY >= deltaX)
dist = deltaY;
else dist = deltaY + (deltaX-deltaY)/2;
return dist;
}

public static void main(String... args) throws Exception
{

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = reader.readLine();
StringTokenizer token;
token = new StringTokenizer(line);
int first = Integer.parseInt(token.nextToken());
int second = Integer.parseInt(token.nextToken());
while(first != 0)
{

int col = 0;
int row = 0;
int col2 = 0;
int row2 = 0;

int Wabe = 1;
int Wabe2 = 1;
int zyklus = 0;
while(Wabe < first)
{
zyklus++;
Wabe += zyklus * 6;
}
// Sektor 1
if(Wabe-zyklus*3 < first && Wabe-zyklus > first)
{
col = - zyklus + first - (Wabe-zyklus*3);
}
// Sektor 2
if(Wabe-zyklus <= first && Wabe >= first)
{
col = zyklus;
}
// Sektor 3
if(Wabe-4*zyklus > first && Wabe - 6*zyklus < first)
{
col = -zyklus + (Wabe-zyklus*4) - first;
}
// Sektor4
if(Wabe - zyklus*3 >= first && Wabe - zyklus*4 <= first)
{
col = -zyklus;
}

// Sektor 1
if(Wabe - zyklus*3 < first && Wabe - zyklus > first)
{
row = 2*zyklus - Math.abs(Wabe-2*zyklus-first);
}
// Sektor 2
if(Wabe >= first && Wabe - zyklus <= first)
{
if(zyklus % 2 == 0)
row = -zyklus + (Wabe - first)*2;
else
row = -zyklus + (Wabe - first)*2;
}
// Sektor 3
if(Wabe - 4*zyklus > first && Wabe - 6*zyklus < first)
{
row = -2*zyklus + Math.abs(Wabe-5*zyklus-first);
}
// Sektor 4
if(Wabe - 3*zyklus >= first && Wabe - zyklus*4 <= first)
{
if(zyklus % 2 == 0)
row = -zyklus + (first - (Wabe - 4*zyklus))*2;
else
row = -zyklus + (first - (Wabe - 4 * zyklus))*2;
}



zyklus = 0;
while(Wabe2 < second)
{
zyklus++;
Wabe2 += zyklus * 6;
}
// Sektor 1
if(Wabe2-zyklus*2-zyklus < second && Wabe2-zyklus*2+zyklus > second)
{
col2 = - zyklus + second - (Wabe2-zyklus*2-zyklus);
}
// Sektor 2
if(Wabe2-zyklus <= second && Wabe2 >= second)
{
col2 = zyklus;
}
// Sektor 3
if(Wabe2-4*zyklus > second && Wabe2 - 6*zyklus < second)
{
col2 = -zyklus + (Wabe2-zyklus*4) - second;
}
// Sektor4
if(Wabe2 - zyklus*3 >= second && Wabe2 - zyklus*4 <= second)
{
col2 = -zyklus;
}



// Sektor 1
if(Wabe2 - zyklus*3 < second && Wabe2 - zyklus > second)
{
row2 = 2*zyklus - Math.abs(Wabe2-2*zyklus-second);
}
// Sektor 2
if(Wabe2 >= second && Wabe2 - zyklus <= second)
{
if(zyklus % 2 == 0)
row2 = -zyklus + (Wabe2 - second)*2;
else
row2 = -zyklus + (Wabe2 - second)*2;
}
// Sektor 3
if(Wabe2 - 4*zyklus > second && Wabe2 - 6*zyklus < second)
{
row2 = -2*zyklus + Math.abs(Wabe2-5*zyklus-second);
}
// Sektor 4
if(Wabe2 - 3*zyklus >= second && Wabe2 - zyklus*4 <= second)
{
if(zyklus % 2 == 0)
row2 = -zyklus + (second - (Wabe2 - 4*zyklus))*2;
else
row2 = -zyklus + (second - (Wabe2 - 4 * zyklus))*2;
}
/**
* Endgültige Berechnung der Distanz mit Hilfe
* der Größen
* rowDif
* colDif
* row, row2
* col, col2
*
* Wir bewegen uns dabei vom kleineren zum größeren Objekt
* col ist KLEINERES
* col2 ist GRÖßERES
*/

System.out.println("The distance between cells "+ first +" and "
+second+" is "+distance(row,col,row2,col2)+".");
line = reader.readLine();
token = new StringTokenizer(line);
first = Integer.parseInt(token.nextToken());
second = Integer.parseInt(token.nextToken());
}

}
}