• CDoj--Search gold(dp)


    Search gold

    Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
     

    Dreams of finding lost treasure almost came true recently. A new machine called 'The Revealer' has been invented and it has been used to detect gold which has been buried in the ground. The machine was used in a cave near the seashore where -- it is said -- pirates used to hide gold. The pirates would often bury gold in the cave and then fail to collect it. Armed with the new machine, a search party went into the cave hoping to find buried treasure. The leader of the party was examining the soil near the entrance to the cave when the machine showed that there was gold under the ground. Very excited, the party dug a hole two feel deep. They finally found a small gold coin which was almost worthless. The party then searched the whole cave thoroughly but did not find anything except an empty tin trunk. In spite of this, many people are confident that 'The Revealer' may reveal something of value fairly soon.

    So,now you are in the point(1,1)(1,1) and initially you have 0 gold.In the nn*mm grid there are some traps and you will lose gold.If your gold is not enough you will be die.And there are some treasure and you will get gold.If you are in the point(x,y),you can only walk to point (x+1,y),(x,y+1),(x+1,y+2)(x+1,y),(x,y+1),(x+1,y+2)and(x+2,y+1)(x+2,y+1).Of course you can not walk out of the grid.Tell me how many gold you can get most in the trip.

    It`s guarantee that(1,1)(1,1)is not a trap;

    Input

    first come 22 integers, n,mn,m(1n10001≤n≤1000,1m10001≤m≤1000)

    Then follows nn lines with mm numbers aijaij

    (100<=aij<=100)(−100<=aij<=100)

    the number in the grid means the gold you will get or lose.

    Output

    print how many gold you can get most.

    Sample input and output

    Sample InputSample Output
    3 3
    1 1 1
    1 -5 1
    1 1 1
    5
    3 3
    1 -100 -100
    -100 -100 -100
    -100 -100 -100
    1

    Source

    第七届ACM趣味程序设计竞赛第四场(正式赛)
    想出是个dp, 却写不出来;
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    using namespace std;  
    int ac[4][2]={0, 1, 1, 0, 1, 2, 2, 1};
    int G[1001][1001], vis[1001][1001], dp[1001][1001];
    int n, m;
    bool Check(int x, int y)
    {
        if(x>=1 && x<=n && y>=1 && y<= m)
            return 1;
        return 0;
    }
    int main()
    {
        while(scanf("%d%d", &n, &m) != EOF)
        {
            for(int i=1; i<= n; i++)
                for(int j=1; j<=m; j++)
                    scanf("%d", &G[i][j]);
            memset(dp, -1, sizeof(dp));
            dp[1][1]=G[1][1];
            int ans=-1;
            for(int i=1; i<=n; i++)
                for(int j=1; j<=m; j++)
                {
                    if(dp[i][j]<0) continue;
                    ans=max(dp[i][j], ans);
                    for(int k=0; k<4; k++)
                    {
                        int x=i+ac[k][0]; int y=j+ac[k][1];
                        if(!Check(x, y)) continue;
                        //printf("%d %d
    ", x, y);
                        dp[x][y]=max(dp[i][j]+G[x][y], dp[x][y]);    
                    } 
                } 
            printf("%d
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    微信公共服务平台开发(.Net 的实现)5-------解决access_token过期的问题
    微信公共服务平台开发(.Net 的实现)4-------语音识别
    微信公共服务平台开发(.Net 的实现)3-------发送文本消息
    微信公共服务平台开发(.Net 的实现)2-------获得ACCESSTOKEN
    微信公共服务平台开发(.Net 的实现)1-------认证“成为开发者”
    checkboxlist 横向显示,自动换行
    VMware Workstation unrecoverable error: (vmx)虚拟机挂起后无法启动问题
    ASP.NET给DataGrid,Repeater等添加全选批量删除等功能
    redis 优缺点 使用场景
    redis消息队列
  • 原文地址:https://www.cnblogs.com/soTired/p/5296352.html
Copyright © 2020-2023  润新知