TopcoderArchiveVisit Topcoder
SRM18 Mar 2003
SRM 139

Skyscraper

Problem statement, definition, constraints, and public examples.

Problem Statement

We need to add wiring to a skyscraper that is shaped like a rectangular box. It is more than 5,000 feet tall, the front and back have a width of 200 feet, and its sides have a width of w. We need to wire from a point (xfront,yfront) on the front to a point (xright,yright) on the side to our right.

The coordinates of a point are given in positive feet, measured from the upper left corner of its face of the building. For example, if w were 300 and the two points were (xfront,yfront) = (25,75) and (xright,yright) = (275,500) they would appear approximately at the locations labeled p and q respectively in the picture.

          ___200____ 
         /         /|
        /         / |
       /         /  |
   w=300       300  |
     /         /    |
    /         /     |
   /___200___/      |
   |         |      |
   |         |      |
   |p        |      |
   |         |      |
   |         |     q|
   |         |      |
   |         |      |
   |         |      |
   |         |      |

Our wire must run along the outside of the building, but it may go over the top and along any sides. Wires come in integer lengths and we want to have as little slack as possible.

Create a class Skyscraper that contains a method wire that is given five int values w, xfront,yfront and xright,yright and that returns the length of the shortest wire that will reach between the two points. If a fractional distance is required, return the next higher integer.

Definition

Class:
Skyscraper
Method:
wire
Parameters:
int, int, int, int, int
Returns:
int
Method signature:
int wire(int w, int xfront, int yfront, int xright, int yright)
(be sure your method is public)

Notes

  • warning: the shortest wire may run along any of the sides of the building, not just the ones that appear in the picture.

Constraints

  • w will be between 1 and 1000 inclusive
  • xfront will be between 0 and 200 inclusive
  • yfront will be between 0 and 1000 inclusive
  • xright will be between 0 and w inclusive
  • yright will be between 0 and 1000 inclusive

Examples

  1. 300
    200
    5
    0
    5
    Returns: 0
    ���� These two points are the same point, located on the edge between the front and right side of the building.
  2. 300
    103
    50
    29
    50
    Returns: 126
    ���� A horizontal wire is the shortest. It must traverse 97 feet along the front and then 29 feet along the right side.
  3. 300
    103
    5
    29
    5
    Returns: 108
    A horizontal wire is not the shortest. The shortest wire goes diagonally up the front, across a corner of the top, and diagonally down to 29,5 on the right side. This will require about 107.52 feet of wire, so we will need a 108-foot wire.
  4. 399
    0
    270
    399
    17
    Returns: 628
Back to all problems