Seventh session of the Blue Bridge Cup c\c++b Group--Square

Source: Internet
Author: User

Part I: Topics

Number of squares to fill

The 10 squares below
+--+--+--+
|  |  | |
+--+--+--+--+
|  |  |  | |
+--+--+--+--+
|  |  | |
+--+--+--+

(See also if you have a problem with the display)



Fill in the number of 0~9. Requirement: Consecutive two digits cannot be contiguous.
(around, up and down, diagonal are counted adjacent)

How many kinds of possible filling schemes are there?

Please complete an integer representing the number of scenarios.
Note: You should submit an integer, do not fill in any superfluous content or descriptive text.

Part II: Ideas

This topic is a little vague, I do not know whether 0~9 can be reused. Now it seems that it cannot be reused. My approach is to take the table as an array of 3 rows and 4 columns, minus one end. Step: Fill in the number and determine whether to meet the requirements: Adjacent position number can not be adjacent. Note: Make sure the numbers are not reused: Use the array take to store the numbers used, and fill in the numbers when they are not in the array.                       Fill in the array after filling in. Determine whether the number of adjacent positions can not be adjacent to the requirements. Look at the code comment. The answer is: 1580 Part III: Code
#include <stdio.h>#include<stdlib.h>intCount=0;inttake[Ten],index=0;//record the numbers that are currently filled in to determine whether they have been used before they are filled in, and to avoid re-useintIs_legal (ints[3][4])//determine whether to meet the requirements: Adjacent position number can not be adjacent, that is, the difference of two number is greater than 1. is why S[0][0], s[2][3] set to 2;{    //The judging method here is a bit stiff. Assuming that each location has up and down and diagonally, just judge these locations//is not in the array, in the comparison, not on the description is not.      for(intI=0;i<3; i++)    {         for(intj=0;j<4; j + +)        {            if(J-1>=0)            {                if(ABS (s[i][j]-s[i][j-1])==1)                {                    return 0; }            }            if(j+1<4)            {                if(ABS (s[i][j]-s[i][j+1])==1)                {                    return 0; }            }            if(i+1<3)            {                if(ABS (s[i][j]-s[i+1][J]) = =1)                {                    return 0; }            }            if(J-1>=0&&i+1<3)            {                if(ABS (s[i][j]-s[i+1][j-1])==1)                {                    return 0; }            }            if(j+1<4&&i+1<3)            {                if(ABS (s[i][j]-s[i+1][j+1])==1)                {                    return 0; }            }            if(I-1>=0&&j+1<4)            {                if(ABS (s[i][j]-s[i-1][j+1])==1)                {                    return 0; }            }            if(I-1>=0)            {                if(ABS (s[i][j]-s[i-1][J]) = =1)                {                    return 0; }            }            if(J-1>=0&&i-1>=0)            {                if(ABS (s[i][j]-s[i-1][j-1])==1)                {                    return 0; }            }        }    }     return 1;}voidFunints[3][4],intAintb) {    inti; if(a==2&&b==3)//indicates that the table is currently filled and needs to be judged to see if it meets the requirements    {        if(Is_legal (s)) {count++; }    }    Else//continue to fill in    {         for(i=0; i<=9; i++)        {            intJ;  for(j=0; j<index;j++)//the numbers you fill in must be unused.            {                if(i==Take[j]) {                     Break; }            }            if(j==index) {S[a][b]=i; Take[index++]=i; if(b<3)//indicates that the current line is not filled out{Fun (s,a,b+1); }                Else//The current line is complete and starts on the next line                {                    if(a<2)//determines whether the current line is the last row{Fun (s,a+1,0); }} Index--;//at the end of a fill, the number of the current position is changed to another number that can be filled//so the numbers currently used need to go out.             }        }     }}intMain () {ints[3][4]; s[0][0]=-2; s[2][3]=-2; //The upper left and bottom right corner are not, to make it easier to judge the value to be 2 (less than 1 is greater than 10)Fun (S,0,1); printf ("%d\n", Count); return 0;}

Seventh session of the Blue Bridge Cup c\c++b Group--Square

Related Article

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.