40分,六个点WA

P1434 [SHOI2002] 滑雪

Bonfire_Lit @ 2020-08-16 10:06:58

#include<cstdio>
#include<algorithm>
using namespace std;
int m[1002][1002],r,c,ans=1;
int cun[1002][1002];
bool gao(int x,int y) {
    return m[x-1][y]<m[x][y]&&m[x][y-1]<m[x][y]&&m[x+1][y]<m[x][y]&&m[x][y+1]<m[x][y];
}
int f(int x,int y) {
    if(cun[x][y])return cun[x][y];
    int sum=1;
    if(m[x-1][y]<m[x][y]) {
        if(cun[x-1][y])sum=max(cun[x-1][y]+1,sum);
        else sum=max(f(x-1,y)+1,sum);
    }
    if(m[x+1][y]<m[x][y]) {
        if(cun[x+1][y])sum=max(cun[x+1][y]+1,sum);
        else sum=max(f(x+1,y)+1,sum);
    }
    if(m[x][y-1]<m[x][y]) {
        if(cun[x][y-1])sum=max(cun[x][y-1]+1,sum);
        else sum=max(f(x,y-1)+1,sum);
    }
    if(m[x][y+1]<m[x][y]) {
        if(cun[x][y+1])sum=max(cun[x][y+1]+1,sum);
        else sum=max(f(x,y+1)+1,sum);
    }
    cun[x][y]=sum;
    return sum;
}
int main() {
    scanf("%d%d",&r,&c);
    for(int i=1; i<=r; i++) {
        for(int j=1; j<=c; j++) {
            scanf("%d",&m[i][j]);
        }
    }
    for(int i=1; i<=r; i++) {
        for(int j=1; j<=c; j++) {
            if(gao) {
                ans=max(ans,f(i,j));
            }
        }
    }
    printf("%d",ans);
}

请问我这个算法有什么问题吗?


by YellowEgg @ 2020-08-16 10:36:56

@Bonfire_Lit

if(m[x-1][y]<m[x][y]) 

所有的这几个if都要判断是否越界


by Bonfire_Lit @ 2020-08-16 10:49:24

@YellowEgg

#include<cstdio>
#include<algorithm>
using namespace std;
int m[1002][1002],r,c,ans=1;
int cun[1002][1002];
bool gao(int x,int y) {
    return m[x-1][y]<m[x][y]&&m[x][y-1]<m[x][y]&&m[x+1][y]<m[x][y]&&m[x][y+1]<m[x][y];
}
int f(int x,int y) {
    if(cun[x][y])return cun[x][y];
    int sum=1;
    if((m[x-1][y]<m[x][y])&&x-1<=r&&y<=c) {
        if(cun[x-1][y])sum=max(cun[x-1][y]+1,sum);
        else sum=max(f(x-1,y)+1,sum);
    }
    if((m[x+1][y]<m[x][y])&&x+1<=r&&y<=c) {
        if(cun[x+1][y])sum=max(cun[x+1][y]+1,sum);
        else sum=max(f(x+1,y)+1,sum);
    }
    if((m[x][y-1]<m[x][y])&&x<=r&&y-1<=c) {
        if(cun[x][y-1])sum=max(cun[x][y-1]+1,sum);
        else sum=max(f(x,y-1)+1,sum);
    }
    if((m[x][y+1]<m[x][y])&&x<=r&&y+1<=c) {
        if(cun[x][y+1])sum=max(cun[x][y+1]+1,sum);
        else sum=max(f(x,y+1)+1,sum);
    }
    cun[x][y]=sum;
    return sum;
}
int main() {
    scanf("%d%d",&r,&c);
    for(int i=1; i<=r; i++) {
        for(int j=1; j<=c; j++) {
            scanf("%d",&m[i][j]);
        }
    }
    for(int i=1; i<=r; i++) {
        for(int j=1; j<=c; j++) {
            if(gao) {
                ans=max(ans,f(i,j));
            }
        }
    }
    printf("%d",ans);
}

新的代码加了判边但是依旧是40?(笑哭)


by YellowEgg @ 2020-08-16 10:52:24

@Bonfire_Lit

if(m[x-1][y]<m[x][y]&&x-1<=r&&y<=c )

x-1要判断是否大于1


by YellowEgg @ 2020-08-16 11:11:43

应该是大于等于


by Bonfire_Lit @ 2020-08-16 11:21:59

@YellowEgg ohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh过了过了过了多谢神犇拔刀相助小女子愿以身相许当牛做马在所不辞


|