PersonalCorpus 版 (精华区)

40684 2002-10-11 13:14:53 Accepted
1107 C++ 00:02.90 460K Big Guava

static int p[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; 

int b[100][100];
int map[100][100];
int n,m;

void tryit (int x, int y)
{
    int i,j,k,c,d;

    for (k=0;k<4;k++) {
        c=x; d=y;
        for (i=1;i<=m;i++) {
            c+=p[k][0]; d+=p[k][1];
            if ((c<0)||(c==n)||(d<0)||(d==n)) break;
            if (map[c][d]>map[x][y]) if (map[c][d]+b[x][y]>b[c][d]) {
                    b[c][d]=map[c][d]+b[x][y];
                    tryit(c,d);
            }
        }
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("c:\\0acm\\zju\\i1107","r",stdin);
    freopen("c:\\0acm\\zju\\o1107","w",stdout);
#endif

    int i,j,k;

    while (1) {
        scanf("%d%d\n",&n,&m);
        if ((n==-1)&&(m==-1)) break;
        memset(map,0,sizeof(map));
        memset(b,0,sizeof(b));
        for (i=0;i<n;i++) for (j=0;j<n;j++) scanf("%d\n",&map[i][j]);
        b[0][0]=map[0][0];
        tryit(0,0);
        k=0; for (i=0;i<n;i++) for (j=0;j<n;j++) if (b[i][j]>k) 
k=b[i][j];
        printf("%d\n",k);
    }
}
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:2.281毫秒