RE求调,给关注
查看原帖
RE求调,给关注
1129161
huangchengyi楼主2024/8/19 18:02
#include <bits/stdc++.h>
using namespace std;
int a[10][10];
int b[10][10];
int mx, n, m;

int ne[8][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, -1}, {-1, 1}, {1, 1}, {-1, -1}};
void dfs(int x, int y, int cnt) {
	if (x == n - 1 && y == m ) {
		mx = max(mx, cnt);
		return;
	}
	if (y == m) {
		y = 0;
		x++;
	}
	if (b[x][y] == 0) {
		b[x][y] = 1;
		for (int i = 0; i < 8; i++) {
			int tx = ne[i][0] + x;
			int ty = ne[i][1] + y;
			if (tx >= 0 && tx < n && ty >= 0 && ty < m ) {
				b[tx][ty] = 1;
			}
		}
		dfs(x, y + 1, cnt + a[x][y]);
		b[x][y] = 0;
		for (int i = 0; i < 8; i++) {
			int tx = ne[i][0] + x;
			int ty = ne[i][1] + y;
			if (tx >= 0 && tx < n && ty >= 0 && ty < m ) {
				b[tx][ty] = 0;
			}
		}
	}
	dfs(x, y + 1, cnt);
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int t;
	cin >> t;
	while (t--) {
		int n, m;
		cin >> n >> m;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cin >> a[i][j];
				b[i][j] = 0;
			}
		}
		mx = 0;
		dfs(0, 0, 0);
		cout << mx;
	}
	return 0;
}
2024/8/19 18:02
加载中...