2016 Blue Bridge Cup Cut Stamp dfs

Blue Bridge Cup Cut Stamp

This is the seventh question of the 2016 Blue Bridge Cup C Language Provincial Competition Group B

Title:
As shown below, there are 12 stamps of 12 Chinese zodiac signs connected together. Now you have to cut out 5 sheets from it, the requirement must be connected. (Just connecting a corner is not considered connected)
figure 1

For example, in the following two pictures, the part shown in pink is a qualified cut.
figure 2

Please calculate how many different clipping methods there are.

Output:
Please fill in an integer representing the number of plans.

OJ link

Ideas:

  • First, we store the array as
    1 2 3 4
    6 7 8 9
    11 12 13 14
    so that if the absolute value of the subtraction of two numbers is 5, it is the upper and lower adjacent relationship, and the absolute value is 1 is the left and right adjacent relationship.

image 3
By observing the above picture, we can find that if the meaning of the question is satisfied, the sum of the number of connected stamps of each stamp must be greater than or equal to 8, and each stamp has a connected stamp. (The sum of the number of connections in the above two pictures is 8. If you cut 12567, the number of connections is 9.) According to this rule, we can use a double loop to search for the answer.

  • The previous five layers of loops were used to combine all the possibilities to prevent duplication.

AC code:

#include<bits/stdc++.h> 
using namespace std;

int temp[]={
    
    1,2,3,4,6,7,8,9,11,12,13,14};
int shuzu[5]; 
int ans=0;

/*
1  2  3  4
6  7  8  9
11 12 13 14
*/

bool judge()
{
    
    
	int count=0;
	for(int now=0;now<5;now++)//取前五个 看是否都相连 
	{
    
    
		int flag=0;//先假设不相连 
		for(int now1=0;now1<5;now1++)//挨个判断 
		{
    
    
			if(now==now1)
				continue;
			if(abs(shuzu[now1]-shuzu[now])==5||abs(shuzu[now1]-shuzu[now])==1)//上下相连 绝对值为5 或者 左右相连 绝对值为1 
			{
    
    
				flag+=1;//相连点位+1 
			}
		}
		if(flag==0)//如果是孤立点 没有相连点位 就直接返回false  
		{
    
    
			return false;
		}
		count+=flag;//加上连接数 
	}
	if(count<8)//如果连接数小于8 则说明5个点位没有相互相邻 
	{
    
    
		return false;
	}
	//printf("%d %d %d %d %d\n",shuzu[0],shuzu[1],shuzu[2],shuzu[3],shuzu[4]);
	return true;
}

int main()
{
    
    
	for(int a=0;a<12;a++)
	{
    
    
		for(int b=a+1;b<12;b++)
		{
    
    
			for(int c=b+1;c<12;c++)
			{
    
    
				for(int d=c+1;d<12;d++)
				{
    
    
					for(int e=d+1;e<12;e++)
					{
    
    
						shuzu[0]=temp[a],shuzu[1]=temp[b],shuzu[2]=temp[c],shuzu[3]=temp[d],shuzu[4]=temp[e];
						if(judge())
						{
    
    
							ans++;
						}
					}	
				}
			}
		}
	}
	cout<<ans; 
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_45698148/article/details/108329617
Recommended