Blue Bridge Cup 2016 javaB group original title cut stamps (bfs+violent iteration)

Cut stamps


As shown in [Picture 1.jpg], there are 12 stamps with 12 zodiac signs connected together.
Now you have to cut out 5 sheets from it, the requirement must be connected.
(Just connecting one corner does not count as connected) 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.

Analysis: Because there are only twelve elements, you can violently loop, nest five levels of for loops to find 5 elements, and convert these points into rows and 4 columns in Figure 3.

The number of rows is equal to i/4 and the number of columns is equal to i%4. Set other points as inaccessible, and set these five elements as accessible, so bfs, if all five elements are accessed, this solution is feasible, ans++

Not much to say, the code is as follows:



import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class L2016剪邮票 {

	static boolean vis[][]=new boolean[3][4];
	static int mp[][]=new int[3][4]; 
	static int dx[]={-1,0,1,0};
	static int dy[]={0,-1,0,1};
	static class d{//点
		int x;
		int y;
		public d(int x,int y)
		{
			this.x=x;
			this.y=y;
		}
	}
	static Queue<d> q=new LinkedList<d>();
	
	static void init()
	{
		for(int i=0;i<3;i++)
		{
			Arrays.fill(vis[i], false);
			Arrays.fill(mp[i],-1);//障碍
		}
	}
	
	static boolean Check(int x,int y)
	{
		if(x>=0&&y>=0&&x<3&&y<4&&mp[x][y]!=-1)//先判断范围,再判断是否为障碍,从而达到防止
//数组越界的效果
			return true;
		return false;
	}
	
	static void bfs(int x,int y)
	{
		d cor=new d(x,y);
		q.add(cor);
		while(!q.isEmpty())
		{
			d now=q.poll();
			for(int i=0;i<4;i++)
			{
				int xx=now.x+dx[i];
				int yy=now.y+dy[i];
				if(Check(xx,yy)&&!vis[xx][yy])
				{
					vis[xx][yy]=true;
					d next=new d(xx,yy);
					q.add(next);
				}
			}
		}
		
		return ;
	}
	
	public static void main(String[] args) 
	{
		int ans=0;
		for(int i=0;i<12;i++)
		{
			for(int j=i+1;j<12;j++)
			{
				for(int k=j+1;k<12;k++)
				{
					for(int l=k+1;l<12;l++)
					{
						for(int m=l+1;m<12;m++)
						{
							init();
							mp[i/4][i%4]=1;//转换
							mp[j/4][j%4]=1;
							mp[k/4][k%4]=1;
							mp[l/4][l%4]=1;
							mp[m/4][m%4]=1;
							vis[i/4][i%4]=true;
							bfs(i/4,i%4);
							
							if(vis[i/4][i%4]==true
								&&vis[j/4][j%4]==true
								&&vis[k/4][k%4]==true
								&&vis[l/4][l%4]==true
								&&vis[m/4][m%4]==true)
							{
								ans++;
							}
						}
							
					}
				}
			}
		}
		System.out.println(ans);
		
	}

}

Get the answer:

116

Feel:

The blue bridge cup fill in the blanks are basically violent! !

Guess you like

Origin blog.csdn.net/Look_star/article/details/88650307