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