#P3315. [SDOI2014] 酗酒者
[SDOI2014] 酗酒者
Description
finds that when people are in a bad mood, they tend to drink heavily. Unlike the jubilant celebration after an contestant’s victory, a drunkard often forgets the way home and wanders randomly around the streets, shouting incomprehensible words.
These days, is in a bad mood due to exams. Every night he finds a bar in the city. After getting drunk and leaving the bar, he begins to wander randomly through the city streets until, at some moment, if he happens to meet who is out watching the stars at night, she will take him home.
The city streets where and live can be modeled as a grid of rows and columns. Rows are numbered from to , and columns from to . There are intersections in total, and each intersection is denoted by coordinates . If , then and are connected by an undirected edge whose length is , representing the time needed to traverse this road. If , then and are connected by an undirected edge whose length is .
Given two points and , which are the locations of the bar that visits tonight and the place where watches the stars tonight, respectively. After leaving the bar, at each intersection, will choose uniformly at random one of the adjacent intersections and then move to it. Before reaching the next intersection, will not turn back mid-edge. Also, ’s future movement is not affected by the roads he has previously taken.
Specifically: if moves from to , he may immediately return to after arriving at . At a four-way intersection, moves in each direction with probability ; at a three-way intersection (only on the boundary), with probability for each; at a two-way intersection (only at the four corners), with probability for each.
wants to know, starting from when leaves the bar, how long she expects to wait until meeting . That is, for the given points and , what is the expected time for to go from to ?
Input Format
The first line contains , .
Then there are lines, each containing positive integers, where the entry in row and column is .
Then there are lines, each containing positive integers, where the entry in row and column is .
A single line follows with an integer , the number of queries.
Then lines follow, each containing four integers , , , .
Output Format
Output lines. For each query, print the expected time for to go from to . You may output any number of decimal places, but your answer is considered correct only if the error rate is within .
2 2
1 2
3
4
4
0 0 0 1
1 0 0 1
1 1 0 1
0 1 1 0
7.0000
10.0000
8.0000
10.0000
Hint
For of the testdata, .
For of the testdata, .
For of the testdata, .
For of the testdata, , , and .
Additionally, for of the testdata, .
Translated by ChatGPT 5
京公网安备 11011102002149号