Huadian North Wind Blows
Key laboratory of cognitive computing and application, Tianjin University
Date: 2015/8/27
Because the code is not debugged, the paper draft renders completely correctly
POJ need to use the state compression dynamic planning, link http://poj.org/problem?id=3254
Online to see a lot of people write code, referring to a top-down with a memo dynamic programming solution, wrote a bottom-up dynamic programming solution
Bottom up:
#include <iostream>#include <math.h>#include <ostream>#include <fstream>using namespace STD;#define MAX (a) > (b) ( A):(B))#define MODULO 100000000intdyna[ A][4200];int Map[ A][ A];intM, N;/ * M stands for row and N stands for line. * /intValiintb) {intMax = (int)(Log(b)/Log(2)); for(inti =0; i < Max; i++)if(B &1<< i) && (b &1<< (i +1)))return 0;return 1;}intMain () {Ifstream in ("C:\\users\\zhengyi\\desktop\\google\\input.txt"); Streambuf *cinbuf =Cin. Rdbuf ();//save old buf Cin. Rdbuf (In.rdbuf ());//redirect std::cin to in.txt! intI, j, ans =0;Cin>> m >> N; for(i =0; I < m; i++) { for(j =0; J <1<< N; J + +) Dyna[i][j] =0; } for(i =0; I < m; i++) { for(j =0; J < N; J + +)Cin>>MapI [j]; }intTar =0; for(i =0; I < n; i++) tar + = (Map[M-1][i]) << i; for(i =0; I <1<< N; i++) {if(~ (Tar | ~i) = =0) {dyna[m-1][i] =1; } } for(i = m-1; i >0; i--) { for(j =0; J <1<< N; J + +) {if(Dyna[i][j] >0) { for(intK =0; K <1<< N; k++) {if(Vali (k))if((j&k) = =0) Dyna[i-1][k] + = dyna[i][j]; }}} tar =0; for(j =0; J < n;j++) tar + = (MapI1][J]) << J; for(j =0; J <1<< N; J + +) {if(~ (Tar | ~j)! =0) Dyna[i-1][J] =0; } }intresult =0; for(j =0; J <1<< N; J + +) {result + = dyna[0][J]; }cout<< result << Endl; System"Pause");return 0;}
Top-Down with memo:
#include <iostream>#include <math.h>#include <ostream>#include <fstream>using namespace STD;#define MAX (a) > (b) ( A):(B))#define MODULO 100000000/ * POJ 3254:corn fields * *intdyna[ A][4200];int Map[ A][ A];intM, N;/ * M stands for row and N stands for line. * /intValiintAintb) {intI, max = (int)(Log(b)/Log(2));intc = ~ (A | (~b));if(c)return 0;Else{ for(i =0; i < Max; i++)if(B &1<< i) && (b &1<< (i +1)))return 0;return 1; }}intFindintRowintBitway) {intAns =0, I, tar =0;if(Dyna[row][bitway] >=0)returnDyna[row][bitway];Else{ for(i =0; I < n; i++) Tar + =Map[Row] [I] * (1<< i); Tar &= (~bitway);if(row = = M-1) { for(i =0; I <1<< N; i++) {if(Vali (tar, i)) ans++; Ans%= MODULO; }returnDyna[row][bitway] = ans% MODULO; }Else{ for(i =0; I <1<< N; i++) {if(Vali (tar, i)) ans + = find (row +1+ N); Ans%= MODULO; }returnDyna[row][bitway] = ans% MODULO; } }}intMain () {Ifstream in ("C:\\users\\zhengyi\\desktop\\google\\input.txt"); Streambuf *cinbuf =Cin. Rdbuf ();//save old buf Cin. Rdbuf (In.rdbuf ());//redirect std::cin to in.txt! intI, j, ans =0;Cin>> m >> N; for(i =0; I < m; i++) { for(j =0; J <1<< N; J + +) dyna[i][j] =-1; } for(i =0; I < m; i++) { for(j =0; J < N; J + +)Cin>>MapI [j]; } ans = max (ans, find (0,0));printf("%d\n", ans); System"Pause");return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Fantastic algorithm-state compression dynamic programming