skip to Main Content

I am trying to create a code that will solve the CCC 2018 J3 Problem "Are We there Yet?".

There are 5 cities, and would like to calculate the distance per city.

enter image description here

Expected output:
enter image description here

Link: https://cemc.math.uwaterloo.ca/contests/computing/past_ccc_contests/2018/stage%201/juniorEF.pdf

My code is this:

 /// GET/api/J3/DistanceCalculator/{distance1}/{distance2}/{distance3}/{distance4} 
    /// api/J3/DistanceCalculator/3/10/12/5 
    /// Output:
    /// First Line: 0/3/13/25/30 -> 0 is the distance of the current destination
    /// Second Line: 3/0/10/22/27
    /// Third Line: 13/10/0/12/17
    /// Fourth Line: 25/22/12/0/5
    /// Fifth Line: 30/27/17/5/0
    /// </example>

    // GET api/J3/DistanceCalculator/{distance1}/{distance2}/{distance3}/{distance4}/
    [HttpGet]
    [Route("api/J3/DistanceCalculator/{distance1}/{distance2}/{distance3}/{distance4}")]
    
    public int DistanceCalculator(int distance1, int distance2, int distance3, int distance4)
    {
        //declare variables and array
        List <int> cities = new List<int> {5};
        List <int> distances = new List<int> {4};
        int currentDistance = 0;

        //loop representing 5 different starting points of 5 cities
        for (int i = 0; i < 5; i++)
        {

            

            //write distance[i]
            //Debug.WriteLine("You are starting in city: " + i);

            //start at 0 since city 1 is starting point
            int cities[0] =  0;
            //continue adding distance per number given
            int cities[i] = cities[i - 1] + distances[i-1];
             
            return cities[i+1] + " " + cities[i+2] + " " + cities[i+3] + " " + cities[i+4] + " " + cities[i+5]; 

            //Debug.WriteLine("East-------------");
            //Debug.WriteLine("You are starting in city: " + i + " and driving to city 0");distances[

            int distances[i] = cities[i + 1] - cities[i]; 


            //create a loop calculating the distance of the city from the original destination
            for (int j = i; j >= 0; j--)
            {
                int formerDistance = cities[j] - cities[j - 1];
                //Debug.WriteLine("The distance from previous city is " + j);

                //convert negative numbers into positive
                if (formerDistance < 0)
                {
                    formerDistance *= -1;
                }
                else
                {
                    formerDistance *= 1;
                }

                //print
                return formerDistance;
            }


            //show former distance 
            return formerDistances;

            //Debug.WriteLine("West--------------");
            //Debug.WriteLine("You are started in city: " + i + " and driving to city 4");

            //calculates the city from the starting point
            for (int j = i; j < 5; j++)
            {
                distances[j + 1] = distances[j] + distances[j + 1];

                int city5 = 0;
                int city4 = distances[j-1] - city5;
                int city3 = distances[j - 1] - city4;
                int city2 = distances[j - 1] - city5;
                int city1 = distances[j - 1] - city2;

                //Debug.WriteLine("The distance from previous city is " + i);
                
                //Debug.WriteLine("You are starting in city: " + j);

            }


        }
        currentDistance = distance1 + distance2 + distance3 + distance4;

        //return the distance
        return currentDistance;
    }
} 

2

Answers


  1. Let’s split the initial problem into smaller ones:

    1. Read initial data
    2. Solve for one line
    3. Solve for the table
    4. Output the solution

    Let me use console to solve the problem; you can adapt it for REST, WinForms etc.

    using System.Linq;
    
    ...
    
    private static int[] ReadData() => Console
      .ReadLine()
      .Split(' ', StringSplitOptions.RemoveEmptyEntries)
      .Select(item => int.Parse(item))
      .ToArray();
    

    Solve for one line (let it be line #index):

    private static int[] Distances(int[] value, int index) {
      int[] result = new int[value.Length + 1];
    
      for (int i = index - 1; i >= 0; --i) 
        result[i] = value[i] + result[i + 1];
    
      for (int i = index + 1; i < result.Length; ++i)
        result[i] = value[i - 1] + result[i - 1];
    
      return result;
    }
    

    Solve for the entire table:

    private static int[][] AllDistances(int[] value) => Enumerable
      .Range(0, data.Length + 1)
      .Select(i => Distances(data, i))
      .ToArray();
    

    Output

    private static void PrintSolution(int[][] value) => Console
      .WriteLine(string.Join(Environment.NewLine, value
         .Select(line => string.Join(" ", line))));
    

    Finally, let’s combine it all together

    private static void Main() {
      int[] data = ReadData();
    
      int[][] table = AllDistances(data);
    
      PrintSolution(table);
    }
    
    Login or Signup to reply.
  2. I think it’s good to apply the permutations problem from algebra lesson.

    Hence:

    distance[i+1] += distance[i];

    and then with extracting the solution from city3-city2 for example, use

    (distance[j] – distance[i]);

    to get distance 2.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search