-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFS取数组最大值.cpp
58 lines (57 loc) · 1.48 KB
/
DFS取数组最大值.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//一个N×M的由非负整数构成的数字矩阵,你需要在其中取出若干个数字,使得取出的任意两个数字不相邻(若一个数字在另外一个数字相邻8个格子中的一个即认为这两个数字相邻),求取出数字和最大是多少。
# include<iostream>
# include<cstdio>
# include<cstring>
# include<cmath>
using namespace std;
int arr[10][10],res[10][10];
int n,m,put,ans;
int dir[8][2]={{1,0},{-1,0},{0,-1},{0,1},{1,1},{-1,-1},{1,-1},{-1,1}};
void DFS(int x,int y){
if(y == m){
DFS(x+1,0);
return;
}
if(x == n){
put = max(put, ans);
return;
}
DFS(x,y+1);
if(!res[x][y]){
ans += arr[x][y];
for(int i = 0; i < 8; i++){
int xx = dir[i][0]+x;
int yy = dir[i][1]+y;
if(xx<n&&xx>=0&&yy<m&&yy>=0){
++res[xx][yy];
}
}
DFS(x,y+1);
for(int i = 0; i < 8; i++){
int xx = dir[i][0]+x;
int yy = dir[i][1]+y;
if(xx<n&&xx>=0&&yy<m&&yy>=0){
--res[xx][yy];
}
}
ans -= arr[x][y];
}
}
int main(){
int t;
cin >> t;
while(t--){
put = 0;
memset(arr,0,sizeof(arr));
memset(res,0,sizeof(res));
cin>>n>>m;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin>>arr[i][j];
}
}
DFS(0,0);
cout<<put<<endl;
}
return 0;
}