首页 > 其他 > 详细

传纸条

时间:2021-04-09 09:47:20      阅读:22      评论:0      收藏:0      [点我收藏+]

传纸条

这道题要注意的是横坐标取值范围:

   x + y = k
=> y = k - x
=> 1 <= y = k - x <= m
=> k - m <= x <= k - 1

    for (int k = 2; k <= m + n; ++k)
        for (int x1 = max(1, k - m); x1 <= n && x1 < k; ++x1)
            for (int x2 = max(1, k - m); x2 <= n && x2 < k; ++x2)

#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PII pair<int, int>
#define VIT vector<int>
#define x first
#define y second
#define inf 0x3f3f3f3f
const int N = 55;
int w[N][N];
int f[N << 1][N][N];
int dx[4][2] = { {0, 0}, {0, -1}, {-1, 0}, {-1, -1} };

int main() {
    //freopen("in.txt", "r", stdin);
    IO;
    int m, n;
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j) cin >> w[i][j];
    
    for (int k = 2; k <= m + n; ++k)
        for (int x1 = max(1, k - m); x1 <= n && x1 < k; ++x1)
            for (int x2 = max(1, k - m); x2 <= n && x2 < k; ++x2)
                for (int i = 0; i < 4; ++i) 
                    if (x1 != x2 || k == 2 || k == m + n) {
                        int v = w[x1][k - x1] + w[x2][k - x2];
                        f[k][x1][x2] = max(f[k][x1][x2], f[k - 1][x1 + dx[i][0]][x2 + dx[i][1]] + v);
                    }
    cout << f[m + n][n][n] << ‘\n‘; 
    return 0; 
}


传纸条

原文:https://www.cnblogs.com/phr2000/p/14635303.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!