Usaco 1.3 Combination Lock

Source: Internet
Author: User

Combination Lock

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

Knowing that he cows is quite clever, Farmer John wants to make sure they cannot easily open the lock by simply trying M Any different combinations. The lock has three dials, each numbered 1..N (1 <= N <=), where 1 and n is adjacent since the dials is Circula R. There is combinations that open the lock, one set by Farmer John, and also a "master" combination set by the lock Maker.

The lock has a small tolerance for error, however, so it would open even if the numbers on the dials is each within at MOS T 2 positions of a valid combination.

For example, if Farmer John's combination is (A-Z) and the master combination is (4,5,6), the lock would open if its dial S is set to (1,3,5) (since-is-close enough to Farmer John's combination) or to (2,4,8) (since the is close enough t o the master combination). Note that (1,5,6) would not open the lock, since it was not close enough to any one single combination.

Given Farmer John ' s combination and the master combination, please determine the number of distinct settings for the dials That'll open the lock. Order matters, so the setting (all-in-a--) is distinct from (3,2,1).

Program Name:comboinput FORMAT:
Line 1: The integer N.
Line 2: Three space-separated integers, specifying Farmer 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):
501 2 35) 6 7
INPUT DETAILS:

Each dial is numbered 1..50. Farmer John's combination is (-), and the master combination is (5,6,7).

OUTPUT FORMAT:
Line 1: The number of distinct dial settings that would open the lock.
SAMPLE OUTPUT (file combo.out):
249
SAMPLE OUTPUT Explanation

Here's a list:

1,1,1 2,2,4 3,4,2 4,4,5 5,4,8 6,5,6 7,5,9 3,50,2 50,1,4 1,1,2 2,2,5 3,4,3 4,4,6 5,4,9 6,5,7 7,6,5 3,50,3 5 0,1,5 1,1,3 2,3,1 3,4,4 4,4,7 5,5,5 6,5,8 7,6,6 3,50,4 50,2,1 1,1,4 2,3,2 3,4,5 4,4,8 5,5,6 6,5,9 7,6,7 3,5 0,5 50,2,2 1,1,5 2,3,3 3,4,6 4,4,9 5,5,7 6,6,5 7,6,8 49,1,1 50,2,3 1,2,1 2,3,4 3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4,8,9 6,4,7 7,5,5 2,50,4 50,1,1 50,50,32,2,1 3,3 ,4 3,8,7 5,4,5 6,4,8 7,5,6 2,50,5 50,1,2 50,50,42,2,2 3,3,5 3,8,8 5,4,6 6,4,9 7,5,7 3,50,1 50,1,3 50,50,52,2, 3 3,4,1 3,8,9 5,4,7 6,5,5 7,5,8

The key: Give two password, as long as each input each number and the correct password in the corresponding location of the number of the difference not more than 2, it is decided to open; just sweep it all over again.

PS: The People's Congress Three dog one, is continuing to update the blog, the article has any questions, I hope you can point out. If in doubt you can leave a comment in the comments section, I will reply as soon as possible. I hope you can learn from each other, thank you

/*id:cxq_xia1prog:combolang:c++*/#include <iostream> #include <cstdio> #include <cstring>using namespace std;struct node{int x, y, z;};    Node Key1,key2;int n,ans;bool flag[102][102][102];int Main () {freopen ("combo.in", "R", stdin);    Freopen ("Combo.out", "w", stdout);    memset (flag,false,sizeof (flag));    Cin >> N;    CIN >> key1.x >> key1.y >> key1.z;    CIN >> key2.x >> key2.y >> key2.z; for (int i=key1.x+n-2;i<=key1.x+n+2;i++) {for (int. j=key1.y+n-2;j<=key1.y+n+2;j++) {for (                    int z=key1.z+n-2;z<=key1.z+n+2;z++) {if (!flag[i%n][j%n][z%n]) {                    ans++;                Flag[i%n][j%n][z%n]=true; }}}} for (int i=key2.x+n-2;i<=key2.x+n+2;i++) {for (int j=key2.y+n-2;j<=key2.y+n +2;j++) {for (int z=key2.z+n-2;z<=key2.z+n+2;z++) {if (!Flag[i%n][j%n][z%n]) {ans++;                Flag[i%n][j%n][z%n]=true;    }}}} cout<<ans<<endl; return 0;}

  






Usaco 1.3 Combination Lock

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.