HDU 1693 Eat the Trees (插頭DP)
阿新 • • 發佈:2017-09-28
tac 回路 ons sign turn algorithm div max ostream
題意:給定一個01矩陣,問你能畫出幾條回路,使得包含所有的1。
析:一個插頭DP,dp[i][j][s] 表示轉移到 (i, j) 這個格子,狀態為 s 時的方案數,然後逐格遞推。對於每個格子要麽有0個插頭要麽有2個。
代碼如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #include <list> #include <assert.h> #include <bitset> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define fi first #define se second #define pb push_back #define sqr(x) ((x)*(x)) #define ms(a,b) memset(a, b, sizeof a) #define sz size() #define pu push_up #define pd push_down #define cl clear() //#define all 1,n,1 #define FOR(i,x,n) for(int i = (x); i < (n); ++i) #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 1e20; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 11 + 10; const ULL mod = 10007; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c) { return r >= 0 && r < n && c >= 0 && c < m; } int a[maxn][maxn]; LL dp[2][1<<12]; int main(){ int T; cin >> T; for(int kase = 1; kase <= T; ++kase){ scanf("%d %d", &n, &m); for(int i = 1; i <= n; ++i) for(int j = 1; j <= m; ++j) scanf("%d", &a[i][j]); int cur = 0; ms(dp[cur], 0); dp[cur][0] = 1; int all = 1<<m+1; for(int i = 1; i <= n; ++i){ cur ^= 1; ms(dp[cur], 0); for(int j = 0; j < (1<<m); ++j) dp[cur][j<<1] = dp[cur^1][j]; for(int j = 1; j <= m; ++j){ cur ^= 1; for(int k = 0; k < all; ++k){ int up = 1<<j; int le = 1<<j-1; if(a[i][j]){ dp[cur][k] = dp[cur^1][k^up^le]; if(k&up && k&le) continue; if(!(k&up) && !(k&le)) continue; dp[cur][k] += dp[cur^1][k]; } else{ if(!(k&up) && !(k&le)) dp[cur][k] = dp[cur^1][k]; else dp[cur][k] = 0; } } } } printf("Case %d: There are %I64d ways to eat the trees.\n", kase, dp[cur][0]); } return 0; }
HDU 1693 Eat the Trees (插頭DP)