POJ 3254 Corn fields (pressure DP, contour line DP)

Source: Internet
Author: User

Test instructions

There is a n*m matrix (0<n,m<=12), there are some of the lattice can be planted grass, some are not species, ask how many different species of grass program (completely not species can also be counted 1 kinds, the answer after the output)?

Ideas:

Obvious pressure DP, just how to compress the state? Like Contour DP, the state is designed in grid units, a state only needs to be represented above and to the left of the lattice, so it is only necessary to save min (n,m) 01 states (can try to rotate the matrix), up to 12 bits. Using a hash table to do it will be faster, do not have to consider the invalid state, such as the appearance of adjacent two 1.

1 //#include <bits/stdc++.h>2#include <iostream>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <map>7#include <algorithm>8#include <vector>9#include <iostream>Ten #definePII pair<int,int> One #defineINF 0x7f3f3f3f A #defineLL Long Long - #defineULL unsigned long Long - using namespacestd; the Const DoublePI = ACOs (-1.0); - Const intn= -; - Const intm=100000000; - intG[n][n]; + structHash_map - { +     Static Const intMod=1237; A     Static Const intn=10000; at     intHEAD[MOD];//Bucket Hands -     intNext[n];//information for the record chain -     intStatus[n];//Status -     intValue[n];//the DP value corresponding to the state.  -     intsize; -     voidClear ()//clears the state from the hash table in     { -Memset (Head,-1,sizeof(head)); toSize =0; +     } -  the     voidInsertintStintVal//the value of the insert State St is Val *     { $         intH = st%MoD;Panax Notoginseng          for(intI=HEAD[H]; i!=-1; I=Next[i]) -         { the             if(Status[i] = = st)//This state already exists and accumulates in it.  +             { AValue[i] + =Val; thevalue[i]%=M; +                 return ; -             } $         } $Status[size]= St;//If the status St is not found, insert St.  -Value[size] =Val; -Next[size] = head[h];//The newly inserted element in the team header theHEAD[H] = size++; -     }Wuyi}hashmap[2]; the  - intCalintNintm) Wu { -     intCur=0, Mod= (1<<m-1)-1; About hashmap[cur].clear (); $hashmap[0].insert (0,1); -      for(intI=1; i<=n; i++) -     { -          for(intj=1; j<=m; J + +) A         { +cur^=1; the hashmap[cur].clear (); -              for(intk=0; k1].size; k++) $             { the                 ints=hashmap[cur^1].status[k]; the                 intv=hashmap[cur^1].value[k]; the                 intt= (S&mod) <<1;//remove the highest bit theHashmap[cur].insert (T,V);//not put -                 if(G[i][j]) in                 { the                     if(s& (1<<m-1) )Continue; the                     if(j!=1&& (s&1) )Continue;//can't put AboutHashmap[cur].insert (t^1, v); the                 } the             } the         } +     } -     intans=0; the      for(intk=0; k)Bayi     { theans+=Hashmap[cur].value[k]; theans%=M; -     } -     returnans; the } the  the  the intMain () - { the     //freopen ("Input.txt", "R", stdin); the     intN, M; the      while(~SCANF ("%d%d",&n,&m))94     { the          for(intI=1; i<=n; i++) the              for(intj=1; j<=m; J + +) thescanf"%d",&g[i][j]);98printf"%d\n", Cal (N,m)); About     } -     return 0;101}
AC Code

POJ 3254 Corn fields (pressure DP, contour line 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.