4.24周五下午记忆化搜索--东塘--蒋

· · 个人记录

P10376 [GESP202403 六级] 游戏

错误原因:

没思路骗分 。

思路:

我们可以用 dfs。然后每次判断如果 x (也就是当时的 n) ,小于 c 就返回 1 ,再定义一个数组 g 就是判断有没有重复的 ,如果已经标记过了 ,就直接返回 g[x] ,否则就先让 g[x] 等于 dfs(x - a) + dfs(x - b) ,再返回 。

代码:

#include<bits/stdc++.h>

using namespace std;

int n, a, b, c, g[200005], mod = 1e9 + 7;

int dfs(int x)
{
    if(x <= c)
    {
        return 1;
    }

    if(g[x] != 0)
    {
        return g[x];
    }

    g[x] = (dfs(x - a) + dfs(x - b)) % mod;

    return g[x];
}

int main()
{
    cin >> n >> a >> b >> c;

    cout << dfs(n);
    return 0;
}   

P1434 [SHOI2002] 滑雪

错误原因;

超时了 。

思路:

循环每个数都用一次 dfs ,然后定义一个二维数组 b_i,_j 表示从 i,j 出发能滑的最长路程 。然后每次判断如果来过就 return ,否则就先标记为 i , 让后循环四个方位 ,如果满足要求就 dfs ,接着就让现在的 b 和 这个方位的 b + 1 取最大值 。

代码:

#include<bits/stdc++.h>

using namespace std;

int n, m, a[105][105], ans, b[105][105];
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};

void dfs(int x, int y)
{
    if(b[x][y] >= 1)
    {
        return ;
    } 
    b[x][y] = 1;

    for(int i = 0; i < 4; i++)
    {
        int nx = dx[i] + x, ny = dy[i] + y;

        if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && a[nx][ny] < a[x][y])
        {
            dfs(nx, ny);
            b[x][y] = max(b[nx][ny] + 1, b[x][y]);
        }
    }

    return ;
}

int main()
{
    cin >> n >> m;

    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            cin >> a[i][j];
        }
    }

    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            dfs(i, j);
        }
    }

    int ans = 0;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            ans = max(ans, b[i][j]);
        }
    }

    cout << ans;
    return 0;
}       

P5635 【CSGRound1】天下第一

错误原因:

没思路 。

思路:

我们可以定义一个标记数组 v , 然后 while 循环直到 x=0 或者 y=0 或者 v_x,_y 标记过 。接着循环里按题目给的规律执行 。最后也按题目要求输出 。

代码:

#include<bits/stdc++.h>

using namespace std;

int t, p;
map<int, map<int, bool> > v;

int main() 
{
    cin >> t >> p;

    while(t--) 
    {
        int x, y;
        cin >> x >> y;

        v.clear();
        bool o = 1;   

        while(!(x == 0 || y == 0 || v[x][y])) 
        {
            v[x][y] = 1;

            if(o == 1) 
            {
                x = (x + y) % p;
            } 
            else 
            {
                y = (x + y) % p;
            }

            o = !o;
        }

        if(x == 0) 
        {
            cout << "1\n";
        } 
        else if (y == 0) 
        {
            cout << "2\n";
        } 
        else 
        {
            cout << "error\n";
        }
    }
    return 0;
}

P2690 [USACO04NOV] Apple Catching G

错误原因:

超时。

思路:

我们可以 dfs , 用 x 表示现在的苹果数 ,c 表示切换的次数 ,f 表示现在的位置 。接着判断如果 x 大于 t 就返回 0 ,否则判断如果 b_x,_c,_f 标记过 就返回 b_x,_c,_f 。然后定义一个变量 o 等于 dfs(x + 1, c, f) , 判断如果 c 小于 w , 就让 o 和 dfs(x + 1, c + 1,3 - f) 取最大值 。最后判断如果 f = a_x 那么则 o 加 1 。让 b_x,_c,_f 等于 o 后 返回 b_x,_c,_f 。

代码:

#include<bits/stdc++.h>

using namespace std;

int t, w, a[1005], b[1005][35][3];  

int dfs(int x, int c, int f)  
{
    if(x > t)  
    {
        return 0;
    }

    if(b[x][c][f] != -1)  
    {
        return b[x][c][f];
    }

    int o = dfs(x + 1, c, f);

    if(c < w)
    {
        o = max(o, dfs(x + 1, c + 1, 3 - f));  
    }

    if(f == a[x])
    {
        o++;
    }

    return b[x][c][f] = o;
}

int main()
{
    cin >> t >> w;

    for(int i = 1; i <= t; i++)
    {
        cin >> a[i];
    }

    memset(b, -1, sizeof b);

    cout << dfs(1, 0, 1); 
    return 0;
}