Poj 1038 pressure DP

Source: Internet
Author: User

A company produces two or three chips, but some of the above raw materials cannot be used as chip materials, giving the size of raw materials and the points above which cannot be used as raw materials, ask you how to break it down to maximize the number of chips generated.


Perform triplicate pressure on m

Last array stores the state of the I-1 row and I-2 row, cur array stores the state of the I row and I-1 row

Cur [k] = 2; // both the K position and the upstream K position of the row are unavailable.

Cur [k] = 1; // The K position of the row is available, and the K position of the uplink is unavailable.

Cur [k] = 0; // both the row and the uplink position K can be used


You must use a rolling array; otherwise, the memory will pop up.


# Include "stdio. H "# include" string. H "int B [20], ANS, n, m; int DP [2] [69050], last [21], cur [21], map [160] [160]; int updata (int I, Int J) {int res, K; Res = 0; memset (cur, 0, sizeof (cur )); memset (last, 0, sizeof (last); For (k = 1; k <= m; k ++) {last [k] = J % 3; j/= 3 ;}for (k = 1; k <= m; k ++) if (Map [I] [k] = 1) {cur [k] = 2; // The K position of the row and the K position of the uplink are not available res + = B [k] * 2 ;} else if (last [k] = 2) // The K position of the last row is occupied, and the K position of the last row is not occupied {cur [k] = 1; // The row's K position is available, and the uplink K position is unavailable Res + = B [k];} else cur [k] = 0; // return Res can be used for both the row and the uplink position K; // row position compression value} void DFS (int I, int now, int K, int status) // row I. The value is now. The current value is K, status {int temp; If (now> ans) ans = now; If (now> DP [I] [Status]) DP [I] [Status] = now; if (k + 1 <= M & cur [k] = 0 & cur [k + 1] = 0 & last [k] = 0 & last [k + 1] = 0) // For the row and uplink, both the K and k + 1 positions in the upper row can be {cur [k] = 2; cur [k + 1] = 2; temp = status + B [k] * 2 + B [k + 1] * 2; DFS (I, now + 1, K + 2, temp ); cur [k] = 0; cur [k + 1] = 0 ;} // place 3 rows * 2 column matrix if (K + 2 <= M & cur [k] = 0 & cur [k + 1] = 0 & cur [k + 2] = 0) // In this row, the uplink K and k + 1, K + 2 locations can be used {cur [k] = cur [k + 1] = cur [K + 2] = 2; temp = status + B [k] * 2 + B [k + 1] * 2 + B [K + 2] * 2; DFS (I, now + 1, K + 3, temp); cur [k] = cur [k + 1] = cur [K + 2] = 0 ;} // place 2 rows * 3 columns of the matrix if (k + 1 <= m) DFS (I, now, k + 1, status);} int main () {int I, case, K, aim, status, J, X, Y; B [0] = 0; B [1] = 1; for (I = 2; I <= 11; I ++) B [I] = B [I-1] * 3; scanf ("% d", & case); While (case --) {s CANF ("% d", & N, & M, & K); memset (MAP, 0, sizeof (MAP); While (k --) {scanf ("% d", & X, & Y); map [x] [Y] = 1;} aim = B [M + 1]-1; status = 0; for (I = 1; I <= m; I ++) if (Map [1] [I] = 1) status + = 2 * B [I]; else status + = B [I]; memset (DP,-1, sizeof (DP )); DP [1] [Status] = 0; ans = 0; for (I = 2; I <= N; I ++) {memset (DP [I % 2], -1, sizeof (DP [I % 2]); For (j = 0; j <= aim; j ++) if (DP [1-I % 2] [J]! =-1) {status = updata (I, j); DFS (I % 2, DP [1-I % 2] [J], 1, status );}} printf ("% d \ n", ANS);} 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.