[Blue] scissors Bridge Cup grid (dfs template title)

Cut grid

Title Description

Time limit: 1.0s memory limit: 256.0MB
problem is described
as shown below, a 3 x 3 grid fill some integer.
± - - ± - +
| 10
. 1 | 52 is |
± - *** - +
| 20 is | 30
. 1 |
******* - +
|. 1 | 2 |. 3 |
± - ± - ± - +
us in FIG asterisk in the cut line, to obtain two portions, each portion and the numbers are 60.
The title is required you program determination: integer given in mxn grid, can be divided into two parts, so that the two regions and the digital equivalent.
If there are multiple answers, please include the output of the minimum number of lattice top left corner of the grid contains.
If you can not split the output 0.

Entry

Program reads two first space-dividing integers mn (m, n <10).
Width and height of the table.
Followed by n lines of m positive integers, separated by spaces. Each integer is not greater than 10,000.

Export

Output an integer, represents in all solutions, the upper left corner of the lattice contains the minimum number of partitions may be included.

Sample input

3 3
10 1 52
20 30 1
1 2 3

Sample Output

3

Code

package 剪格子;
import java.util.Scanner;
public class Main {
	public static int m,n;
	public static int sum;
	public static int minn=100000;
	public static int [][]move= {{0,1},{0,-1},{1,0},{-1,0}};
	public static int [][]a=new int [11][11];
	//public static HashSet<String> set = new HashSet<String>()
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in=new Scanner(System.in);
		m=in.nextInt();
		n=in.nextInt();
		sum=0;
		//int minn=1000000;
		int [][]mp=new int [11][11];
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				a[i][j]=in.nextInt();
				mp[i][j]=0;
				sum=sum+a[i][j];
			}
		}
		bfs(a[1][1],1,1,1,mp);
		System.out.println(minn);
	}
	public static void bfs(int s,int x,int y,int k,int mp[][])
	{
		if(s==sum/2)
		{
			if(k<minn)
			{
				minn=k;
				//System.out.println(minn);
			}
			return ;
		}
		else
		{
			if(x<=n&&x>=1&&y<=m&&y>=1)
			{
				mp[x][y]=1;
				for(int i=0;i<4;i++)
				{
					if(mp[x+move[i][0]][y+move[i][1]]==0)
					{
						bfs(s+a[x+move[i][0]][y+move[i][1]],x+move[i][0],y+move[i][1],k+1,mp);
					}
				}
				mp[x][y]=0;
			}
		}
		
	}

}

Published 20 original articles · won praise 0 · Views 3858

Guess you like

Origin blog.csdn.net/weixin_44544406/article/details/104307938