BZOJ 1306 DFS

思路:
搜索就好

(注意不要枚举太多东西)

这里写图片描述

//By SiriusRen
#include <cstdio>
using namespace std;
int n,point[10],ans[10][10],answer;
bool check(){
    for(int i=1;i<=n;i++)
        if(point[i])return 0;
    return 1;
}
void dfs(int x,int y){
    if(x>n||y>n){
        if(check())answer++;
        return;
    }
    if(point[x]>(n-y+1)*3)return;
    if(y==n){
        if(point[x]==1){
            point[x]--,point[y]--;
            dfs(x+1,x+2);
            point[x]++,point[y]++;
        }
        else if(point[x]==3){
            point[x]-=3;
            dfs(x+1,x+2);
            point[x]+=3;
        }
        else if(!point[x]){
            point[y]-=3;
            dfs(x+1,x+2);
            point[y]+=3;
        }
    }
    else
    {
        if(point[x]-1>=0&&point[y]-1>=0){
            point[x]-=1,point[y]-=1;
            dfs(x,y+1);
            point[x]+=1,point[y]+=1;
        }
        if(point[x]-3>=0&&point[y]>=0){
            point[x]-=3;
            dfs(x,y+1);
            point[x]+=3;
        }
        if(point[y]-3>=0&&point[x]>=0){
            point[y]-=3;
            dfs(x,y+1);
            point[y]+=3;
        }
    }
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&point[i]);
    dfs(1,2);
    printf("%d\n",answer);
}

这里写图片描述

posted @ 2016-11-09 19:46  SiriusRen  阅读(218)  评论(0编辑  收藏  举报