蓝桥杯ALGO-1003 礼物 ALGO1001 跳马

ALGO1001 礼物 

采用的是二分的思想

我感觉用二分的时候就是所找的值在一个区间里面我们就可以采用

二分查找的模板是:

整数二分最后退出的条件是 l = r,所以最终的 l 和 r 是相等的,输出哪个都行。 

	while (l < r)
    {
        int mid = l + r >> 1;	//(l+r)/2
        if (check(mid))  r = mid;    // check()判断mid是否满足性质
        else l = mid + 1;
    }
//再往左找目标
//当目标值存在多个时候找的是最左侧的目标值
	while (l < r)
    {
        int mid = l + r + 1 >> 1;	//(l+r+1)/2
        if (check(mid))  l = mid;
        else r = mid - 1;
    }
//在往右找目标
//当目标值有多个时候找到的是最右侧的目标值

像这道题而言答案就在 1到N之间暴力肯定超时

我们用二分来找这个最大值我们要考虑数据的大小来采用不同的数据类型(这个还是很重要的)

 

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int M = 1e6+5;
int N;long long S;
long long val[M], a[M]; //前缀和,石子每个的质量
bool check(int mid)
{
    for (int i = mid; i <= N-mid; i++)
    {
        if (val[i] - val[i - mid] <= S && val[i + mid] - val[i] <= S)
        {
            return true; //存在符合情况的
        }
    }
    return false;
}
int main()
{
    cin >> N >> S;
    for (int i = 1; i <= N; i++)
    {
        cin >> a[i];
        val[i] = val[i - 1] + a[i]; //求前缀和
    }
    //找符合要求的最大值采用二分法
    int l = 1, r = N;
    while (l < r)
    {
        int mid = l + r + 1 >> 1; //(l+r)/2
        if (check(mid))
        {
            l = mid;
        }
        else
            r = mid - 1;
    }
    cout << 2 * l << endl;
}

ALGO1001跳马

这个采用的是BFS

#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
//采用BFS解决走马问题
int vir[8][2] = {{1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}, {-2, 1}, {-1, 2}}; // 8个方向
bool vis[10][10];                                                                         //判断型数组
int a, b, c, d;
typedef struct
{
    int x, y, temp = 0; //横坐标纵坐标以及步数
} list;
int BFS()
{
    vis[a][b] = true;
    queue<list> Q; //用结构体队列
    list l1;
    l1.x = a;
    l1.y = b;
    Q.push(l1);
    while (!Q.empty())
    {
        list l2 = Q.front();
        Q.pop(); //出队列
        for (int i = 0; i < 8; i++)
        {
            int x = l2.x + vir[i][0];
            int y = l2.y + vir[i][1];
            if (1 <= x && x <= 8 && 1 <= y && y <= 8)
            {
                if (x == c && y == d)
                {
                    cout << ++l2.temp;
                    return 1;
                }
                else if (!vis[x][y])
                {
                    list l3;
                    l3.x = x;
                    l3.y = y;
                    l3.temp = l2.temp + 1;
                    Q.push(l3);
                    vis[x][y]=true;
                }
            }
        }
    }
    return -1;
}
int main()
{
    cin >> a >> b >> c >> d;
    if(a==c&&b==d){
        cout<<0<<endl;
    }
    else{
        int B=BFS();
    if(B==-1){
        cout<<-1;
    }
    }
    

}

  • 22
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小李小于

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

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

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

打赏作者

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

抵扣说明:

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

余额充值