uva 10285 滑雪,记忆化搜索

题意:

就是poj那道滑雪,可以dfs可以dp记忆化搜索。

给一张图,问一个人从一个点滑到底最多可以滑多远。


解析:

记忆化搜索。


代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define LL long long

using namespace std;
const int maxn = 100 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = 4 * atan(1.0);
const double ee = exp(1.0);

int dir[][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int g[maxn][maxn];
int dp[maxn][maxn];
int r, c;

int dfs(int x, int y)
{
    int& res = dp[x][y];
    if (0 < res)
        return res;
    res = 1;
    for (int i = 0; i < 4; i++)
    {
        int nx = x + dir[i][0];
        int ny = y + dir[i][1];
        if (0 <= nx && nx < r && 0 <= ny && ny < c)
        {
            if (g[nx][ny] < g[x][y])
            {
                res = max(res, dfs(nx, ny) + 1);
            }
        }
    }
    return res;
}

int main()
{
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
    #endif // LOCAL
    int ncase;
    scanf("%d", &ncase);
    while (ncase--)
    {
        memset(g, 0, sizeof(g));
        memset(dp, 0, sizeof(dp));
        string name;
        cin >> name;
        scanf("%d%d", &r, &c);
        for (int i = 0; i < r; i++)
        {
            for (int j = 0; j < c; j++)
            {
                scanf("%d", &g[i][j]);
            }
        }
        int ans = 0;
        for (int i = 0; i < r; i++)
        {
            for (int j = 0; j < c; j++)
            {
                ans = max(ans, dfs(i, j));
            }
        }
        cout << name << ": " << ans << endl;
    }
    return 0;
}


你可能感兴趣的:(动态规划)