方格取数(状压DP)

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int maxn=25;
int n, num[maxn][maxn], sum[maxn][maxn], dp[maxn][maxn];
int state[maxn], cnt=0;

void init(){
	for(int i=1; i<(1<<n); i++)
	{
		if((i&(i<<1))==0)//判断取的两个数是否相邻的方法
		{
			state[++cnt]=i;//保存状态
			
			for(int j=1; j<=n; j++)
				for(int k=0; k<n; k++)
						if((i>>k)&1)   sum[j][cnt]+=num[j][k+1];
		}	
	}
}

void solve(){
	for(int j=1; j<=cnt; j++)
		dp[1][j]=sum[1][j];
	
	for(int i=2; i<=n; i++)
	{
		for(int j=1; j<=cnt; j++)
			for(int k=1; k<=cnt; k++)
				if((state[j]&state[k])==0)
					dp[i][j]=max(dp[i][j], dp[i-1][k] + sum[i][j]);//第i层j状态的状态转移方程
	}
	int mx=0;
	for(int i=1; i<=cnt; i++)
		mx=max(mx, dp[n][i]);
	printf("%d\n", mx);
	
	return ;
}

int main(){
	scanf("%d", &n);
	
	for(int i=1; i<=n; i++)
		for(int j=1; j<=n; j++)
			scanf("%d", &num[i][j]);
	
	init();
	
	solve();
	
	return 0;
}

### 方格问题的动态规划算法实现 #### 问题描述 在一个 \( N \times N \)方格图中,某些方格填有正整值,其余为空白(即值为 0)。目标是从左上角 (\(1,1\)) 走到右下角 (\(N,N\)),允许走两次不同的路径,并使这两条路径上的字总和最大化。每经过一个方格,可以拾起其上的字。 --- #### 解决思路 此问题可以通过 **二维动态规划** 来解决。由于需要考虑两条不重叠的路径,因此态设计较为复杂。以下是具体分析: 1. **定义态变量** 设定四维的态表示方式: - \( dp[x1][y1][x2][y2] \): 表示第一条路径到达坐标 (\(x1,y1\)) 和第二条路径到达坐标 (\(x2,y2\)) 时的最大得分。 如果两条路径当前位于同一格子,则只累加一次该格子的值;如果不在同一格子,则分别累加对应的值[^3]。 2. **初始化条件** 初始化起点 (\(dp[1][1][1][1]\)) 的初始值为网格中的第一个单元格值 \( grid[1][1] \)[^5]。 3. **转移方程** 对于每一个可能的(\(x1,y1,x2,y2\)): - 假设可以从四个方向之一移动过来:上方或左侧。 - 更新公式如下: ```python if (x1 == x2 and y1 == y2): dp[x1][y1][x2][y2] = max( dp[x1-1][y1][x2-1][y2], # 上一步均为向上 dp[x1-1][y1][x2][y2-1], # 第一条路径向上,第二条向左 dp[x1][y1-1][x2-1][y2], # 第一条路径向左,第二条向上 dp[x1][y1-1][x2][y2-1] # 双方均向左 ) + grid[x1][y1] else: dp[x1][y1][x2][y2] = max( dp[x1-1][y1][x2-1][y2], dp[x1-1][y1][x2][y2-1], dp[x1][y1-1][x2-1][y2], dp[x1][y1-1][x2][y2-1] ) + grid[x1][y1] + grid[x2][y2] ``` 4. **边界处理** 需要特别注意越界情况以及当两条路径相交的情况下的特殊逻辑。 5. **优化空间复杂度** 使用滚动组技术来减少内存消耗。因为每次更新仅依赖前一时刻的据,故可将三维缩至两层循环内的二维存储结构[^5]。 6. **最终结果提** 结果存放在终点处的所有可能性之中最大的那个值里头,也就是遍历所有可能结束位置组合求得最大值作为答案返回给调用者函。 --- #### Python 实现代码 下面提供了一个基于上述理论框架的具体实现版本: ```python def max_sum_of_two_paths(grid): n = len(grid) # Initialize DP table with zeros. dp = {} # Initial state setup at the start point of both paths being same i.e., top-left corner. dp[(1, 1, 1, 1)] = grid[1][1] directions = [(0,-1), (-1,0)] for sum_steps in range(2, 2*n): new_dp = {} for pos1 in range(max(sum_steps-n+1,1), min(n,sum_steps)+1): pos2 = sum_steps - pos1 if not (1<=pos2<=n): continue for path1_dir in directions: prev_pos1_x = pos1-path1_dir[0] prev_pos1_y = pos2-path1_dir[1] if not (1<=prev_pos1_x<=n and 1<=prev_pos1_y<=n): continue for path2_dir in directions: prev_pos2_x = pos1-path2_dir[0] prev_pos2_y = pos2-path2_dir[1] if not (1<=prev_pos2_x<=n and 1<=prev_pos2_y<=n): continue key_prev = (prev_pos1_x, prev_pos1_y, prev_pos2_x, prev_pos2_y) if key_prev not in dp: continue current_key = (pos1,pos2,min(pos1,pos2),max(pos1,pos2)) value_to_add = ( grid[pos1][pos2] + ((grid[pos1][pos2]) if pos1 != pos2 or pos1==pos2 else 0 ) ) if current_key not in new_dp: new_dp[current_key] = dp[key_prev]+value_to_add else: new_dp[current_key] = max(new_dp[current_key], dp[key_prev]+value_to_add) dp = new_dp result = 0 for k,v in dp.items(): if v>result: result=v return result ``` --- #### 复杂度分析 时间复杂度主要由量决定,大约为 \(O(N^4)\),但由于实际计算过程中会有很多剪枝操作,通常运行效率较高。空间复杂度则通过滚动组降低到了 \(O(N^2)\)。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值