1 条题解

  • 0
    @ 2025-3-23 16:42:07
    #include <bits/stdc++.h>
    using namespace std;
    int T;
    int n, m;
    int a[10][10];
    bool vis[10][10];
    int ans;
    void dfs(int x, int y, int sum)
    {
        if (y > m)
        {
            x++;
            y = 1;
        }
        if (x > n)
        {
            ans = max(ans, sum);
            return;
        }
        dfs(x, y + 1, sum);
        if (!vis[x - 1][y - 1] && !vis[x - 1][y] && !vis[x - 1][y + 1] &&
            !vis[x][y - 1])
        {
            vis[x][y] = true;
            dfs(x, y + 1, sum + a[x][y]);
            vis[x][y] = false;
        }
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin >> T;
        while (T--)
        {
            cin >> n >> m;
            for (int i = 1; i <= n; i++)
                for (int j = 1; j <= m; j++)
                {
                    cin >> a[i][j];
                    vis[i][j] = false;
                }
            ans = 0;
            dfs(1, 1, 0);
            cout << ans << "\n";
        }
        return 0;
    }
    
    • 1

    信息

    ID
    1837
    时间
    1000ms
    内存
    125MiB
    难度
    3
    标签
    递交数
    2
    已通过
    2
    上传者