USACO section 1.3 Combination Lock

这个程序也主要分为三个部分,一个是全排列函数,两个测试函数。
全排列用来模拟密码,第一个测试函数是用来剪枝的,当你模拟出来的当前密码数字不符合要求时后续操作就完全没有必要进行,直接减掉。还有一个就是测试结果是佛符合条件。

/*
ID: 13913351
LANG: C++
TASK:combo
*/
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
int N;
int a[3],b[3];
int res[3];
int sum=0;
bool test1(int step,int i)
{
    if(abs(i-a[step])<=2||abs(i-b[step])<=2||abs(i-a[step])>=N-2||abs(i-b[step])>=N-2)
    {
        return true;
    }
    return false;
}
bool test2(int c[3])
{
    int i;
    for(i=0;i<3;i++)
    {
        if(abs(res[i]-c[i])>2&&abs(res[i]-c[i])<N-2)
        {
            return false;
        }
    }
    return true;
}
void dfs(int step)
{
    if(step==3)
    {
        if(test2(a)||test2(b))
        {
            sum++;
        }
        return ;
    }
    for(int i=1;i<=N;i++)
    {
        if(test1(step,i))
        {
            res[step]=i;
            dfs(step+1);
        }
    }
 } 
int main()
{
    ifstream cin;
    ofstream cout;
    cin.open("combo.in");
    cout.open("combo.out");
    cin>>N;
    cin>>a[0]>>a[1]>>a[2];
    cin>>b[0]>>b[1]>>b[2];
    dfs(0); 
    cout<<sum<<endl;
 } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值