uva: 534 – Frogger (Floyd-Warshall minimax)

534 – Frogger

 

Problem analysis:

  • To execute a given sequence of jumps, a frog’s jump range obviously must be at least as long as the longest jump occuring in the sequence.
  • The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones.
  • You are given the coordinates of Freddy’s stone, Fiona’s stone and all other stones in the lake. Your job is to compute the frog distance between Freddy’s and Fiona’s stone.

 

Solution analysis:

  • It is not solvable using classic Floyd-Warshall algorithm, needs to modify as per problem requirements
  • MiniMax, MaxiMin problems can be solved using modified Floyd-Warshall

[stackoverflow]  To understand maximin paths (often called bottleneck paths) in a graph, consider the following problem. You have a road map of a country as a graph where each node represents an intersection and each edge represents a road. Each road has a weight limit, and if you drive a truck that exceeds the limit over that road it will break. You have a truck that you want to drive from some start location to some end location, and you want to put the maximum possible amount of weight on it. Given this, what path should you drive the truck in order to send the maximum possible weight?

If you think about this problem, for any path that you take in the graph, the maximum weight you can send along that path is going to be determined by the edge on that path with the minimum capacity. As a result, you want to find the path from the start to the destination whose minimum-capacity edge is maximized. The path with this property is called the maximin path or bottleneck path, and can be found with a straightforward set of modifications to mot shortest-path algorithms.

The minimax path represents the opposite idea – the path between two points that minimizes the maximum edge capacity.

 

Java solution:

 


import java.util.Scanner;

class Main_Frogger_534_FW_Minimax {

    public static void main(String[] args) {
        final int Limit=205;
        int stones,testCase=0;

        Scanner sc = new Scanner(System.in);        

        while((stones = sc.nextInt())>0)
        {
            testCase++;

            double weight[][] = new double[Limit][Limit];
            int x[]=new int[Limit], y[]=new int[Limit];
            
            //scan input for stones
            for(int i=1;i<=stones;i++)
            {
                x[i]=sc.nextInt();
                y[i]=sc.nextInt();
            }
            //initialize weights
            for(int i=1;i<=stones;i++)
                for(int j=1;j<=stones;j++)
                    weight[i][j]=weight[j][i]= Integer.MAX_VALUE;

            //compute distance of stones
            for(int i=1;i<=stones;i++)
                for(int j=1;j<=stones;j++)
                {
                    //calculate distance from x1y1 to x2y2
                    weight[i][j] =weight[j][i]= Math.sqrt( Math.pow((x[j] - x[i]),2) + Math.pow((y[j]-y[i]),2));                
                }

            //Floyd-Warshall algorithm for MiniMax
            for(int k=1;k<=stones;k++)
                for(int i=1;i<=stones;i++)
                    for(int j=1;j<=stones;j++)
                    {
                        weight[i][j] = Math.min(weight[i][j], Math.max(weight[i][k], weight[k][j]));
                    }

            //print output
            System.out.printf("Scenario #%d%n", testCase);
            System.out.printf("Frog Distance = %.3f%n%n", weight[1][2]);
        }

    }
}

Sample Input

2
0 0
3 4

3
17 4
19 4
18 5

0
Sample Output

Scenario #1
Frog Distance = 5.000

Scenario #2
Frog Distance = 1.414

Advertisements

About M Moniruzzaman
A passionate software engineer, have been developing applications on various platforms such as Android, iPhone, .Net (C#) technologies and web based ASP.NET, PHP, JavaScript, jQuery technologies for more than 10 years. Especially I have expertise on developing applications for Android and iPhone, as well as service oriented, client-server based applications where clients will be reside on Android/iPhone that communicate with WCF(.NET) service hosted on server. I have completed certification in Microsoft Certified Professional Developer (MCPD) on .Net 4 . I have completed my graduation in -- B.Sc. (Engineering) in Computer Science and Engineering, ShahJalal University of Science and Technology, Bangladesh. Thanks, M. Moniruzzaman (Zaman)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: