Gravity Point
Time limit2sMemory limit512 MB
A random mass for tiles A and B is drawn from given ranges; find the chance the grid's center of gravity falls in a filled cell.
- Level
Hard8 of 10
- Topics
- Geometry, Probability, Combinatorics, Math
- Solved
- No attempts yet
Problem
You are practicing a juggle that involves a number of square tiles. They all look the same in size, but there are actually three kinds of tiles: A, B, and X. The kind of a tile can be told apart by its mass. All tiles of the same kind have exactly the same mass. The mass of type A tiles is in the range [mA1, mA2]. The mass of type B tiles is likewise in the range [mB1, mB2]. You do not know the exact numbers for types A and B. The mass of type X tiles is exactly mX.
You have just got one big object made of the tiles. The tiles are arranged in an H × W grid. All adjacent tiles are glued together along their edges. Some cells in the H × W grid can be empty.
You wanted to balance the object on a pole. You started to wonder about the center of gravity of the object. You cannot put the object on a pole if the center of gravity is on an empty cell. The center of gravity of each single square tile is at the center of the square. The center of gravity of an object that combines two objects with masses m1 and m2 is the point that divides the segment between those centers internally in the ratio m2 : m1.
Your task is to write a program that computes the probability that the center of gravity of the big object is actually on the object, not on a hole in the object. Although the exact masses are unknown for tiles A and B, the probability follows the continuous uniform distribution within the range mentioned above. You can assume the distributions are independent between A and B.
Input
The input is formatted as follows.
H W
mA1 mA2 mB1 mB2 mX
M1,1M1,2...M1,W
M2,1M2,2...M2,W
:
:
MH,1MH,2...MH,W
The first line of the input contains two integers H and W (1 ≤ H, W ≤ 50) separated by a space, where H and W are the numbers of rows and columns of the given matrix.
The second line of the input contains five integers mA1, mA2, mB1, mB2, and mX (1 ≤ mA1 < mA2 ≤ 100, 1 ≤ mB1 < mB2 ≤ 100, and 1 ≤ mX ≤ 100) separated by a space.
The following H lines, each consisting of W characters, denote the given matrix. In those H lines, A, B, and X denote a tile of type A, type B, and type X respectively, and . denotes an empty cell with no tile. There are no other characters in those H lines.
For the cell at the i-th row and j-th column, the coordinate of the top left corner is (i, j), and the coordinate of the bottom right corner is (i+1, j+1).
You may assume that the given matrix has at least one A, B, and X each, and all tiles are connected along at least one edge. Also, you may assume that the probability that the x-coordinate of the center of gravity of the object is an integer equals zero, and the probability that the y-coordinate of the center of gravity of the object is an integer equals zero.
Output
Print the probability that the center of gravity of the object is on the object. The output should not contain an error greater than 10-8.