G.数三角形(easy)(2024牛客寒假训练营4)

文章描述了一个C++程序,用于计算给定二维数组中每个*所在位置形成等腰三角形的可能性。程序通过预存连续*的数量,避免了超时问题,主要涉及数组遍历和条件判断。
摘要由CSDN通过智能技术生成

思路解析:可以枚举顶点然后判等腰三角形

需要预存一下连续的‘*’有多少,以备判等腰三角形的时候用(不然会超时哦)

拿样例来说就像这样:

其中的非0数字就是其以及其左边连续的‘*’的个数

废话不多说,代码如下:
 

#include<bits/stdc++.h>
using namespace std;
#define int long long
using T = pair<int, int>;
//set<int>S;
//unordered_map<int, int>mp;
const int N = 2e5 + 10;
char a[501][501];
int left_num[501][501];
int n, m;
int ans(int x, int y) {
    int i, cnt{ 2 }, j;
    int res{};
    for (i = x + 1; i <= n; ++i) {
        if (y - cnt + 1 < 1 || y + cnt - 1 > m || a[i][y - cnt + 1] == '.' || a[i][y + cnt - 1] == '.')return res;
        if (left_num[i][y + cnt - 1]>=2*cnt-1)++res;
        ++cnt;
    }
    return res;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> m;
    int i, j,k;
    for (i = 1; i <= n; ++i) {
        for (j = 1; j <= m; ++j) {
            cin >> a[i][j];
            if (a[i][j] == '*'){
                left_num[i][j] = left_num[i][j - 1]+1;
            }
        }
    }
    int res{};
    for (i = 1; i <= n; ++i) {
        for (j = 1; j <= m; ++j) {
            if (a[i][j] == '*')
                res += ans(i, j);
            //cout << left_num[i][j] << " ";
        }//cout << endl;
    }
    cout << res;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值