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