HDU 4285 Circuits

Source: Internet
Author: User
Circuits

Time Limit: 30000/15000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 592 accepted submission (s): 175

Problem description given a map of N * m (2 <= n, m <= 12 ),'. 'Means empty, '*' means Wils. you need to build K circuits and no circuits cocould be nested in another. A circuit is a route connecting adjacent cells in a cell sequence, and also connect
The first cell and the last cell. Each cell shocould be exactly in one circuit. How many ways do we have?

 

Input the first line of input has an integer T, number of cases.
For each case:
The first line has three integers n m K, as described above.
Then the following n lines each has m characters, '.' or '*'.

Output for each case output one lines.
Each line is the answer % 1000000007 to the case.

Sample Input

24 4 1**..............4 4 1................
 

Sample output

26
 

Source2012 ACM/ICPC Asia Regional Tianjin online
 

Similar to HDU 1693 eat the trees, recommendliuyiding only limits the number of loops. So add an array of record loops in hashmap. Determine the number of plugs before the current grid if there is nesting. If there are odd numbers. It indicates that there must be nesting. Draw a picture to understand.

# Include <stdio. h> # include <string. h> # include <iostream> # include <algorithm> using namespace STD; const int hash = 30007; // the size of the hash table const int state = 1000010; // number of States const int maxd = 15; const int mod = 1000000007; int n, m, Mac; // number of target Mac circuits int code [maxd], maze [maxd] [maxd], mcod [maxd]; // encoding. And save the map. Minimum representation char s [20]; 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 int circle [State]; void Init () // hash table initialization function {SZ = 0; memset (Head,-1, sizeof (head ));} void push (long St, long ans, int CIR) // press status and number of methods {int I, H = sT % hash; for (I = head [H]; i! =-1; I = next [I]) if (ST = State [I] & CIR = circle [I]) // If the status already exists. The number of methods is increased. {// you can calculate the equivalent f [I] + = ans; F [I] % = MOD; return;} only when the number of circuits is the same ;} f [SZ] = ans; // Save the new viable State [SZ] = sT; circle [SZ] = CIR; next [SZ] = head [H]; head [H] = SZ ++;} hm [2]; void decode (int * code, int M, long St) // encoding starts from the high position to the low position m to the 0 encoding. 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 ++) {scanf ("% s ", S + 1); For (j = 1; j <= m; j ++) if (s [J] = '. ') maze [I] [J] = 1 ;}} void dpblank (int I, Int J, int cur) // handle cases where the cells can be processed {int K, T, left, up, temp, cot; For (k = 0; k <HM [cur]. SZ; k ++) // traverses the status of the J-cell output contour line for state transfer {If (HM [cur]. circle [k]> Mac) continue; 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 loop {COT = 0; For (t = 0; t <J-1; t ++) // determine the number of plug-ins before the current grid if (code [T]) cot ++; If (COT & 1) // if the number of plug-ins is odd, it indicates nested. I can understand the continue painting; Code [J-1] = Code [J] = 0; // only 0 one grid can only be two plug HM [cur ^ 1]. push (encode (Code, j = m? M-1: m), HM [cur]. f [K], HM [cur]. circle [k] + 1); // press new State} 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], HM [cur]. Circle [k]); // press into 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], HM [cur]. circle [k]);} 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], HM [cur]. circle [k]) ;}} 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], HM [cur]. circle [k]) ;}}} void dpblock (int I, Int J, int cur) // handle cases where the cells cannot reach {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]. push (encode (Code, j = m? M-1: m), HM [cur]. f [K], HM [cur]. circle [k]) ;}} void solve () {int I, j, cur = 0; long ans = 0; 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 ++) if (HM [cur]. circle [I] = Mac) ans + = HM [cur]. f [I]; printf ("% i64d \ n", ANS);} int main () {int CAS; scanf ("% d", & CAS ); while (CAS --) {scanf ("% d", & N, & M, & MAC); Init (); solve () ;}return 0 ;}
 

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.