80分求助

P1434 [SHOI2002] 滑雪

luqyou @ 2022-07-25 11:57:28

#include<iostream>
#include<queue>
using namespace std;
struct Node{
    int i,j,num;
};
struct cmp{
    bool operator()(Node x,Node y){
        return x.num>y.num;
    }
};
Node node;
priority_queue<Node,vector<Node>,cmp>q;
int n,m,maxn,g[101][101],dp[101][101];
int x,y,height;
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            dp[i][j]=1;
            cin>>g[i][j];
            node.i=i;
            node.j=j;
            node.num=g[i][j];
            q.push(node);
        }
    }
    while(!q.empty()){

        node=q.top();
        x=node.i;
        y=node.j;
        height=node.num;
        q.pop();
        if(g[x-1][y]<height) 
            dp[x][y]=max(dp[x][y],dp[x-1][y]+1);
        if(g[x+1][y]<height) 
            dp[x][y]=max(dp[x][y],dp[x+1][y]+1);
        if(g[x][y-1]<height) 
            dp[x][y]=max(dp[x][y],dp[x][y-1]+1);
        if(g[x][y+1]<height) 
            dp[x][y]=max(dp[x][y],dp[x][y+1]+1);
        if(maxn<dp[x][y]) 
            maxn=dp[x][y];
    }
    cout<<maxn << endl;
    return 0;
}

by miaonei214 @ 2022-08-06 19:18:10

你没有判断下一步的坐标是否越界。 故四个条件语句应该改为: 1.if(g[x-1][y]<height&&x!=1) 2.if(g[x+1][y]<height&&x!=n) 3.if(g[x][y-1]<height&&y!=1) 4.if(g[x][y+1]<height&&y!=m)


|