Pipes
Time Limit: 5000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 405 accepted submission (s): 200
Problem descriptionthe construction of office buildings has become a very standardized task. pre-fabricated modules are combined according to the customer's needs, shipped from a faraway factory, and assembled on the construction site. however, there
Are still some tasks that require careful planning, one example being the routing of pipes for the heating system.
Amodern office building ismade up of squaremodules, one on each floor being a service module from which (among other things) hot water is pumped out to the other modules through the heating pipes. each module (including the service module) will have heating
Pipes connecting it to exactly two of its two to four neighboring modules. thus, the pipes have to run in a circuit, from the service module, visiting each module exactly once, before finally returning to the service module. due to different properties
The modules, having pipes connecting a pair of adjacent modules comes at different costs. for example, some modules are separated by thick Wals, increasing the cost of laying pipes. your task is to, given a description of a floor of an office building, decide
The cheapest way to route the heating pipes.
Inputthe first line of input contains a single integer, stating the number of floors to handle. then follow N floor descriptions, each beginning on a new line with two integers, 2 <= r <= 10 and 2 <= C <= 10, defining the size of the floor
-R-by-C modules. beginning on the next line follows a floor description in ASCII format, in total 2R + 1 rows, each with 2C + 2 characters, including the final newline. all floors are perfectly rectangular, and will always have an even number of modules.
All interior Wils are represented by numeric characters, '0' to '9', indicating the cost of routing pipes through the wall (see sample input ).
Outputfor each test case, output a single line with the cost of the cheapest route.
Sample Input
34 3######## 2 3 ##1#9#1## 2 3 ##1#7#1## 5 3 ##1#9#1## 2 3 ########4 4########## 2 3 3 ##1#9#1#4## 2 3 6 ##1#7#1#5## 5 3 1 ##1#9#1#7## 2 3 0 ##########2 2###### 1 ##2#3## 4 ######
Sample output
284510
Sourcenwerc2004
Previously, recommendwangye used to calculate the number of paths for the plug DP. This is the first plug to find the optimal solution. Similar ideas. Recurrence by grid.
# Include <stdio. h> # include <string. h ># include <iostream> # include <algorithm> # define min (A, B) (a) <(B )? (A) :( B) using namespace STD; const int hash = 30007; // the size of the hash table const int state = 1000010; // number of States const int maxd = 15; int n, m; int code [maxd], maze [maxd] [maxd], mcod [maxd]; // encoding. And save the map. Minimum representation char graph [30] [30]; struct hashmap // Hash Table Structure {int head [hash], next [State], SZ; // use a linked list to connect the hash table with the same module. It is convenient to search for and Judge long f [State], State [State]; // number of methods in the corresponding state of F records. Next points to the next state with the same modulus. State record status. Total number of SZ record statuses void Init () // hash table initialization function {SZ = 0; memset (Head,-1, sizeof (head);} void push (long St, long long ans) // The Press status and number of methods {int I, H = sT % hash; for (I = head [H]; I! =-1; I = next [I]) if (ST = State [I]) // If the status already exists. If the number of methods is increased, {f [I] = min (ANS, F [I]); return;} f [SZ] = ans; // Save the new viable State state State [SZ] = sT; next [SZ] = head [H]; head [H] = SZ ++ ;}} HM [2]; void decode (int * code, int M, long St) // code from high to low m to 0. Corresponding plug {int I; for (I = m; I> = 0; I --) {code [I] = sT & 7; st >>>= 3 ;}} long encode (int * code, int m) // The minimum representation is decoded to {int I, CNT = 1; long long St = 0; memset (mcod,-1, sizeof mcod); mcod [0] = 0; for (I = 0; I <= m; I ++) {If (mcod [Code [I] =-1) mcod [Code [I] = CNT ++; Code [I] = mcod [Code [I]; st <= 3; ST | = Code [I];} return st;} void Init () // read data. Initialize {int I, j; memset (maze, 0, sizeof maze); for (I = 1; I <= N; I ++) for (j = 1; j <= m; j ++) maze [I] [J] = 1; for (I = 1; I <= 2 * n + 1; I ++) gets (graph [I] + 1);} void dpblank (int I, Int J, int cur) // handle the case where the cells can be processed {int K, T, left, up, temp; For (k = 0; k <HM [cur]. SZ; k ++) // traverses the status of the J-cell output contour line for state transfer {decode (Code, M, HM [cur]. state [k]); // encode the State left = Code [J-1]; // obtain the left plug status up = Code [J]; // obtain the status of the upper plug if (left & UP) // 11-> 00 {If (Left = up) // forming the loop {if (I = N & J = m ){ Code [J-1] = Code [J] = 0; // only 0 one grid can have only two plugs HM [cur ^ 1]. Push (encode (Code, j = m? M-1: m), HM [cur]. f [k]); // press new status} else {code [J-1] = Code [J] = 0; For (t = 0; t <= m; t ++) if (code [T] = up) {code [T] = left; break;} hm [cur ^ 1]. push (encode (Code, j = m? M-1: m), HM [cur]. f [k]); // press in new State} else if ((! Left & UP) | (left &&! Up) // 01 or 10 {If (up) temp = up; else temp = left; If (maze [I] [J + 1]) // when J = m, maze [I] [J] is 0 and shift {code [J-1] = 0; Code [J] = temp; HM [cur ^ 1]. push (encode (Code, m), HM [cur]. f [k] + graph [2 * I] [2 * j + 1]-'0');} If (maze [I + 1] [J]) {code [J-1] = temp; Code [J] = 0; HM [cur ^ 1]. push (encode (Code, j = m? M-1: m), HM [cur]. f [k] + graph [2 * I + 1] [2 * j]-'0 ');}} else {If (maze [I] [J + 1] & maze [I + 1] [J]) // shift {code [J] = Code [J-1] = 13; HM [cur ^ 1] is not required if J = m is not legal. push (encode (Code, m), HM [cur]. f [k] + graph [2 * I] [2 * j + 1] + graph [2 * I + 1] [2 * j]-'0'-'0' );}}}} void dpblock (int I, Int J, int cur) // handle cases where cells cannot be reached {int K; For (k = 0; k <HM [cur]. SZ; k ++) // The storage status is valid. You do not need to judge {decode (Code, M, HM [cur]. state [k]); // The first encoding code [J-1] = Code [J] = 0; // certainly cannot use the plug HM [cur ^ 1]. P Ush (encode (Code, j = m? M-1: m), HM [cur]. f [k]) ;}} void solve () {int I, j, cur = 0; long ans = 0000000000ll; HM [cur]. init (); // cur is used to scroll the array. Save space. Cur stores the current plug and left plug. Cur ^ 1 is used to record the new State transferred out, that is, the next cell HM [cur]. push (0, 0); for (I = 1; I <= N; I ++) for (j = 1; j <= m; j ++) {HM [cur ^ 1]. init (); // initialize if (maze [I] [J]) dpblank (I, j, cur); else dpblock (I, j, cur ); cur ^ = 1 ;}for (I = 0; I <HM [cur]. SZ; I ++) ans = min (HM [cur]. f [I], ANS); printf ("% i64d \ n", ANS);} int main () {int CAS; scanf ("% d", & CAS ); while (CAS --) {scanf ("% d", & N, & M); getchar (); Init (); solve () ;}return 0 ;}