P1123 错了
  • 板块学术版
  • 楼主Commandant
  • 当前回复6
  • 已保存回复6
  • 发布时间2022/8/3 20:00
  • 上次更新2023/10/27 17:09:50
查看原帖
P1123 错了
456419
Commandant楼主2022/8/3 20:00
#include<iostream>
#include<climits>
#include<cstring>
#include<cstdio>
using namespace std;

const int maxn = 10;

int t;
int n, m;
int a[maxn][maxn];

bool in(int x, int y) {
    return (x >= 1 && x <= n && y >= 1 && y <= m);
}
bool mark[maxn][maxn];
int dx[8] = { 0, 1, 0, -1, 1, -1, 1, -1 };
int dy[8] = { 1, 0, -1, 0, 1, -1, -1, 1 };
int ans, now;
void dfs(int x, int y) {
    if (y == m + 1) {
        dfs(x + 1, 1);
        return;
    }
    if (x == n + 1) {
        ans = max(ans, now);
        return;
    }

    dfs(x, y + 1);

    if (!mark[x][y]) {
        for (int i = 0; i < 8; i++) {
            int xx = x + dx[i];
            int yy = y + dy[i];
            if (in(xx, yy)) mark[xx][yy] = 1;
        }
        now += a[x][y];
        dfs(x, y + 1);
        now -= a[x][y];
        for (int i = 0; i < 8; i++) {
            int xx = x + dx[i];
            int yy = y + dy[i];
            if (in(xx, yy)) mark[xx][yy] = 0;
        }
    }
}

int main() {
    cin >> t;
    while (t--) {
        memset(mark, 0, sizeof(mark));
        memset(a, 0, sizeof(a));
        ans = 0, now = 0;
        cin >> n >> m;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                cin >> a[i][j];
            }
        }
        dfs(1, 1);
        cout << ans << endl;
    }
    return 0;
}
2022/8/3 20:00
加载中...