Blue Bridge 31 days | 4 questions today Day14 | C++

1. With scores

insert image description here
dfs

#include <iostream>
#include <vector>
using namespace std;
long long n,ans;
const int N=10;
bool st[N];
vector<int>num;
int calc(int l,int r){
    
    
  int sum=0;
  for(int i=l;i<r;i++){
    
    
     sum*=10;
     sum+=num[i];

  }

  return sum;
}
void check(vector<int>num){
    
    
    for(int i=1;i<=7;i++){
    
    
       for(int j=i+1;j<=8;j++){
    
    
           int a=calc(0,i);
            //printf("a=%d\n",a);
           if(a>n)continue;
           int b=calc(i,j);
           //printf("b=%d\n",b);
           int c=calc(j,9);
           //printf("c=%d\n",c);
           if(a*c+b==n*c)ans++;
       }
    }
    return;
}
void dfs(int level){
    
    
   if(level==9){
    
    
    //puts("!");
    //for(int i=0;i<9;i++){
    
    
    //    cout<<num[i];
    //}
   //puts("!");
     check(num);
     return;
   }
   for(int i=1;i<=9;i++){
    
    
      if(!st[i]){
    
    
        num.push_back(i);
        st[i]=true;
        dfs(level+1);
        st[i]=false;
        num.pop_back();
      }
   }
}
int main()
{
    
    
  scanf("%d",&n);
  dfs(0);
  printf("%lld",ans);
  return 0;
}

2. Walk the maze

insert image description here
walk the maze
bfs

#include <iostream>
#include <queue>
using namespace std;
typedef pair<int,int>PII;
#define x first
#define y second
const int N=110;
int n,m;
int inx,iny,outx,outy;
int mp[N][N];
int dis[N][N];
int dx[4]={
    
    0,1,0,-1},dy[4]={
    
    1,0,-1,0};
queue<PII>q;
int bfs(int x,int y){
    
    
   dis[x][y]=0;
   q.push({
    
    x,y});
   while(!q.empty()){
    
    
     PII t=q.front();
     q.pop();
     int x=t.x,y=t.y;
     for(int i=0;i<4;i++){
    
    
       int sx=x+dx[i],sy=y+dy[i];
       if(mp[sx][sy]&&!dis[sx][sy]&&sx>=0&&sx<n&&sy>=0&&sy<m){
    
    
         dis[sx][sy]=dis[x][y]+1;
         if(sx==outx-1&&sy==outy-1)return dis[sx][sy];
         q.push({
    
    sx,sy});
       }
     }

   }
   return -1;
}
int main()
{
    
    
  scanf("%d%d",&n,&m);
  for(int i=0;i<n;i++){
    
    
    for(int j=0;j<m;j++){
    
    
      scanf("%d",&mp[i][j]);
    }
  }

  scanf("%d%d%d%d",&inx,&iny,&outx,&outy);

  printf("%d",bfs(inx-1,iny-1));
  return 0;
}

3. Blue Bridge Kindergarten

insert image description here
and check

#include <iostream>
using namespace std;
const int N=2e5+10;
int p[N];
int find(int x){
    
    
  if(p[x]!=x)return p[x]=find(p[x]);
  return p[x];
}
int main()
{
    
    
  int n,m;
  scanf("%d%d",&n,&m);
  //初始化
  for(int i=1;i<=n;i++){
    
    
    p[i]=i;
  }
  for(int i=0;i<m;i++){
    
    
    int op;
    scanf("%d",&op);

    int a, b;
    scanf("%d%d",&a,&b);

    if(op==1){
    
    
       p[find(a)]=find(b);
    }else if(op==2){
    
    
       bool is=(find(a)==find(b));
       if(is){
    
    
         puts("YES");
       }else{
    
    
         puts("NO");
       }
    }
  }
  return 0;
}

4. Jumping rocks

insert image description here
insert image description here
The shortest and longest, longest and shortest, largest and smallest, and smallest and largest problems are generally solved by two points.
If you haven’t done similar questions, the idea is not easy
to think. Here is to perform a binary
check function on the shortest distance to check whether the number of stones removed is less than or equal to m when the shortest distance is mid, and find the boundary value that satisfies the property (that is, remove the largest stone less than or equal to m). mid value) is whether the template selected by ans
is judged according to whether r=mid or r=mid after the update

#include <iostream>
using namespace std;
const int N=5e4+10;
int stone[N];
int l,n,m;
bool check(int mid){
    
    
   int now=0;//上一块石头
   int sum=0;//移除石头总数
   //起点stone[0]=0,默认移走右边的石头
   for(int i=1;i<=n;i++){
    
    
     if(stone[i]-stone[now]<mid){
    
    sum++;
     //printf("stone%d\n",i);
     }
     else now=i;
   }
  // printf("mid=%d\n",mid);
   //printf("sum=%d\n",sum);
   return sum<=m;
}
int search(int l,int r){
    
    
  while(l<r){
    
    
    int mid=l+r+1>>1;
    if(check(mid))l=mid;
    else r=mid-1;
  }
  return r;
}
int main()
{
    
    
  scanf("%d%d%d",&l,&n,&m);
  //中间的石头
  for(int i=1;i<=n;i++){
    
    
    scanf("%d",&stone[i]);
  }
  printf("%d",search(0,l));
  return 0;
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324104692&siteId=291194637