跳到正文

个人记录

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

liuyilun123约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年5月3日
收录
2026年5月3日
更新
2026年11月14日
篇幅
约 7 分钟

版本

  1. v2当前
  2. v1

P10376 [GESP202403 六级] 游戏

错误原因:

没思路骗分 。

思路:

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

代码:

C++
#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 ,然后定义一个二维数组 bi,jb_i,_j 表示从 i,ji,j 出发能滑的最长路程 。然后每次判断如果来过就 return ,否则就先标记为 ii , 让后循环四个方位 ,如果满足要求就 dfs ,接着就让现在的 bb 和 这个方位的 b+1b + 1 取最大值 。

代码:

C++
#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】天下第一

错误原因:

没思路 。

思路:

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

代码:

C++
#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 , 用 xx 表示现在的苹果数 ,cc 表示切换的次数 ,ff 表示现在的位置 。接着判断如果 xx 大于 tt 就返回 00 ,否则判断如果 bx,c,fb_x,_c,_f 标记过 就返回 bx,c,fb_x,_c,_f 。然后定义一个变量 oo 等于 dfs(x + 1, c, f) , 判断如果 cc 小于 ww , 就让 oo 和 dfs(x + 1, c + 1,3 - f) 取最大值 。最后判断如果 f=axf = a_x 那么则 oo 加 11 。让 bx,c,fb_x,_c,_f 等于 oo 后 返回 bx,c,fb_x,_c,_f 。

代码:

C++
#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;
}
原文