USACO 1.3 - Combination Lock(暴力枚举)

Farmer John's cows keep escaping from his farm and causing mischief. To tryand prevent them from leaving, he purchases a fancy combination lock tokeep his cows from opening the pasture gate.

Knowing that his cows are quite clever, Farmer John wants to make sure theycannot easily open the lock by simply trying many different combinations. The lock has three dials, each numbered 1..N (1 <= N <= 100), where 1 and Nare adjacent since the dials are circular. There are two combinations thatopen the lock, one set by Farmer John, and also a "master" combination setby the lock maker. The lock has a small tolerance for error, however, soit will open even if the numbers on the dials are each within at most 2positions of a valid combination.

For example, if Farmer John's combination is (1,2,3) and the mastercombination is (4,5,6), the lock will open if its dials are set to (1,N,5)(since this is close enough to Farmer John's combination) or to (2,4,8)(since this is close enough to the master combination). Note that(1,5,6) would not open the lock, since it is not close enough to anyone single combination.

Given Farmer John's combination and the master combination, pleasedetermine the number of distinct settings for the dials that will open thelock. Order matters, so the setting (1,2,3) is distinct from (3,2,1).

PROGRAM NAME: combo

INPUT FORMAT:

Line 1:The integer N.
Line 2:Three space-separated integers, specifyingFarmer John's combination.
Line 3:Three space-separated integers, specifying the master combination (possibly the same as Farmer John's combination).

SAMPLE INPUT (file combo.in):

50
1 2 3
5 6 7

INPUT DETAILS:

Each dial is numbered 1..50. Farmer John's combination is (1,2,3), and themaster combination is (5,6,7).

OUTPUT FORMAT:

Line 1:The number of distinct dial settings that will open the lock.

SAMPLE OUTPUT (file combo.out):

249

                                              

思路:

应该注意到N的数字范围只有 1 <= N <= 100 所以直接暴力即可。

暴力也是有技巧的,傻傻不会暴力。

CODE:

/*
ID: sotifis3
LANG: C++
TASK: combo
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <math.h>
#include <algorithm>
using namespace std;

int n;

bool close(int a, int b)
{
    if(abs(a - b) <= 2) return true;
    if(abs(a - b) >= n - 2) return true;
    return false;
}
bool moreclose(int a1, int b1, int c1,
               int a2, int b2, int c2)
{
    return close(a1, a2) && close(b1, b2) && close(c1, c2);
}

int main()
{
    //freopen("in", "r", stdin);
    freopen("combo.in","r",stdin);
    freopen("combo.out","w",stdout);
    while(~scanf("%d", &n)){
        int ans = 0;
        int x1, y1, z1, x2, y2, z2;
        scanf("%d%d%d %d%d%d", &x1, &y1, &z1, &x2, &y2, &z2);
        for(int i = 1; i <= n; ++i){
            for(int j = 1; j <= n; ++j){
                for(int k = 1; k <= n; ++k){
                    if(moreclose(i, j, k, x1, y1, z1) ||
                       moreclose(i, j, k, x2, y2, z2))
                      ans ++;
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值