蓝桥杯/上机题目 打卡Day2

蓝桥杯/上机题目 打卡Day2

题目1 AcWing 3426. 糖果分享游戏
题目2 AcWing 3385. 玛雅人的秘密

1.AcWing 3426. 糖果分享游戏

思路: 本题的本质就是对于题目描述进行模拟 对于题目给出的证明只需要理解即可
同时注意代码如何处理 0 号 下标如何实现 传递给右边(通过取模运算) 注意对于细 的把握

#include<iostream>
#include<string>

using namespace std;
const int N = 110;
int q[N],w[N]; 

int main()
{
    int n; 
    
    while(cin >> n, n){
        
        int cnt = 0;
        for(int i = 0; i < n; i ++){
            cin >> q[i];
        }
        
      while(true)
       {
             bool is_same =true;
             
             for(int i = 1; i < n; i ++){
                if(q[i] !=  q[0]){
                    is_same = false;
                }
            }
            if(is_same) break;
            
            cnt ++;
            for(int i = 0; i < n; i ++){
                w[i] = (q[i] + q[(i + n - 1) % n]) / 2;
            }
            
            for(int i = 0; i < n; i ++){
                q[i] = w[i];
                if(q[i] % 2 == 1){
                    q[i] ++;
                }
            }  
      }
      
      cout << cnt << " " << q[0] << endl; 
        
  }
    
    return 0;
}

2.AcWing 3385. 玛雅人的秘密

思路 :这是一道宽搜的题目 注意对于宽搜性质的把握 宽搜性质保证 边权为1的情况下能够处理最短路径 问题 对于搜素问题应该理解处理它的核心思路 每一轮迭代我们加入改变原先状态的字符串 通过判断是否在哈希表中出现过来进行是否入队的一个操作

#include<iostream>
#include<string>
#include<queue>
#include<unordered_map>

using namespace std;


int n; 

int bfs(string temp){
   queue<string>q; 
   unordered_map<string,int>dist;
   
   q.push(temp);
   dist[temp] = 0;
   
   while(q.size()){
      auto t = q.front();
      q.pop();
     
      for(int i = 0; i < n; i ++){
          if(t.substr(i,4) == "2012"){
              return dist[t];
          }
      }
      
      for(int i = 0; i < n - 1 ; i ++){
          auto r = t;
          swap(r[i], r[i + 1]);
          if(!dist.count(r)){
              dist[r] = dist[t] + 1;
              q.push(r);
          }
      }
   }
   return -1;
}


int main()
{
    string temp;
    cin >> n >> temp;
    cout << bfs(temp) << endl; 
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值