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;
 }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值