Tuesday, September 24, 2013

[Fox and Rabbit Problem]: How many rabbits can the fox eat

Problem:
Given a grid, the coordinates of a fox and the coordinates of the rabbits, find out the number of rabbits that the fox can eat.
Condition:
The fox can only eat rabbits which are in its own row, own column or in any of the diagonals.
Example configuration:

Grid

Java Code

import java.util.ArrayList;
import java.util.List;

class Coordinate {
Integer x;
Integer y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "(" + this.x.toString() + "," + this.y.toString() + ").";
}
}

public class Fox {
private static final String prefixEatString = "Fox can eat the rabbit at: ";
private static final String prefixCannotEatString = "Fox cannot eat the rabbit at: ";
private static final String prefixTotalEatableString = "In all the fox can eat ";
private static final String suffixTotalEatableString = " rabbits.";
public static void main(String[] args) {
Coordinate foxCoordinates = new Coordinate(0,0);
List rabbitCoordinates = new ArrayList();
//Initialize the coordinates of the rabbits.
for (int i =-2; i <= 2; i++) {
for (int j =-2; j <= 2; j++) {
if(foxCoordinates.getX() == i && foxCoordinates.getY() == j) {
continue;
}
rabbitCoordinates.add(new Coordinate(i, j));
}
}
int foodCount = 0;
for (int i=0; i<rabbitCoordinates.size(); i++) {
int diffX = foxCoordinates.getX() - rabbitCoordinates.get(i).getX();
int diffY = foxCoordinates.getY() - rabbitCoordinates.get(i).getY();
if (diffX == 0) {
foodCount++;
System.out.println(prefixEatString + rabbitCoordinates.get(i).toString());
}
else if (diffY == 0) {
foodCount++;
System.out.println(prefixEatString + rabbitCoordinates.get(i).toString());
}
else if (diffY/diffX == 1 || diffY/diffX == -1) {
foodCount++;
System.out.println(prefixEatString + rabbitCoordinates.get(i).toString());
}
else {
System.out.println(prefixCannotEatString + rabbitCoordinates.get(i).toString());
}
}
System.out.println(prefixTotalEatableString + foodCount + suffixTotalEatableString);
}
}
Output:
Fox can eat the rabbit at: (-2,-2).
Fox cannot eat the rabbit at: (-2,-1).
Fox can eat the rabbit at: (-2,0).
Fox cannot eat the rabbit at: (-2,1).
Fox can eat the rabbit at: (-2,2).
Fox cannot eat the rabbit at: (-1,-2).
Fox can eat the rabbit at: (-1,-1).
Fox can eat the rabbit at: (-1,0).
Fox can eat the rabbit at: (-1,1).
Fox cannot eat the rabbit at: (-1,2).
Fox can eat the rabbit at: (0,-2).
Fox can eat the rabbit at: (0,-1).
Fox can eat the rabbit at: (0,1).
Fox can eat the rabbit at: (0,2).
Fox cannot eat the rabbit at: (1,-2).
Fox can eat the rabbit at: (1,-1).
Fox can eat the rabbit at: (1,0).
Fox can eat the rabbit at: (1,1).
Fox cannot eat the rabbit at: (1,2).
Fox can eat the rabbit at: (2,-2).
Fox cannot eat the rabbit at: (2,-1).
Fox can eat the rabbit at: (2,0).
Fox cannot eat the rabbit at: (2,1).
Fox can eat the rabbit at: (2,2).
In all the fox can eat 16 rabbits.

No comments:

Post a Comment