RatRoute Problem

Class

public class RatRoute { public int numRoutes(String[] enc) { // fill in this method (and others you can write) } }

Problem Statement

A psychologist wants to study instinctive route-making behavior in lab rats. She builds a two-dimensional enclosure and places blocks (each one foot wide and one foot deep) at random points inside the enclosure. She then places a piece of cheese at a random point in the enclosure and a lab rat at another random point. Here is an example setup, where a square foot of space is represented by a period, a block is represented by an X, the rat is represented by an R, and the piece of cheese is represented by a C (spaces are added for easy reading):

. R . . .
. . X . .
. . . . X
X . X . X
. . . C .

Before putting the rat in the enclosure, the psychologist shows the location of the piece of cheese to the rat. Naturally, the rat will attempt to take a path with the shortest possible distance to the cheese. But the rat isn't that intelligent so the rat will only move towards the cheese and never move away from the cheese. Assume that the rat can only move horizontally or vertically, one point at a time. In the above example, there are 3 possible routes the rat can take to get to the cheese:

  1. East 2 points, South 4 points
  2. South 2 points, East 2 points, South 2 points
  3. South 4 points, East 2 points
Create a class RatRoute that contains the method numRoutes, which takes a String[] representing the configuration of the test (each string represents one row of the maze), and returns an int representing the number of possible routes the rat can take to get to the cheese without ever increasing the distance between itself and the cheese at any point on its path (see first NOTE).

Notes and Constraints

Examples

  1. 
    enc = {
    ".R...",
    "..X..",
    "....X",
    "X.X.X",
    "...C."}
    
    return 3
    
    
    The example shown above.

  2. 
    enc = {
    "...X.C",
    "R....."}
    
    return 2
    
    
    There are 2 possible routes:
    1. East 5 points, North 1 point
    2. East 4 points, North 1 point, East 1 point

  3. 
    enc = {
    "C..X.",
    ".....",
    "X..R.",
    "....."}
    
     return 8
    

    1. North 1 point, West 1 point, North 1 point, West 2 points
    2. North 1 point, West 2 points, North 1 point, West 1 point
    3. North 1 point, West 3 points, North 1 point
    4. West 1 point, North 2 points, West 2 points
    5. West 1 point, North 1 point, West 1 point, North 1 point, West 1 point
    6. West 1 point, North 1 point, West 2 points, North 1 point
    7. West 2 points, North 2 points, West 1 point
    8. West 2 points, North 1 point, West 1 point, North 1 point

  4. 
    enc = {
    "C........R",
    ".........."}
    
      return 1
    
    
  5. 
    "C",
    ".",
    ".",
    ".",
    ".",
    ".",
    ".",
    "X",
    ".",
    "R"}
    
    return 0
    
    

  6. 
    enc = {
    "....X",
    ".CXX.",
    ".XX..",
    "....R"}
    
    return 0
    
    
    The only way the rat can get to the cheese is to go West 4 points, North 2 points, and East 1 point. However, when the rat moves into the lower-left corner, it increases the distance between itself and the cheese, so this route does not count.