• 【u108】取数游戏


    Time Limit: 1 second
    Memory Limit: 128 MB

    【问题描述】

    一个N×M的由非负整数构成的数字矩阵,你需要在其中取出若干个数字,使得取出的任意两个数字不相邻(若一个数字在另外一个数字相邻8个格子中的一个即认为这两个数字相邻),求取出数字和最大是多少。

    【输入格式】

    输入文件number.in的第1行有一个正整数T,表示了有T组数据。 对于每一组数据,第1行有两个正整数N和M,表示了数字矩阵为N行M列。 接下来N行,每行M个非负整数,描述了这个数字矩阵。 。
    【输出格式】

    输出文件number.out包含T行,每行一个非负整数,输出所求得的答案。

    【数据规模】

    对于20%的数据,N, M≤3; 对于40%的数据,N, M≤4; 对于60%的数据,N, M≤5; 对于100%的数据,N, M≤6,T≤20。

    Sample Input1

    3
    4 4
    67 75 63 10
    29 29 92 14
    21 68 71 56
    8 67 91 25
    2 3
    87 70 85
    10 3 17
    3 3
    1 1 1
    1 99 1
    1 1 1

    Sample Output1

    271
    172
    99

    【样例说明】

    对于第1组数据,取数方式如下:

    *67 75 63 10
    29 29 *92 14
    *21 68 71 56
    8 67 *91 25

    【题目链接】:http://noi.qz5z.com/viewtask.asp?id=u108

    【题解】

    dfs
    n和m都不大、一格一格地枚举该格要不要取数字就好;
    从左到右从上到下;
    如果该格取数了,直接往右跳2格;没取数就往右跳1格;
    这样我们在判断这个地方能不能取数字的时候就只要看
    左上、上、右上这3个格子就可以了;(下面不可能有格子被取,我们还没搜到那里呢!);

    【完整代码】

    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <set>
    #include <map>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <queue>
    #include <vector>
    #include <stack>
    #include <string>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    void rel(LL &r)
    {
        r = 0;
        char t = getchar();
        while (!isdigit(t) && t!='-') t = getchar();
        LL sign = 1;
        if (t == '-')sign = -1;
        while (!isdigit(t)) t = getchar();
        while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
        r = r*sign;
    }
    
    void rei(int &r)
    {
        r = 0;
        char t = getchar();
        while (!isdigit(t)&&t!='-') t = getchar();
        int sign = 1;
        if (t == '-')sign = -1;
        while (!isdigit(t)) t = getchar();
        while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
        r = r*sign;
    }
    
    const int MAXN = 10;
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    
    int n,m;
    LL a[MAXN][MAXN],ans;
    bool bo[MAXN][MAXN];
    
    void dfs(int x,int y,LL now)
    {
        if (y>m)
            x=x+1,y = 1;
        if (x==n+1)
        {
            ans = max(ans,now);
            return;
        }
        bool j1,j2,j3;
        if (x-1<1 || y-1<1) j1 = true; else j1 = bo[x-1][y-1];
        if (x-1<1) j2 = true;else j2 = bo[x-1][y];
        if (x-1<1 || y+1>m) j3 = true;else j3 = bo[x-1][y+1];
        if (j1&&j2&&j3)
        {
            bo[x][y] = false;
            dfs(x,y+2,now+a[x][y]);
            bo[x][y] = true;
        }
        dfs(x,y+1,now);
    }
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        int T;
        rei(T);
        while (T--)
        {
            ans = 0;
            memset(bo,true,sizeof(bo));
            rei(n);rei(m);
            rep1(i,1,n)
                rep1(j,1,m)
                    rel(a[i][j]);
            dfs(1,1,0);
            cout << ans << endl;
        }
        return 0;
    }
    
  • 相关阅读:
    js 遍历EL表达式 list对象
    easyui-datebox 点击事件
    java-启动和关闭.exe程序
    css 样式控制文本过长实现省略号
    java-plupload上传大文件
    .ajax向后台传递数组(转)
    <c:> </c:>
    访问修饰符
    附录A培训实习生-面向对象基础方法重载(3)
    附录A培训实习生-面向对象基础构造方法和带参数的构造方法(2)
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626914.html
Copyright © 2020-2023  润新知