洛谷P1825

题目链接

一个BFS,虽然洛谷难度标签是提高,但是感觉还行。
很熟悉的传送门,比较特殊的地方是需要剪枝(可能是因为我题目刷少了,还没遇到过BFS剪枝

AC代码:

/*
 * @Author: hesorchen
 * @Date: 2020-04-14 10:33:26
 * @LastEditTime: 2020-06-24 23:36:50
 * @Link: https://hesorchen.github.io/
 */
#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <cmath>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define endl '\n'
#define PI acos(-1)
#define PB push_back
#define ll long long
#define INF 0x3f3f3f3f
#define mod 3123456777
#define pll pair<ll, ll>
#define lowbit(abcd) (abcd & (-abcd))
#define max(a, b) ((a > b) ? (a) : (b))
#define min(a, b) ((a < b) ? (a) : (b))

#define IOS                      \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);
#define FRE                              \
    {                                    \
        freopen("in.txt", "r", stdin);   \
        freopen("out.txt", "w", stdout); \
    }

inline ll read()
{
    ll x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * f;
}
//==============================================================================

bool vis[310][310];
bool vis2[310][310];
char mp[310][310];
ll mov[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
struct node
{
    ll x, y, step;
};
queue<node> q;

ll sx, sy, ex, ey;
vector<pll> vec[30];
ll ans = INF;
ll n, m;

void bfs()
{
    while (q.size())
    {
        node temp = q.front();
        q.pop();
        if (temp.x == ex && temp.y == ey)
        {
            ans = min(ans, temp.step);
            continue;
        }
        if (temp.step > ans) //剪枝 没减tle四个点
            continue;
        for (int i = 0; i < 4; i++)
        {
            ll xx = temp.x + mov[i][0];
            ll yy = temp.y + mov[i][1];
            if (xx >= 1 && yy >= 1 && xx <= n && yy <= m && !vis[xx][yy] && mp[xx][yy] != '#' && vis2[xx][yy] <= 1)
            {
                if (mp[xx][yy] == '.' || mp[xx][yy] == '=')
                {
                    vis[xx][yy] = 1;
                    q.push(node{xx, yy, temp.step + 1});
                }
                else if (mp[xx][yy] <= 'Z' && mp[xx][yy] >= 'A')
                {
                    ll x1 = vec[mp[xx][yy] - 'A'][0].first;
                    ll y1 = vec[mp[xx][yy] - 'A'][0].second;
                    ll x2 = vec[mp[xx][yy] - 'A'][1].first;
                    ll y2 = vec[mp[xx][yy] - 'A'][1].second;

                    vis2[x1][y1]++;
                    vis2[x2][y2]++;
                    if (x1 == xx && y1 == yy)
                        q.push(node{x2, y2, temp.step + 1});
                    else if (x2 == xx && y2 == yy)
                        q.push(node{x1, y1, temp.step + 1});
                }
            }
        }
    }
}

int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> mp[i] + 1;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
        {
            if (mp[i][j] == '=')
                ex = i, ey = j;
            if (mp[i][j] == '@')
                sx = i, sy = j;
            if (mp[i][j] <= 'Z' && mp[i][j] >= 'A')
                vec[mp[i][j] - 'A'].PB(make_pair(i, j));
        }
    vis[sx][sy] = 1;
    q.push(node{sx, sy, 0});
    bfs();
    cout << (ans == INF ? -1 : ans) << endl;
    return 0;
}
03-21
### 关于动态规划 (Dynamic Programming, DP) 的解决方案 在解决洛谷平台上的编程问题时,尤其是涉及动态规划的题目,可以采用以下方法来构建解决方案: #### 动态规划的核心思想 动态规划是一种通过把原问题分解为相对简单的子问题的方式来求解复杂问题的方法。其核心在于存储重复计算的结果以减少冗余运算。通常情况下,动态规划适用于具有重叠子问题和最优子结构性质的问题。 对于动态规划问题,常见的思路包括定义状态、转移方程以及边界条件的设计[^1]。 --- #### 题目分析与实现案例 ##### **P1421 小玉买文具** 此题是一个典型的简单模拟问题,可以通过循环结构轻松完成。以下是该问题的一个可能实现方式: ```cpp #include <iostream> using namespace std; int main() { int n; cin >> n; // 输入购买数量n double p, m, c; cin >> p >> m >> c; // 输入单价p,总金额m,优惠券c // 计算总价并判断是否满足条件 if ((double)n * p <= m && (double)(n - 1) * p >= c) { cout << "Yes"; } else { cout << "No"; } return 0; } ``` 上述代码实现了基本逻辑:先读取输入数据,再根据给定约束条件进行验证,并输出最终结果[^2]。 --- ##### **UOJ104 序列分割** 这是一道经典的区间动态规划问题。我们需要设计一个二维数组 `f[i][j]` 表示前 i 次操作后得到的最大价值,其中 j 是最后一次切割的位置。具体实现如下所示: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 5e3 + 5; long long f[MAXN], sumv[MAXN]; int a[MAXN]; int main(){ ios::sync_with_stdio(false); cin.tie(0); int n,k; cin>>n>>k; for(int i=1;i<=n;i++)cin>>a[i]; for(int i=1;i<=n;i++)sumv[i]=sumv[i-1]+a[i]; memset(f,-0x3f,sizeof(f)); f[0]=0; for(int t=1;t<=k;t++){ vector<long long> g(n+1,LLONG_MIN); for(int l=t;l<=n;l++)g[l]=max(g[l-1],f[t-1][l-1]); for(int r=t;r<=n;r++)f[r]=max(f[r],g[r]+sumv[r]*t); } cout<<f[n]<<'\n'; return 0; } ``` 这段程序利用了滚动数组优化空间复杂度,同时保持时间效率不变[^3]。 --- ##### **其他常见问题** 针对更复杂的路径覆盖类问题(如 PXXXX),我们往往需要结合一维或多维动态规划模型加以处理。例如,在某些场景下,我们可以设定 dp 数组记录到达某一点所需最小代价或者最大收益等指标[^4]。 --- ### 总结 以上展示了如何运用动态规划技巧去应对不同类型的算法挑战。无论是基础还是高级应用场合,合理选取合适的数据结构配合清晰的状态转换关系都是成功解决问题的关键所在。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hesorchen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值