Ural1519 Formula 1 (plug DP)

Source: Internet
Author: User

Original title URL: http://acm.timus.ru/problem.aspx?space=1&num=1519

For plug DP and state compression please refer to:
Http://wenku.baidu.com/link?url=AFuYe_ Efr5yxmnk0ry-tale6llgkhsovxbm1rqulxelprvjqvlo724nuxlxtadx4alp7fhiz8aexyity06_r4cv5xus6c9lm5vpz5kdr6hg

Detailed code (c + +):

1#include <cstdio>2#include <algorithm>3#include <cstring>4 //using namespace std;5 6typedefLong LongLL;7 Const intMAXRC = the, maxstate=300001;8 intm,n,er=-1, EC, idx=0;9 intcity[maxrc][maxrc]={0}, BITS_AT[MAXRC];//1 is valid.Ten  One classhashmap{ A  Public: -     intHead[maxstate], nxt[maxstate], size; - LL State[maxstate], num[maxstate]; the     voidinit () { -Size =0; -Memset (Head,-1,sizeofhead); -         //memset (NXT,-1, sizeof NXT);//head interpolation without the initialization of the NXT +     } -     voidpush (ll S, ll N) { +         intpos = s%maxstate, BG =Head[pos]; A          while(BG! =-1){ at             if(state[bg]==s) { -NUM[BG] + =N; -                 return ; -             } -BG =NXT[BG]; -         } inState[size] =s; -Num[size] =N; to         //Head Insertion Method +Nxt[size] =Head[pos]; -Head[pos] = size++; the     } *}hm[2]; $ Panax NotoginsengInlineintGet_state_at (LL S,intj) { -     return(S&bits_at[j]) >> (j<<1); the } +inline ll Set_state_at (ll S,intJintb) { AS &= ~(Bits_at[j]); the     returns| (b<< (j<<1)); + } -inline ll Set_state_at (ll S,intJintbjintbn) { $S &= ~ (bits_at[j]+bits_at[j+1]); $S |= (bj+ (bn<<2)) << (j<<1); -     returns; - } the intFind_match (LL S,intj) {//c!=0 -     intc = Get_state_at (S, j), d = c = =1?1:-1, F =0;Wuyi      for(;; j+=d) { the         if(Get_state_at (S, j) ==c) f++; -         Else if(Get_state_at (s,j)) f--; Wu         if(f = =0)returnJ; -     } About     return-1; $ } - voiddp () { -IDX =0; Hm[idx].init (); Hm[idx].push (0,1); -      for(intI=0; i<n; ++i) { A          for(intj=0; jj) +HM[IDX].STATE[J] <<=2;//the right bit must be 0 . the          for(intj=0; j<m; ++j) { -             intCur = idx^1;//scrolling array, required set of States $ hm[cur].init (); the              for(intk=0; kk) { theLL PS = hm[idx].state[k], pn =Hm[idx].num[k]; the                 intSL = Get_state_at (PS, j), Su = Get_state_at (PS, j+1); the                 if(SL = =0&& Su = =0){ -                     if(!city[i][j]) {//extend the state to a non-. Place inHm[cur].push (Set_state_at (PS, J,0,0), PN); the}//the plug should point to a blank grid the                     Else if(city[i][j+1] && city[i+1][j]) { AboutHm[cur].push (Set_state_at (PS, J,1,2), PN); the                     } the                 } the                 //else if (!city[i][j]) continue;//does not execute here +                 Else if(SL = =0|| Su = =0){//extends only one plug -                     if(city[i][j+1]) Hm[cur].push (Set_state_at (PS, J,0, sl+su), PN); the                     if(city[i+1][j])BayiHm[cur].push (Set_state_at (PS, J, Sl+su,0), PN);  the                 } the                 Else if(sl = = su) {//merge connected blocks with opening or closing parentheses -                     intPOSL = Find_match (PS, j), Posu = Find_match (PS, j+1); -                     intMN = Std::min (POSL, posu), mx =Std::max (POSL, Posu); theLL cs = Set_state_at (PS, MN,1); theCS = Set_state_at (cs, MX,2); theHm[cur].push (Set_state_at (CS, J,0,0), PN); the                 } -                 Else if(SL = =2&& Su = =1){//merging into simple paths theHm[cur].push (Set_state_at (PS, J,0,0), PN); the                 } the                 Else if(i = = er && j = = EC) {//merged into loops, only in the last valid lattice94Hm[cur].push (Set_state_at (PS, J,0,0), PN); the                 } the             } theIDX = cur;//Exchange Status98         } About     } - }101 intMain () {102Freopen ("In.txt","R", stdin);103scanf"%d%d", &n, &m);104     CharCY[MAXRC][MAXRC]; the      for(intI=0; i<n; ++i) {106scanf"%s", Cy[i]);107          for(intj=0; j<m; ++j) {108             if(Cy[i][j] = ='.'){109CITY[I][J] =1; theER = i; EC =J;111             } the         }113     } the      for(intI=0; i<=m; ++i) { theBits_at[i] =3<< (i<<1);//0 is invalid, 1 are left bracket, 2 are right bracket. the     }117 DP ();118printf"%lld\n", hm[idx].size>0? hm[idx].num[0]:0LL);119     return 0; -}

Ural1519 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.