PAT T1022 Werewolf

暴力搜索加剪枝~

#include<bits/stdc++.h>
using namespace std;
const int maxn=1014;
int a[maxn];
bool visit[maxn];
vector<int> path,tmp;
int N,M,L,cnt=0;
void dfs (int v) {
    if (tmp.size()==M) {
        int liecnt=0,liewolf=0;
        for (int i=1;i<=N;i++) {
            if (a[i]<0&&visit[abs(a[i])]==false) {
                liecnt++;
                if (visit[i]==true) liewolf++;
            }
            else if (a[i]>0&&visit[abs(a[i])]==true) {
                liecnt++;
                if (visit[i]==true)  liewolf++;
            }
        }
        if (liecnt==L&&liewolf>=1&&liewolf<M&&cnt==0) path=tmp,cnt++;
        return;
    }
    if (tmp.size()+v<L) return;
    v--;
    while (v>=1) {
        visit[v]=true;
        tmp.push_back(v);
        dfs (v);
        visit[v]=false;
        tmp.pop_back();
        v--; 
    }
}
int main () {
    scanf ("%d %d %d",&N,&M,&L);
    for (int i=1;i<=N;i++) {
        scanf ("%d",&a[i]);
    } 
    dfs (N+1);
    if (path.size()==0) {
        printf ("No Solution");
        return 0;
    }
    for (int i=0;i<path.size();i++) {
        if (i!=0) printf (" ");
        printf ("%d",path[i]);
    }
    return 0;
}

 

posted @ 2020-02-13 12:04  zlc0405  阅读(230)  评论(0编辑  收藏  举报