搜索例题 2019/7/3

例题1

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,x,y,map[16][16],cnt;
char ch[10];
bool vis[16][16];
void dfs(int x,int y){
    cnt++;
    vis[x][y]=1;
    if((map[x+1][y]==map[x][y])&&!vis[x+1][y])dfs(x+1,y);
    if((map[x][y+1]==map[x][y])&&!vis[x][y+1])dfs(x,y+1);
    if((map[x-1][y]==map[x][y])&&!vis[x-1][y])dfs(x-1,y);
    if((map[x][y-1]==map[x][y])&&!vis[x][y-1])dfs(x,y-1);
}
int main(){
     scanf("%d%d%d%d",&n,&m,&x,&y);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            scanf("%s",ch);
            if(ch[0]=='A')map[i][j]=1;
            if(ch[0]=='B')map[i][j]=2;
            if(ch[0]=='C')map[i][j]=3;
            if(ch[0]=='D')map[i][j]=4;
        }
    }
    if(!map[x][y]){
        puts("0");
        return 0;
    }
    dfs(x,y);
    printf("%d\n",cnt);
    return 0;
}

例题2

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,k,ans,cnt[2],a[2][100010];
bool vis[100010];
int main(){
    scanf("%d%d",&n,&k);
    if(n==k){
        puts("0");
        return 0;
    }
    vis[n]=1;
    a[0][++cnt[0]]=n;
    bool f=0;
    while(1){
        ans++;
        f=!f;
        for(int i=1;i<=cnt[!f];i++){
            int cur;
            cur=a[!f][i]+1;
            if(cur<=100000&&cur>=0){
                if(!vis[cur]){
                    if(cur==k){
                        printf("%d\n",ans);
                        return 0;
                    }
                    else {
                        vis[cur]=1;
                        a[f][++cnt[f]]=cur;
                    }
                }
            }
            cur=a[!f][i]-1;
            if(cur<=100000&&cur>=0){
                if(!vis[cur]){
                    if(cur==k){
                        printf("%d\n",ans);
                        return 0;
                    }
                    else {
                        vis[cur]=1;
                        a[f][++cnt[f]]=cur;
                    }
                }
            }
            cur=a[!f][i]*2;
            if(cur<=100000&&cur>=0){
                if(!vis[cur]){
                    if(cur==k){
                        printf("%d\n",ans);
                        return 0;
                    }
                    else {
                        vis[cur]=1;
                        a[f][++cnt[f]]=cur;
                    }
                }
            }
        }
        cnt[!f]=0;
    }
    return 0;
}

例题4

#include<cstdio>
#include<iostream>
#include<map>
#define M 45
#define ll long long
using namespace std;
ll a[M],s1[M],s2[M],n,key,ans;
map<ll,int> hash;
void dfs(int x,int sum,int flag) {
    if(!flag&&x>n/2) {
        hash[sum]++;
        return;
    }
    if(flag&&x>n) {
        ans+=hash[key-sum];
        return;
    }
    dfs(x+1,sum+a[x],flag);
    dfs(x+1,sum,flag);
}
int main() {
    cin>>n>>key;
    for(int i=1; i<=n; i++)
        cin>>a[i];
    dfs(1,0,0);
    dfs(n/2+1,0,1);
    cout<<ans;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/thmyl/p/11124729.html