求助,记搜还是T了一个点

P1434 [SHOI2002] 滑雪

焚魂 @ 2021-02-08 22:52:53

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int d,c,ans = -1,un = -1;
int map[110][110];
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
int f[110][110];

int dfs(register int x,register int y) {
    if(f[110][110]) {
        return f[110][110];
    }
    f[x][y] = 1;
    for(register int i = 0;i < 4;i++) {
        register int xx = x+dx[i];
        register int yy = y+dy[i];
        if(xx < 1 || xx > d || yy < 1 || yy > c || map[xx][yy] >= map[x][y])
        continue;
        dfs(xx,yy);
        f[x][y] = max(f[x][y],f[xx][yy]+1);
    }
    return f[x][y];
}

int main() {
    cin >> d >> c;
    for(register int i = 1;i <= d;i++) {
        for(register int j = 1;j <= c;j++) {
            scanf("%d",&map[i][j]);
        }
    }

    for(register int i = 1;i <= d;i++) {
        for(register int j = 1;j <= c;j++) {
            un = max(dfs(i,j),un);
        }
    }

    printf("%d",un);

    return 0;
}

by _caiji_ @ 2021-02-08 22:54:23

if(f[110][110]) {
        return f[110][110];
    }

记忆完不用几个意思\kk


by 老子是北瓜 @ 2021-02-08 22:55:25

@焚魂 dfs函数开头

    if(f[110][110])

错了吧?


by 焚魂 @ 2021-02-08 22:57:37

@老子是北瓜 @caijianhong 失误打错了(打扰了打扰了


by My_Faith @ 2021-02-09 21:39:43

@老子是北瓜 Orz,神仙爆切记搜,蒟蒻还在打A+B


by 老子是北瓜 @ 2021-02-09 22:24:49

@My_Faith

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
//#include<windows.h> 
using namespace std;
#define N 103
struct block{
    int x,y,v;
} b[N*N];
bool cmp(block b1,block b2){
    return b1.v>b2.v;
}
int dis[N][N],g[N][N];
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0}; 
int main(){
    int n,m,t=0;
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++){
            ++t;
            b[t].x=i;
            b[t].y=j;
            scanf("%d",&b[t].v);
            g[i][j]=b[t].v;
        }
    sort(b+1,b+1+t,cmp);
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
            dis[i][j]=1; 
    for(int i=1; i<=t; i++){
        for(int j=0; j<4; j++){
            int sx=b[i].x+dx[j];
            int sy=b[i].y+dy[j];
            if(sx>n || sx<1 || sy>m || sy<1) continue;
            if(g[sx][sy]>=b[i].v) continue;
            if(dis[sx][sy]<1+dis[b[i].x][b[i].y])
                dis[sx][sy]=1+dis[b[i].x][b[i].y];
        }
    }
    int maxn=0;
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
            maxn=max(maxn,dis[i][j]);
    cout<<maxn;
    return 0;
} 

好好看看,我用的贪心!


|