[ACM] HDU 2262 where is the canteen (Gaussian deyuan expectation)

Source: Internet
Author: User

Where is the canteen

Problem descriptionafter a long drastic struggle with himself, ll decide to go for some snack at last. but when steping out of the dormitory, he found a serious problem: He can't remember where is the canteen... even worse is the campus is very dark at night. so, each time he move, he check front, back, left and right to see which of those four adjacent squares are free, and randomly walk to one of the Free squares until landing on a canteen.
 
Inputeach case begin with two integers n and M (n <= 15, m <= 15), which indicate the size of the campus. then N line follow, each contain M characters to describe the map. there are 4 different type of area in the MAP:

'@' Is the start location. There is exactly one in each case.
'#' Is an impassible square.
'$' Is a canteen. There may be more than one in the campus.
'.' Is a free square.
Outputoutput the expected number of moves required to reach a canteen, which accurate to 6 fractional digits. If it is impossible, output-1.
Sample Input
1 2@$2 2@..$1 3@#$
 
Sample output
1.0000004.000000-1


Solution:

A map of N * m has a starting point with multiple exits. It goes up, down, left, and right. Some grids cannot go. The expected number of steps from the starting point to an exit is calculated.

Map N * m is regarded as a grid, numbered 0, 1, 2, 3... M-1 then the grid number of coordinate I and j is I * m + J

Ek indicates the expected number of steps from a grid numbered K to an exit.

So EK = 0, K here is the Exit Number

What we want is Ek. k is the start number.

From one status to another, assuming that the current K can go in three directions,

