Member Search

Problem Statement

Introduction

A mining company would like to determine if a particular mountain has heavy metals in it, without resorting to expensive digging operations. One idea that they have had is to try and generate a density image of a horizontal cross section of the mountain. However, the only method they have at their disposal to do this is to shoot high energy electromagnetic waves (like X-rays) through the mountain, and measure them on the other side.

We will model this problem by first imagining that the cross section of the mountain can be laid out on a grid, where each unit square has constant density. Next, we pick two coordinates and shoot the high energy electromagnetic waves from one to the other. We then measure the fraction of these waves that reaches the destination. (Picture a tight beam going from one point to another). The goal is to determine the density of each unit square on the grid, where we define density here as the fraction of waves deflected per unit length. For instance, a density of 0.03 means that 3% of the wave is deflected for each unit travelled. Thus, if the wave passed through 0.6 units of 0.03 density material, 0.970.6 of the waves would remain. Of course, the measurement process isn't perfect, and the actual fraction of waves always ends up being off a bit. To model this, a random variable will be sampled from a Gaussian distribution with mean 0 and standard deviation 0.002, and this random variable will be added to the final fraction before it is returned to you. (If adding this random variable pushes the fraction below 0 or over 1, it will be adjusted to 0 or 1.) Thus, if the beam passed through 4 units of density 0.02 and 2 units of density 0.04, a fraction 0.984 * 0.962 would be expected to pass through. However, you would observe a slightly perturbed fraction, due to the random noise that would be added.

Implementation Details

You will be given the shape of the mountain as a String[], where 'X' represents a part of the mountain's cross section and a '.' represents space. You will then make a number of measurements, and return a double[] representing the density of each location in the cross section of the mountain, where the elements of the return correspond to the 'X's in the input in row major order.

More specifically, if character i of element j is an 'X', the mountain has some constant density from (i,j) to (i+1,j+1), and you cannot start or end a measurement in that unit square. To make a measurement, you should call the static method measure, in class Mountain, with parameters sx and sy representing the starting location, and fx and fy giving the ending location. The method will return the observed fraction that passes through the mountain. Note that the segment connecting (sx,sy) to (fx,fy) may travel a distance between 0 and sqrt(2) through each unit square. Also, notice that the way the examples are displayed, (0,0) in the upper left, and (101,101) in the lower right.

Test Case Generation

Mountains will be generated on a 101x101 grid by first placing an 'X' in the center of the grid, and then randomly selecting points that are adjacent to a previously placed 'X'. This process is continued until there is at least one 'X' on ever edge, at which time the process stops immediately. Holes are then filled in.

To generate the densities, a random value between 0.0 and 0.1 will be chosen for each unit square in the mountain. A blurring process will then be used to smooth the resulting density map: each density will be set to the weighted average of it and its (up to) 4 neighbors, where each neighbor receives a weight of 0.25, while the center square has a weight of 1. This blurring will be repeated between 1 and 5 times (chosen randomly).

Scoring

Scoring will be based on the accuracy of your return, and the number of queries you make. The accuracy of your return will be measured by its squared error: for each grid location, the difference between the true density and the one you report will be squared, and this value will be summed over all locations. Added to this will be the number of queries you make, multiplied by the input costPerMeasurement, which will be a random value chosen uniformly between 0 and 0.00001. Your score for a single test case will be the difference between this sum and the squared error of the trivial solution of predicting 0.05 for every grid location (i.e. TRIVIAL_ERROR - YOUR_ERROR - MEASUREMENT_COST). Your overall score will simply be the sum of these improvements over all test cases, multiplied by 100.

Definition

Class:

DensityImaging

Method:

image

Parameters:

String[], double

Returns:

double[]

Method signature:

double[] image(String[] shape, double costPerMeasurement)

(be sure your method is public)

Available Libraries

Class:

Mountain

Method:

measure

Parameters:

double, double, double, double

Returns:

double

Sample Call:

val = Mountain.measure(sx, sy, fx, fy);

Notes

-

The time limit is 30 seconds and the memory limit is 64MB.

-

The following diagram illustrates what is meant by row-major order, where the 'X's are replaced by digits giving the order of the return

0123...4567...89..

-

Horizontal or vertical queries that run between integer coordinates (like one from (1,3) to (5,3)) will always result in a return of -1, because they go along the edge between two unit squares.

-

While the waves are travelling through air (not part of the mountain) none of them are deflected.

-

Invalid measurements (ones that start or end inside the mountain) will be counted against you in the total cost, and will result in a return of -1.

-

Your measurements may start and end right on the edge of the mountain, or anywhere else outside of it (including coordinates off the map, like negative ones).

-

You may make at most 10,000 measurements.

-

If your score is negative for any test case, it will be increased to 0.

-

A hole is a non-mountain point that does not have a path to the edge of the input which takes vertical or horizontal steps to other non-mountain points.

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2010, TopCoder, Inc. All rights reserved.