这样的写法是错在哪了呢?
  • 板块P1123 取数游戏
  • 楼主jor蛋
  • 当前回复7
  • 已保存回复7
  • 发布时间2021/11/17 00:29
  • 上次更新2023/11/4 00:21:35
查看原帖
这样的写法是错在哪了呢?
72921
jor蛋楼主2021/11/17 00:29
#include<bits/stdc++.h>
using namespace std;
int n,m,t;
int g[10][10];
bool st[10][10];
int res;
int dx[8]={-1,-1,-1,0,1,1,1,0};
int dy[8]={-1,0,1,1,1,0,-1,-1};
bool check(int x,int y){
    for(int i=0;i<8;i++){
        int a=x+dx[i],b=y+dy[i];
        if(st[a][b])
            return false;
    }
    return true;
}
void dfs(int sum,int ii,int jj){
    if(jj>m)   jj=1,ii++;
    if(ii>n) return ;
    res=max(res,sum);
    for(int i=ii;i<=n;i++){
        for(int j=jj;j<=m;j++){
            if(check(i,j)){
                st[i][j]=true;
               //for(int iw=1;iw<=n;iw++){ for(int jw=1;jw<=m;jw++) if(st[iw][jw]) cout<<-1<<' '; else cout<<g[iw][jw]<<' '; cout<<endl; }
              //  cout<<endl;
                dfs(sum+g[i][j],i,j+2);
                st[i][j]=false;
            }
        }
    }
}
int main(){
    cin>>t;
    while(t--){
        cin>>n>>m;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                cin>>g[i][j];
        res=0;
        dfs(0,1,1);
        memset(st,false,sizeof st);
        cout<<res<<endl;
    }
}

求大佬指点

2021/11/17 00:29
加载中...