AcWing
  • 首页
  • 课程
  • 题库
  • 更多
    • 竞赛
    • 题解
    • 分享
    • 问答
    • 应用
    • 校园
  • 关闭
    历史记录
    清除记录
    猜你想搜
    AcWing热点
  • App
  • 登录/注册

P1123 取数游戏

作者: 作者的头像   jy9 ,  2024-12-04 22:27:54 ,  所有人可见 ,  阅读 1


0


#include <iostream>
#include <cstring>
using namespace std;

const int N = 10;

int mp[N][N];
int st[N][N];
int T, n, m;
int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int ans;

void dfs(int x, int y,int res){

    if(x == n + 1){
        ans = max(res, ans);
        return;
    }

    if(y == m + 1){
        dfs(x + 1, 1, res);
        return;
    }
    dfs(x, y+1, res);

    if(st[x][y]) return;

    for (int i = 0; i < 8; i ++ ){
        int xx = x + dx[i];
        int yy = y + dy[i];
        st[xx][yy] ++;
    }
    dfs(x, y + 1, res + mp[x][y]);
    for (int i = 0; i < 8; i ++ ){
        int xx = x + dx[i];
        int yy = y + dy[i];
        st[xx][yy] --;
    }

}

int main(){

    cin >> T;
    while(T --){
        memset(st, 0, sizeof st);
        cin >> n >> m;
        ans = 0;
        for (int i = 1; i <= n; i ++ ){
            for (int j = 1; j <= m; j ++ ) cin >> mp[i][j];
        }
        dfs(1, 1, 0);
        cout << ans << endl;
    }



    return 0;
}

0 评论

App 内打开
你确定删除吗?
1024
x

© 2018-2025 AcWing 版权所有  |  京ICP备2021015969号-2
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息