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;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值