ZOJ 3213 Beautiful Meadow(插头DP)

题意:在一个棋盘模型上求最大权值的路径,路径的起点和终点可以任意选定。

思路:相比于普通的回路DP,增加一个状态表示独立插头,即起点和终点,这道题分的情况比较多。

默默吐槽...代码量5000b写4个小时调试4个小时,分类讨论简直恶心到吐.....一不小心就wa了.......

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<ctime>
#define eps 1e-6
#define LL long long
#define pii pair<int, int>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

const int hashSize = 30000;
int Hash[hashSize]; 
int n, m, Index;
int total[2], state[2][hashSize], Bit[100];
int dp[2][hashSize], ans;
int Map[15][15];
void init() {
	memset(Map, 0, sizeof Map);
	Index = 0;
	total[0] = 1;
	state[0][1] = 0;
	dp[Index][1] = 0;
	ans = 0;
}
void hashCal(int s, int num) {
	int hashPos =  s % hashSize;
	while(Hash[hashPos] != -1) {
		if(state[Index][Hash[hashPos]] == s) {
			dp[Index][Hash[hashPos]] = max(dp[Index][Hash[hashPos]], num);
			return;
		}
		hashPos++;
		if(hashPos==hashSize) hashPos = 0;
	}
	total[Index]++;
	dp[Index][total[Index]] = num;
	Hash[hashPos] = total[Index];
	state[Index][total[Index]] = s;
}
void DP() {
	for(int i = 1; i <= n; i++) {
		for(int k = 1; k <= total[Index]; k++) {
			int num = state[Index][k] & 3;
			state[Index][k] <<= 2;
			state[Index][k] += num - (num<<2);
		}
		for(int j = 1; j <= m; j++) {
			//cout << total[Index^1] << endl;
			Index ^= 1;
			total[Index] = 0;
			memset(Hash, -1, sizeof Hash);
			for(int k = 1; k <= total[Index^1]; k++) {
				int s = state[Index^1][k];
				int num = dp[Index^1][k];
				int plug = s & 3;
				int p = (s>>Bit[j-1]) % 4;
				int q = (s>>Bit[j]) % 4;
				//cout << i << " " << j << " " << p << " " << q << " " << num << endl;
				if(!Map[i][j]) {
					if(!p && !q) hashCal(s, num);
				}
				else if(p+q == 0){
					hashCal(s, num);
					num += Map[i][j];
					int t1 = s + (1<<Bit[j-1]) + (1<<(Bit[j]+1));
					if(Map[i+1][j] && Map[i][j+1]) hashCal(t1, num);
					int t2 = s + (1<<Bit[j-1])*3;
					if(Map[i+1][j] && plug<2) hashCal(t2+1, num);
					int t3 = s + (1<<Bit[j])*3;
					if(Map[i][j+1] && plug<2) hashCal(t3+1, num);
				}
				else if(!p && q) {
					num += Map[i][j];
					if(Map[i][j+1]) hashCal(s, num);
					if(Map[i+1][j]) {
						int t = s + (1<<Bit[j-1])*q - (1<<Bit[j])*q;
						hashCal(t, num);
					}
					int t = s - (1<<Bit[j])*q;
					if(plug<2) hashCal(t+1, num);
					if(q==3 && plug==1) ans = max(ans, num);
				}
				else if(!q && p) {
					num += Map[i][j];
					if(Map[i+1][j]) hashCal(s, num);
					if(Map[i][j+1]) {
						int t = s + (1<<Bit[j])*p - (1<<Bit[j-1])*p;
						hashCal(t, num);
					}
					int t = s - (1<<Bit[j-1])*p;
					if(plug<2) hashCal(t+1, num);
					if(p==3 && plug==1) ans = max(ans, num);
				}
				else if(p+q == 2) { //合并连通块 
					num += Map[i][j];
					int b = 1; //寻找最近的右括号 
					for(int t = j+1; t <= m; t++) {
						int v = (s>>Bit[t]) % 4;
						if(v == 1) b++;
						if(v == 2) b--;
						if(!b) {
							s -= (1<<Bit[t]);
							break;
						} 
					}
					s = s - (1<<Bit[j-1])-(1<<Bit[j]);  
					hashCal(s, num);
				}
				else if(p==2 && q==2) {
					num += Map[i][j];
					int b=1;  
                    for(int t = j - 2; t >= 0; t--){//寻找最近的匹配括号   
                        int v = (s>>Bit[t]) % 4;  
                        if(v == 2) ++b;  
                        if(v == 1) --b;  
                        if(!b){  
                            s += (1<<Bit[t]);//将左括号变为右括号   
                            break;  
                        }  
                    }  
                    s = s - 2*(1<<Bit[j-1]) - 2*(1<<Bit[j]);  
                    hashCal(s, num);
				}
				else if(p==2 && q==1) {
					num += Map[i][j];
					s = s - (1<<Bit[j-1])*p - (1<<Bit[j])*q;
					hashCal(s, num);
				}
				else if(p==3 && q==1) {
					num += Map[i][j];
					int b = 1; 
					for(int t = j+1; t <= m; t++) {
						int v = (s>>Bit[t]) % 4;
						if(v == 1) b++;
						if(v == 2) b--;
						if(!b) {
							s += (3-2)*(1<<Bit[t]);
							break;
						} 
					}
					s = s - 3*(1<<Bit[j-1]) - (1<<Bit[j]);  
					hashCal(s, num);
				}
				else if(q==3 && p==2) {
					num += Map[i][j];
					int b = 1; 
					for(int t = j - 2; t >= 0; t--){//寻找最近的匹配括号   
                        int v = (s>>Bit[t]) % 4;  
                        if(v == 2) ++b;  
                        if(v == 1) --b;  
                        if(!b){  
                            s += (3-1)*(1<<Bit[t]);  
                            break;  
                        }  
                    }
                    s = s - 3*(1<<Bit[j]) - 2*(1<<Bit[j-1]);  
					hashCal(s, num);
				}
				else if(p+q==6 && plug==2) {
					//cout << "gan" << endl;
					num += Map[i][j];
					ans = max(ans, num);
				}
			}
		}
	}
	for(int i = 1; i <= total[Index]; i++) ans = max(ans, dp[Index][i]);
}
int main() {
    //freopen("input.txt", "r", stdin);
	for(int i = 0; i <= 25; i++) Bit[i] = (i+1)<<1; //求第i个插头需要右移的位置 
	int T; cin >> T;
	while(T--) {
		scanf("%d%d", &n, &m); 
		init();
		for(int i = 1; i <= n; i++) 
			for(int j = 1; j <= m; j++) {
				scanf("%d", &Map[i][j]);
				ans = max(ans, Map[i][j]);
			}
		DP();
		cout << ans << endl;
	}
    return 0;
}


















  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值