Blue Bridge Cup Real Question-Cut Stamps

Attach the title content first.
As [Picture 1.jpg], there are 12 stamps with 12 zodiac signs connected together.
Now you have to cut out 5 sheets from it, and the requirements must be connected.
(Just connecting one corner does not count as connecting) For
example, in [Picture 2.jpg], [Picture 3.jpg], the part shown in pink is a qualified cut.

Please calculate how many different clipping methods there are.

Please fill in an integer representing the number of plans.
Note: What you submit should be an integer, do not fill in any extra content or explanatory text.
Insert picture description here
Insert picture description here
Insert picture description here
This topic is a bit like a maze problem. At first, I wanted to use the dfs algorithm to traverse all the 5-segment paths, but I didn’t know how to remove the duplicates. Later, I still used the ideas of the big guys on csdn to solve the problem.

The scale of this question is small, so we can violently enumerate all the combinations containing 5 numbers from 1 to 12, and then check the connectivity of each combination, and add up if it is connected.

After understanding the idea of ​​the question, take a look at the code implementation:

First, we need to violently enumerate all the combinations:

public static void main(String[] args) {
    
    
	int count=0;
	for(a[0] = 0;a[0]<12;a[0]++)
		for(a[1] = a[0]+1;a[1]<12;a[1]++)
			for(a[2] = a[1]+1;a[2]<12;a[2]++)
				for(a[3] = a[2]+1;a[3]<12;a[3]++)
					for(a[4] = a[3]+1;a[4]<12;a[4]++)
					{
    
    
						if(judge())   这个是后面判断连通性的函数
							count++;
					}
 System.out.println(count);
 }

To determine connectivity, you need to first create an array of Boolean visit[5], use dfs to find all points, and set the corresponding visit to true if you find it. After the traversal, if it is connected, the visit array is all true.

private static boolean judge() {
    
    
	boolean visit[] = new boolean [5];
	dfs(visit,0);
	return visit[0]&&visit[1]&&visit[2]&&visit[3]&&visit[4];
}

Finding points is regular. If two points in the same row are connected, their values ​​will differ by 1. If two points in the same column are connected, their values ​​will differ by 4.

private static void dfs(boolean[] visit, int i) {
    
    
    visit[i] = true;                    每次进入一个新的点就把visit置为true
    for(int j=0;j<visit.length;j++) {
    
    
    	if(!visit[j]&&(a[i]/4==a[j]/4)&&(a[i]-1==a[j] || a[i]+1==a[j]))  判断同一行的且相差1dfs(visit,j);
    	if(!visit[j]&&(a[i]%4==a[j]%4)&&(a[i]-4==a[j] || a[i]+4==a[j]))  判断同一列的且相差4dfs(visit,j);
    }
}

Guess you like

Origin blog.csdn.net/weixin_44919936/article/details/111651681