The Maze

3.5

20 votes
Probablity, Ad-Hoc, Hard, Basic Probability, Mathematics, 難しい
Problem

This is an approximate problem.

Let s be an N×N matrix. We can distinguish four types of cells in this matrix: start cell, finish cell, empty cell, and cell with an obstacle. You can change at most X cells from the type "empty cell" to type "cell with an obstacle" and at most Y cells from the type "cell with an obstacle" to type "empty cell".

A path of size t in matrix s is a collection of cells (x1,y1),(x2,y2),,(xt,yt) (1xi,yiN) such that |xixi+1|+|yiyi+1|=1 (1i<t) and every (xi,yi) is not a cell with an obstacle.

It is guaranteed that there is at least one path from every start cell to every finish cell. Please note that the final matrix must also satisfy this condition.

Assume that the matrix has A start cells and B finish cells. A pointer will be placed in one of the A start cells and in each one with the same probability of 1A. In the next moves, we will move the pointer in one of four adjacent cells (from (x,y) to {(x+1,y),(x,y1),(x1,y),(x,y+1)}), whenever possible. If it is not possible to move the pointer in a certain direction (in case there is an obstacle in that cell or it will go outside the matrix), it will remain in its initial position. Moreover, at the ith(1iC) move, we will move our pointer to the left with probability li, to right with probability ri, up with probability ui ,and down with probability di. Note that after the pointer has reached a finish cell, it will stop there and will not move further.

Let Dt,x,y (1x,yN,1tC) be the probability that we will end up in cell (x,y) after t moves. You need to change matrix s, such that Ci=1(x,y) is a finish cellDi,x,yi is minimized.

Note that the up direction of (x,y) is (x+1,y) and towards the right is (x,y+1).

Input format

  • First line: Numbers  N,X , and Y
  • Next N lines: N characters 
    • The jth character on the (i+1)th line represents si,j.
    • If si,j=A then (i,j) is a start cell, if si,j=B then (i,j) is a finish cell, if si,j=. then (i,j) is an empty cell, else if si,j=X then (i,j) is a cell with an obstacle.
  •  (i+N+1)th line: Four reals - ri,ui,li,di(0.1li,ri,di,ui0.7,li+ri+di+ui=1).

Output format

Print N lines where the ith line comprises N characters - vi,1,vi,2,,vi,N (vi,j{A,B,.,X}), where v is the modified matrix s

Note that there must be at most X cells (x,y) with sx,y=.,vx,y=X and at most Y cells with sx,y=X,vx,y=.. If sx,y{A,B}, then vx,y must be equal to sx,y.

Scoring

Your scoring will be equal to Ci=1(x,y) with vx,y=BDi,x,yi.

Test generation

  • C=105
  • N=50
  • For the first 50 tests, X=Y=100 and there are 300 cells with obstacles.
  • For the next 50 tests, X=Y=25 and there are 100 cells with obstacles.
  • All the start cells, finish cells, and cells with obstacles are distributed across the matrix with the same probability.
  • The tests 10t+k10t+k+50 (1k10,0t4) have exactly t+4 start cells and exactly t+4 finish cells.
Sample Input
4 1 1
..XA
B...
XXA.
B...
0.283641 0.153956 0.325699 0.236703
0.275974 0.401394 0.192051 0.130581
0.119227 0.123525 0.593442 0.163805
0.599573 0.113157 0.107362 0.179908
0.266159 0.413543 0.17684 0.143459
0.187402 0.294855 0.164145 0.353597
0.163471 0.412708 0.198182 0.225639
0.105335 0.104499 0.179505 0.610661
0.264149 0.186897 0.104731 0.444223
0.224816 0.374595 0.164892 0.235697
...
Sample Output
..XA
B...
X.A.
B...
Time Limit: 10
Memory Limit: 256
Source Limit:
Explanation

Because the input is too big, the complete version is omitted. You can download the full test here.

Note that if I did not change matrix s, the score would be equal to 38.185658, compared with current score of 34.185303.

Below are attached matrices D0,D1,D2,D3.

D0=[0000.50000000.500000], D1=[0000.423022000.1183520.07697800.1628500.141821000.0769780], D2=[0000.26327500.04399460.03023820.24222300.03127540.1297360.070037300.08015050.03089850.0781698], D3=[00.0072065300.2704310.02610820.02306770.1751950.076478300.1141150.05408840.06654370.04756470.03210040.07578780.0313112]

Editor Image

?