其他分享
首页 > 其他分享> > USACO-Section1.3 Combination Lock

USACO-Section1.3 Combination Lock

作者:互联网

Combination Lock

2017.06.01
###题解###

搜索,按条件剪枝即可,注意搜索最后要再次检验数字是否符合同一个密码的条件。

###代码###

/*
ID: xhzdcyy1
PROB: combo
LANG: C++         
*/
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#define cin fin
#define cout fout 
using namespace std;
ofstream fout ("combo.out");
ifstream fin ("combo.in");
int N;
int res=0;
int a[3],b[3],c[3];
int abs(int a,int b){
	return a-b>0?a-b:b-a;
}
bool check(int t[],int i){
	int left=t[i]-2;
	if(left<=0) left+=N;
	int right=t[i]+2;
	if(right>N) right-=N;
	if(left<right){
		if(c[i]<left||c[i]>right) return false;
	}
	else{
		if(c[i]>right&&c[i]<left) return false;
	}
	return true;
	
}
void func(int n){
	if(n==3){
		if(check(a,0)&&check(a,1)&&check(a,2)) ++res;
		else if(check(b,0)&&check(b,1)&&check(b,2)) ++res;
		return;
	}
	for(int i=0;i<N;i++){
		c[n]=i;
		if(check(a,n)||check(b,n))
			func(n+1);
	}
}

int main()
{
	int tmp;
	cin>>N;
	for(int i=0;i<3;i++){
		cin>>a[i];
	}
	for(int i=0;i<3;i++){
		cin>>b[i];
	}

	func(0);
	cout<<res<<endl; 

 	return 0;
 }

标签:right,Combination,int,Lock,fout,combo,include,Section1.3,###
来源: https://blog.csdn.net/xhzdcyy/article/details/72848402