Emm BFS开了O2优化居然过了。不知道编译器施了什么魔法

P1434 [SHOI2002] 滑雪

KaenbyouRin @ 2019-07-12 16:11:27

#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;

int dp[105][105], r, c, ans = -1, dx[4] = { 1, -1, 0, 0 }, dy[4] = { 0, 0, 1, -1 };
bool v[105][105];
long long g[105][105];

inline bool legal(const int x, const int y) {
    return x >= 0 && x < r && y >= 0 && y < c;
}

struct St {
    int x, y;
};

int main() {
    ios::sync_with_stdio(false);
    cin >> r >> c;
    for (int i = 0; i < r; ++i) {
        for (int j = 0; j < c; ++j) {
            dp[i][j] = 1;
        }
    }
    for (int i = 0; i < r; ++i)
        for (int j = 0; j < c; ++j) {
            cin >> g[i][j];
        }

    queue<St> q;
    for (int i = 0; i < r; ++i) {
        for (int j = 0; j < c; ++j) {
            q.push({ i, j });
        }
    }
    while (!q.empty()) {
        St f = q.front();
        q.pop();
        ans = max(ans, dp[f.x][f.y]);
        v[f.x][f.y] = false;
        for (int i = 0; i < 4; ++i) {
            int x = f.x + dx[i], y = f.y + dy[i];
            if (legal(x, y) && g[x][y] < g[f.x][f.y] && dp[x][y] < dp[f.x][f.y] + 1) {
                dp[x][y] = dp[f.x][f.y] + 1;
                if (!v[x][y]) {
                    v[x][y] = true;
                    q.push({ x, y });
                }
            }
        }
    }
    cout << ans;
}

by KaenbyouRin @ 2019-07-12 16:12:53

原本卡第二个点的。


by 幻之陨梦 @ 2019-07-12 16:20:52

@KaenbyouRin 这题应该用深搜剪枝吧。


by 幻之陨梦 @ 2019-07-12 16:21:27

并附上我的AC代码

#include<bits/stdc++.h>
using namespace std;
int r,c;
int a[105][105],l[105][105];
int dx[]={-1,0,1,0},dy[]={0,1,0,-1};
int dfs(int x,int y)
{
    int nx,ny;
    if(l[x][y]>0) return l[x][y];
    int ans=1;
    for(int i=0;i<4;i++)
    {
        nx=x+dx[i],ny=y+dy[i];
        if(nx>=1&&nx<=r&&ny>=1&&ny<=r&&a[x][y]>a[nx][ny]) ans=max(dfs(nx,ny)+1,ans);
    }
    l[x][y]=ans;
    return ans;
}
int main(void)
{
    int i,j,maxn=-1;
    scanf("%d%d",&r,&c);
    for(i=1;i<=r;i++) for(j=1;j<=c;j++) scanf("%d",&a[i][j]);
    for(i=1;i<=r;i++) for(j=1;j<=c;j++) maxn=max(maxn,dfs(i,j));
    printf("%d",maxn);
}

by liukairui @ 2019-08-13 14:46:21

同BFS


by liukairui @ 2019-08-13 14:48:35

老哥解决了吗?


by LegendN @ 2020-03-13 19:37:00

我觉得是这一步剪枝了一小部分

判断条件里面

dp[x][y] < dp[f.x][f.y] + 1


|