题目:取数游戏
思路:裸的dfs。
代码:
#include<bits/stdc++.h>
using namespace std;
#define maxn 6
int n,m;
int a[maxn+5][maxn+5];
bool isuse[maxn+5][maxn+5];
int ans=0;
void dfs(int x,int y,int s) {
if(y>m) y=1,x++;
if(x==n+1) {
ans=max(ans,s);
return ;
}
if(!isuse[x-1][y]&&!isuse[x][y-1]&&!isuse[x-1][y-1]&&!isuse[x-1][y+1]) {
isuse[x][y]=true;
dfs(x,y+1,s+a[x][y]);
isuse[x][y]=false;
}
dfs(x,y+1,s);
}
int main() {
int T;
scanf("%d",&T);
while(T--) {
memset(isuse,0,sizeof(isuse));
ans=0;
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
scanf("%d",&a[i][j]);
}
}
dfs(1,1,0);
printf("%d\n",ans);
}
}