Then EK = (EK (A) + EK (B) + EK (c)/3 + 1

General EK = (enext1 + enext2 + enext3 +... ecnt)/CNT + 1

Compiled by enext1 + enext2 + enext3 +... ecnt-ek * CNT =-CNT

For each EK (k = 0, 1, 2, 3... m-1, all establish such an equation, then you can list N * m equations, and then use Gaussian elimination element to obtain e (starting point)

There are N * m equations and N * m variables.

A [I] [J] indicates the I-th formula sub (starting from 0), the coefficient of the J-th unknown (starting from 0), where EK is unknown

A [0] [0] * E0 + A [0] [1] * E1 + ......... A [0] [n m-1 * E (n * m-1 = A [0] [N * m]

A [1] [0] * E0 + A [1] [1] * E1 + .................... ......................... = A [1] [N * m]

.....

.....

A [n M-1] [0] * E0 + A [n M-1] [1] * E1 + ................ ........... = A [M-1] [N * m]

Therefore, the key requirement is a [I] [J]. If all array a is obtained, then it can be directly imported into the Gaussian deyuan template.

Preprocessing: Perform BFs at each exit and mark the reachable position with flag [I] [J] = 1.

Traverse each lattice, create an equation for each lattice, and obtain the coefficients of each unknown number for each equation.

Use Gaussian elimination element to solve the problem.

If the start point can be accessed (flag [I] [J] = 1 and Gaussian elimination element has a solution), the output unique solution is E (SX * m + Sy, SX, Sy is the coordinate of the starting point

Otherwise, output-1.

Code:

# Include <iostream> # include <queue> # include <cmath> # include <stdio. h> # include <iomanip> # include <algorithm> # include <string. h> using namespace STD; const int maxn = 250; const double EPS = 1e-12; char MP [20] [20]; // map bool flag [20] [20]; // determine whether double A [maxn] [maxn] has been accessed; // The coefficients int n, m; int Sx, Sy for each unknown in each equation; // start point int DX [4] = {-,}; int dy [4] = {, 1,-1}; int equ, VAR; // equ equation, VAR Variable double X [maxn]; // solution set bool free_x [maxn]; ST Ruct node {int X, Y ;}; queue <node> q; node AA, BB; bool OK (INT X, int y, int D) // determine whether the current coordinate is feasible. Flag [x] [Y] = 0 is required for wide search. When establishing an equation, flag [x] [Y] = 1 is required, subsequent values can reach {If (D = 0) {If (x> = 0 & x <n & Y> = 0 & Y <M & amp; MP [x] [Y]! = '#'&&! Flag [x] [Y]) // do not forget flag [x] [Y] Return true ;} else {If (x> = 0 & x <n & Y> = 0 & Y <M & MP [x] [Y]! = '#' & Flag [x] [Y]) return true;} return false;} int SGN (Double X) {return (x> EPS) -(x <-EPS);} void BFS () {While (! Q. empty () {BB = Q. front (); q. pop (); For (INT I = 0; I <4; I ++) {AA. X = BB. X + dx [I]; AA. y = BB. Y + dy [I]; If (OK (AA. x, AA. y, 0) {flag [AA. x] [AA. y] = 1; q. push (AA) ;}}}// Gaussian elimination method (Gauss-Jordan elimination ). (0 indicates no solution, 1 indicates a unique solution, greater than 1 indicates an infinite solution, and returns the number of free variables) int Gauss () {equ = N * m, Var = N * m; int I, j, k; int max_r; // the row with the largest absolute value in the current column. int Col; // The column currently being processed. double temp; int free_x_num; int free_index; // convert to a step array. col = 0; // The column currently being processed. memset (free_x, True, sizeof (free_x); For (k = 0; k <equ & Col <var; k ++, Col ++) {max_r = K; for (I = k + 1; I <equ; I ++) {If (SGN (FABS (A [I] [col]) -FABS (A [max_r] [col])> 0) max_r = I;} If (max_r! = K) {// exchange with row K. for (j = K; j <var + 1; j ++) Swap (A [k] [J], a [max_r] [J]);} if (SGN (A [k] [col]) = 0) {// indicates that all the values in row K of the col column are 0, the next column of the current row is processed. k --; continue;} for (I = k + 1; I <equ; I ++) {// enumerate the rows to be deleted. if (SGN (A [I] [col])! = 0) {temp = A [I] [col]/A [k] [col]; for (j = Col; j <var + 1; j ++) {A [I] [J] = A [I] [J]-A [k] [J] * temp ;}}} for (I = K; I <equ; I ++) {If (SGN (A [I] [col])! = 0) return 0;} If (k <var) {for (I = K-1; I> = 0; I --) {free_x_num = 0; For (j = 0; j <var; j ++) {If (SGN (A [I] [J])! = 0 & free_x [J]) free_x_num ++, free_index = J;} If (free_x_num> 1) continue; temp = A [I] [Var]; for (j = 0; j <var; j ++) {If (SGN (A [I] [J])! = 0 & J! = Free_index) temp-= A [I] [J] * X [J];} X [free_index] = temp/A [I] [free_index]; free_x [free_index] = 0;} return var-K;} for (I = var-1; I> = 0; I --) {temp = A [I] [Var]; for (j = I + 1; j <var; j ++) {If (SGN (A [I] [J])! = 0) temp-= A [I] [J] * X [J];} X [I] = temp/A [I] [I];} return 1 ;} int main () {While (CIN> N> m) {While (! Q. empty () Q. pop (); memset (flag, 0, sizeof (FLAG); For (INT I = 0; I <n; I ++) for (Int J = 0; j <m; j ++) {CIN> MP [I] [J]; If (MP [I] [J] = '@') {SX = I; sy = J;} If (MP [I] [J] = '$') {AA. X = I; AA. y = J; q. push (AA); // Add each exit to the queue to prepare for the following BFs. Note that extensive search should start from the exit, the flag [I] [J] = 1 ;}} BFS (); memset (A, 0, sizeof (A) cannot be searched from the starting point )); for (INT I = 0; I <n; I ++) // traverse each lattice, create an equation for each lattice, and obtain the coefficient of each unknown for (Int J = 0; j <m; j ++) {int CNT = 0; // calculates the number of subsequent statuses. If (MP [I] [J] = '#') continue; if (MP [I] [J] = '$') // exit {A [I * m + J] [N * m] = 0; A [I * m + J] [I * m + J] = 1; continue;} For (INT S = 0; S <4; s ++) {int x = I + dx [s]; int y = J + dy [s]; If (OK (X, Y, 1) {CNT ++; A [I * m + J] [x * m + Y] = 1 ;}} A [I * m + J] [I * m + J] =-1 * CNT; A [I * m + J] [N * m] =-1 * CNT;} If (flag [SX] [sy] & Gauss ()) cout <setiosflags (IOs: fixed) <setprecision (6) <X [SX * m + Sy] <Endl; else cout <-1 <Endl;} return 0 ;}






[ACM] HDU 2262 where is the canteen (Gaussian deyuan expectation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.