1 条题解

  • 0
    @ 2025-10-27 14:56:05
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    int n, k;
    int g[10][10];
    int ans = -1;
    
    // DFS搜索所有路径
    void dfs(int x, int y, int sum, int cnt) {
    // 到达终点
        if (x == n && y == n) {
            if (cnt == k) {
                ans = max(ans, sum);
            }
            return;
        }
    
    // 向右移动
        if (y + 1 <= n) {
            int ne = g[x][y + 1];
            int pl = (ne % 2 == 0) ? 1 : 0;
            dfs(x, y + 1, sum + ne, cnt + pl);
        }
    
    // 向下移动
        if (x + 1 <= n) {
            int ne = g[x + 1][y];
            int pl = (ne % 2 == 0) ? 1 : 0;
            dfs(x + 1, y, sum + ne, cnt + pl);
        }
    
    }
    
    int main() {
    // 读取输入
        cin >> n >> k;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                cin >> g[i][j];
            }
        }
    
    // 起点(1,1)的格子也要算上
        int st = (g[1][1] % 2 == 0) ? 1 : 0;
        int stsum = g[1][1];
    
    // 开始DFS搜索
        dfs(1, 1, stsum, st);
    
    // 输出结果
        cout << ans << endl;
    
        return 0;
    
    }
    
    
    
    
    • 1

    信息

    ID
    1159
    时间
    1000ms
    内存
    256MiB
    难度
    9
    标签
    递交数
    16
    已通过
    4
    上传者