URAL-1519 Formula 1 (plug DP)

Source: Internet
Author: User

Here to write the link content

Just started to learn the plug DP good difficulty, read someone else's code a little to understand, so the reference to someone else's code, wrote a comment, hope to help
If you can't read it, you can ask.

//The following are all in the 13th page of the paper#include <cstdio>#include <cstring>#include <algorithm>usingnamespace Std;#define N#define S1 14000#define S2 1600000structqueue{intOptLong Longsum;} q[2][S1];CharG[n][n];intPow3[n];//cnt refers to the number of Statesintcnt[2];//state[][j] refers to J. What is the ordinal number of this state ?intstate[2][S2];intN, M, final, now;//Get the status of the nth columnint Get(intOptintCol) {returnOpt/pow3[col]%3;}//Set col column to valueint Set(intOptintColint value) {returnOpt + (value-Get(Opt, col)) * Pow3[col];}//This is the solution to the memory-width-first search, inserting the queuevoidEnqueueintOptintColLong LongSUM) {//If it is the last column, and the last column has a right plug, this does not fit, and the first m stored is the right plug of Col column    if(col = = M-1&&Get(OPT, m))return;//Find out the serial number of the OPT status    int&id = state[now][opt];//If you have extended    if(ID) q[now][id].sum + = sum;Else{//has not expandedid = ++cnt[now];        q[now][id].opt = opt;    Q[now][id].sum = sum; }}//Change status, this is the case in the paper 2.1intChange1 (intOptinti) { for(intFlag =0; I < m; i++) {intTMP =Get(opt, i);if(TMP = =1) flag++;Else if(TMP = =2) flag--;if(!flag)return Set(Opt, I,1); }return-1;}//Change status, this is the case in the paper 2.2intChange2 (intOptinti) { for(intFlag =0; I >=0; i--) {intTMP =Get(opt, i);if(TMP = =2) flag++;Else if(TMP = =1) flag--;if(!flag)return Set(Opt, I,2); }return-1;}voidInit () {pow3[0] =1; for(inti =1; i < N; i++) Pow3[i] = pow3[i-1] *3; scanf"%d%d", &n, &m); now =0; for(inti =0; I < n; i++) {scanf ('%s ', G[i]); for(intj =0; J < M; J + +)if(G[i][j] = ='. ') Final = i * m + j; } enqueue (0,0,1);}voidSolve () {now ^=1; for(inti =0; I < n; i++) for(intj =0; J < M; J + +) { for(intK =1, L = cnt[now ^1]; K <= L; k++) {intopt = Q[now ^1][k].opt;Long Longsum = q[now ^1][k].sum;intleft =Get(OPT, m), up =Get(OPT, j);if(G[i][j] = =' * ') {if(left = =0&& up = =0) Enqueue (opt, j, sum);Continue; }//Case in paper 1                if(left = =0&& up = =0) {Enqueue (Set(Set(OPT, M,2), J,1), J, sum); }//The situation in the paper 2.1                Else if(left = =1&& up = =1) {intTMP = Change1 (opt, j); EnqueueSet(Set(TMP, M,0), J,0), J, sum); }//The situation in the paper 2.2                Else if(left = =2&& up = =2) {intTMP = Change2 (opt, j); EnqueueSet(Set(TMP, M,0), J,0), J, sum); }//The situation in the paper 2.3                Else if(left = =1&& up = =2) {if(I * m + j = Final) Enqueue (Set(Set(OPT, M,0), J,0), J, sum); }//The situation in the paper 2.4                Else if(left = =2&& up = =1) {Enqueue (Set(Set(OPT, M,0), J,0), J, sum); }//Case in paper 3                Else if(left = =0|| up = =0) {Enqueue (Set(Set(OPT, M,0), J, left + up), j, sum); EnqueueSet(Set(Opt, J,0), M, left + up), j, sum); }} now ^=1; for(intK =1, t = Cnt[now]; K <= T; k++) State[now][q[now][k].opt] =0; Cnt[now] =0; } printf ("%lld\n", Q[now ^1][state[now ^1][0]].sum);}intMain () {init (); Solve ();return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

URAL-1519 Formula 1 (plug DP)

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.