洛谷 P4147 玉蟾宫 题解

作者:岸芷汀兰

一、题目:

题目链接

二、思路:

单调栈裸题。
预处理:s[i][j]表示(i,j)向上有几个连续的”F”。
需要用单调栈统计两个数组:l[i][j]表示s[i][j]的左边第一个比s[i][j]小的位置,r[i][j]表示s[i][j]的右边第一个比s[i][j]小的位置。
那么此时矩形的面积就是(r[i][j]-1-l[i][j])*s[i][j]。
具体细节看代码。
写得太丑。

三、代码:

#include<iostream>
#include<cstdio>
#include<stack>
#include<algorithm>

using namespace std;
inline int read(void) {
    int 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 * 10 + ch - '0';
        ch = getchar();
    }
    return f * x;
}

const int maxn = 1005;

int n, m, map[maxn][maxn], s[maxn][maxn], l[maxn][maxn], r[maxn][maxn], ans;

stack<int>monotonous;

inline void clear(void) {
    while (monotonous.size())monotonous.pop(); return;
}

int main()
{
    n = read(); m = read();
    for (register int i = 1; i <= n; i++) {
        for (register int j = 1; j <= m; j++) {
            char ch; cin >> ch;
            map[i][j] = (ch == 'F' ? 1 : 0);
        }
    }
    for (register int i = 1; i <= n; i++) {
        for (register int j = 1; j <= m; j++) {
            if (map[i][j]) {
                s[i][j] = s[i - 1][j] + 1;
            }
        }
    }
    for (register int i = 1; i <= n; i++) {
        clear();
        for (register int j = 1; j <= m; j++) {
            if (monotonous.empty()) {
                monotonous.push(j); l[i][j] = 0;
            }
            else {
                while (monotonous.size() && s[i][j] <= s[i][monotonous.top()])monotonous.pop();
                if (monotonous.size())l[i][j] = monotonous.top();
                else l[i][j] = 0;
                monotonous.push(j);
            }
        }
        clear();
        for (register int j = m; j >= 1; j--) {
            if (monotonous.empty()) {
                monotonous.push(j); r[i][j] = m + 1;
            }
            else {
                while (monotonous.size() && s[i][j] <= s[i][monotonous.top()])monotonous.pop();
                if (monotonous.size())r[i][j] = monotonous.top();
                else r[i][j] = m + 1;
                monotonous.push(j);
            }
        }
    }
    for (register int i = 1; i <= n; i++) {
        for (register int j = 1; j <= m; j++) {
            if (!s[i][j])continue;
            int area = ((r[i][j] - 1) - l[i][j])*s[i][j];
            ans = max(ans, area);
        }
    }
    printf("%d\n", ans * 3);
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
过河卒是一个典型的动态规划问题。首先,我们将整个棋盘看作一个二维数组,数组的每个元素表示到达该位置的路径数目。然后,我们根据题目给出的条件,逐步更新数组中的元素,直到计算出到达目标位置的路径数目。 具体的解题思路如下: 1. 首先,我们可以将马的位置设置为0,表示无法经过该位置。 2. 然后,我们根据马的位置,更新数组中的元素。对于二维数组中的每个位置,我们根据左边和上边的位置来计算到达当前位置的路径数目。具体地,如果左边和上边的位置都可以经过,那么到达当前位置的路径数目就等于左边和上边位置的路径数目之和。如果左边或上边的位置无法经过,那么到达当前位置的路径数目就等于左边或上边位置的路径数目。 3. 最后,我们输出目标位置的路径数目。 下面是洛谷p1002过河卒题解的C++代码: ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long a[21][21]; int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; // 初始化数组,马的位置设置为0 for(int i=0; i<=20; i++) { for(int k=0; k<=20; k++) { a[i][k] = 1; } } a[x2][y2] = 0; // 根据马的位置更新数组中的元素 if(x2 >= 2 && y2 >= 1) a[x2-2][y2-1] = 0; if(x2 >= 1 && y2 >= 2) a[x2-1][y2-2] = 0; if(x2 <= 18 && y2 >= 1) a[x2+2][y2-1] = 0; if(x2 <= 19 && y2 >= 2) a[x2+1][y2-2] = 0; if(x2 >= 2) a[x2-2][y2+1] = 0; if(x2 >= 1) a[x2-1][y2+2] = 0; if(y2 >= 1) a[x2+2][y2-1] = 0; if(y2 >= 2) a[x2+1][y2-2] = 0; // 动态规划计算路径数目 for(int i=1; i<=20; i++) { for(int k=1; k<=20; k++) { if(a[i][k] != 0) { a[i][k] = a[i-1][k] + a[i][k-1]; } } } // 输出目标位置的路径数目 cout << a[x1][y1] << endl; return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值