首页 > ACM题库 > POJ 2253 Frogger [解题报告] Java
2013
11-11

POJ 2253 Frogger [解题报告] Java

Frogger

问题描述 :

Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists’ sunscreen, he wants to avoid swimming and instead reach her by jumping.

Unfortunately Fiona’s stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps.

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.

输入:

The input will contain one or more test cases. The first line of each test case will contain the number of stones n (2<=n<=200). The next n lines each contain two integers xi,yi (0 <= xi,yi <= 1000) representing the coordinates of stone #i. Stone #1 is Freddy's stone, stone #2 is Fiona's stone, the other n-2 stones are unoccupied. There's a blank line following each test case. Input is terminated by a value of zero (0) for n.

输出:

For each test case, print a line saying “Scenario #x” and a line saying “Frog Distance = y” where x is replaced by the test case number (they are numbered from 1) and y is replaced by the appropriate real number, printed to three decimals. Put a blank line after each test case, even after the last one.

样例输入:

2
0 0
3 4

3
17 4
19 4
18 5

0

样例输出:

Scenario #1
Frog Distance = 5.000

Scenario #2
Frog Distance = 1.414

解题代码:

//* @author
//import java.io.File;
//import java.io.FileNotFoundException;
//import java.util.Arrays;
import java.util.Scanner;

public class Main{
Scanner cin = new Scanner(System.in);

int n;
int cases;
int stone[][];
double map[][];

public void inPut() {
   //File f = new File("D:\\ACM\\POJ解题\\2253\\test.txt");
  // cin = new Scanner(f);

   while (true) {
    cases ++;
    n = cin.nextInt();
    if (n == 0) {
     return;
    }
    stone = new int[n][2];
    map = new double[n][n];
    for (int i = 0; i < n; i++) {
     stone[i][0] = cin.nextInt();
     stone[i][1] = cin.nextInt();
    }

    for (int i = 0; i < n; i++) {
     for (int j = 0; j < n; j++) {
      map[i][j] = Math.sqrt((stone[i][0] - stone[j][0])
        * (stone[i][0] - stone[j][0])
        + (stone[i][1] - stone[j][1])
        * (stone[i][1] - stone[j][1]));
     }
    }

    solve();
   }
}

private void solve() {
//   System.out.println(Arrays.deepToString(map));
   System.out.println("Scenario #" + cases);
  
   System.out.printf("Frog Distance = %.3f\n\n", floyd());
}

private double floyd() {
   for (int i = 0; i < n; i++) {
    for (int k = 0; k < n; k++) {
     for (int j = 0; j < n; j++) {

//这个if语句里面的东西有改变,根据题意是要求两个石头之间的所有路径中最大跳(necessary jump range)最小的

//这样就能把从石头A跳到石头B的所有路径中最大跳最小的值存到map[A][B]中,最后map[0][1]中存的就是所要的值
      if (map[k][i] != 0
        && map[i][j] != 0
        && map[k][j] > (map[k][i] > map[i][j] ? map[k][i]
          : map[i][j])) {
       map[k][j] = (map[k][i] > map[i][j] ? map[k][i] : map[i][j]);
      }
     }
    }
   }
   return map[0][1];
}

public static void main(String[] args) {
   new Main().inPut();
}

}

  1. 第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。