大三寒假学习进度笔记21

今天看到了一道蓝桥杯的题目,其中使用到了dfs算法,在之前的数据结构中学习过这种算法,但是并没有在代码中使用过,因此根据给出的思路在写了一遍这个题目。
复制代码
#include<bits/stdc++.h>
using namespace std;

int a[100],ans=0;
bool vis[20240000];
bool check(int date){
    if(vis[date]){
        return false;
    }
    vis[date]=1;
    int mm=date/100%100;
    int dd=date%100;
    if(mm<1||mm>12){
        return false;
    } 
    if(mm==1||mm==3||mm==5||mm==7||mm==8||mm==10||mm==12){
        if(dd>=1&&dd<=31){
            return true;
        }
    }else if(mm==2){
        if(dd>=1&&dd<=28){
            return true;
        }
    }else if(1<=dd&&dd<=30){
        return true;
    }else{
        return false;
    }
}

void dfs(int x,int pos,int date){
    if(x==100) return;
    if(pos == 8){
        if(check(date)) ++ans;
        return;
    }
    if(
    (pos==0 && a[x]==2) ||
    (pos==1 && a[x]==0) ||
    (pos==2 && a[x]==2) ||
    (pos==3 && a[x]==3) ||
    (pos==4 && 0<=a[x] && a[x]<=1) ||
    (pos==5 && 0<=a[x] && a[x]<=9) ||
    (pos==6 && 0<=a[x] && a[x]<=3) ||
    (pos==7 && 0<=a[x] && a[x]<=9) 
    ){
        dfs(x+1,pos+1,date*10+a[x]);
    }
    dfs(x+1,pos,date);
}

int main(){
    ios::sync_with_stdio(0);cin.tie(0);
    for(int i=0;i<100;i++){
        cin>>a[i];
    }
    dfs(0,0,0);
    cout<<ans;
    return 0;
} 
复制代码

其中还使用到了这个函数

std::printf
std::cout
cin
cout
posted @   wrf12  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示