zoukankan      html  css  js  c++  java
  • UVa 10913 Walking on a Grid dp

    4th IIUC Inter-University Programming Contest, 2005

    I

    Walking on a Grid

    Input: standard input
    Output: standard output

    Problemsetter: Sohel Hafiz

    You will be given a square grid of size N × N. The top-left square has a coordinate of (1, 1) and that of bottom-right is (N, N). Your job is to walk from (1, 1) to (N, N). Very easy, right? That’s why you have to follow some rules when walking.

    1. You can only move left, right or down.
    2. (i, j-1) is left of (i, j), (i, j+1) is right of (i, j) and (i+1, j) is down of (i, j).
    3. You can never move outside the grid.
    4. You can not step on a cell more than once.
    5. Every cell has an integer associated with it.
    6. You have to make sure the sum of integers of the path is maximized.
    7. You can step on at most negative integers from source to destination.

    Input

    Each case will start with two integers N and kN ≤ 75 and k ≤ 5. Each of the next N lines will containN integers each given in row major order. That is, the first integer of the first row is (1, 1) and the last integer of last row is (N, N). Input terminates with two zeros on a line.

    Output

    For every case output the case number. If it’s not possible to reach the destination meeting the above rules then output “impossible”, else print the maximum sum of integers of the path.

    Sample Input

    Output for Sample Input

    4 1
    1 2 3 -5
    -10 6 0 -1
    -10 -10 -10 2
    0 0 0 1
    4 0
    1 2 3 -5
    -10 6 0 -1
    -10 -10 -10 2
    0 0 0 1
    0 0

    Case 1: 11
    Case 2: impossible

    ----------------------

    =。= 忘了初始化结果无限wa

    ----------------------

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    
    const long long OO=1e17;
    long long a[111][111];
    long long f[111][111][6][3];
    bool v[111][111][6][3];
    int n;
    
    long long dfs(int x,int y,int k,int tp)
    {
        long long &ret=f[x][y][k][tp];
        long long tmp;
        if (v[x][y][k][tp]) return ret;
        v[x][y][k][tp]=true;
        ret=-OO;
        if (a[x][y]<0) k--;
        if (k<0) return -OO;
        if (x==1&&y==1) return ret=a[1][1];
        if (tp!=1&&y>1)
        {
            tmp=dfs(x,y-1,k,2);
            if (tmp!=-OO) ret=max(ret,tmp+a[x][y]);
        }
        if (tp!=2&&y<n)
        {
            tmp=dfs(x,y+1,k,1);
            if (tmp!=-OO) ret=max(ret,tmp+a[x][y]);
        }
        if (x>1)
        {
            tmp=dfs(x-1,y,k,0);
            if (tmp!=-OO) ret=max(ret,tmp+a[x][y]);
        }
        return ret;
    }
    
    int main()
    {
        int cnt=1;
        int k;
        while (cin>>n>>k)
        {
            if (n==0&&k==0) break;
            memset(v,0,sizeof(v));
            for (int i=1; i<=n; i++)
            {
                for (int j=1; j<=n; j++)
                {
                    cin>>a[i][j];
                }
            }
            long long ans=dfs(n,n,k,0);
            cout<<"Case "<<cnt++;
            if (ans==-OO) cout<<": impossible"<<endl;
            else cout<<": "<<ans<<endl;
        }
        return 0;
    }
    




  • 相关阅读:
    spring子模块----->Spring Security------->相关教程(参考资料)
    Maven--->学习心得--->maven 概述
    Spring和Spring MVC 、Spring Security的关系
    leapMotion简介
    软件工程需求分析
    大型web网站-----系统架构
    Maven的安装与配置
    A Java Exception occured 解决
    mysql-5.7.20安装和配置
    线段树 poj 3667
  • 原文地址:https://www.cnblogs.com/cyendra/p/3226380.html
Copyright © 2011-2022 走看看