Title Description
Cut the grid of previous test questions
Time limit: 1.0s memory limit: 256.0MB
Problem description
As shown in the figure below, some integers are filled in the 3 x 3 grid.
±-*–±-+
|10* 1|52|
±- ****–+
|20|30 * 1|
********–+
| 1| 2| 3|
±-±-±-+
We cut along the asterisk line in the figure to get two parts. The sum of the numbers in each part is 60.
The requirement of this problem is to ask you to program to determine whether the integer in a given lattice of m x n can be divided into two parts so that the sum of the numbers in these two areas is equal.
If there are multiple solutions, please output the minimum number of grids contained in the area containing the upper left grid.
If it cannot be split, 0 is output.
input
The program first reads in two integers m and N and divides them with spaces (m, n < 10).
Represents the width and height of the table.
Next are n lines, each with m positive integers, separated by spaces. Each integer is not greater than 10000.
output
Output an integer representing the minimum number of grids that may be contained in the partition area containing the upper left corner in all solutions.
sample input
3 3
10 1 52
20 30 1
1 2 3
sample output
3
Problem solving ideas
DFS + backtracking is used in this problem. The depth search + backtracking of the whole grid array starts from the upper left corner, and a variable curSum is used to record the sum of the currently accessed paths. As long as curSum==sum/2 (sum is the sum of all grids accumulated and calculated during input), the problem conditions are satisfied: it can be divided into two parts to make the numbers and sums of the two areas equal.
If there are multiple answers to the question, the output contains the minimum number of upper left squares. You can define a minCount member variable to record the minimum number of squares. Each time the question conditions (can be divided into two equal parts) are met, judge whether the current number of squares is less than this minCount, and update if it is less than this minCount.
code
Global member variables
//Four directions static int x[]= {-1,0,+1,0}; static int y[]= {0,+1,0,-1}; //Lattice array static int gezi[][]=null; //Secondary path marker static boolean visited[][]=null; //Sum of all grids static int sum; //Minimum lattice number static int minCount=Integer.MAX_VALUE; static int m; static int n;
main method
public static void main(String[] args) { Scanner input=new Scanner(System.in); m=input.nextInt(); n=input.nextInt(); gezi=new int[n][m]; visited=new boolean[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { gezi[i][j]=input.nextInt(); sum+=gezi[i][j]; } } dfs(0,0,0,0); System.out.println(minCount); }
dfs
public static void dfs(int x0,int y0,int curSum,int count) { visited[x0][y0]=true; curSum+=gezi[x0][y0]; count++; if(curSum==sum/2) { if(count<minCount) { minCount=count; } } int nextx=0; int nexty=0; for(int i=0;i<4;i++) { nextx=x0+x[i]; nexty=y0+y[i]; if(nextx>=0&&nextx<n&&nexty>=0&&nexty<m&&!visited[nextx][nexty]) { dfs(nextx,nexty,curSum,count); visited[nextx][nexty]=false;//to flash back } } }
Complete code
import java.util.Scanner; public class P1432 { //Four directions static int x[]= {-1,0,+1,0}; static int y[]= {0,+1,0,-1}; //Lattice array static int gezi[][]=null; //Secondary path marker static boolean visited[][]=null; //Sum of all grids static int sum; //Minimum lattice number static int minCount=Integer.MAX_VALUE; static int m; static int n; public static void main(String[] args) { Scanner input=new Scanner(System.in); m=input.nextInt(); n=input.nextInt(); gezi=new int[n][m]; visited=new boolean[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { gezi[i][j]=input.nextInt(); sum+=gezi[i][j]; } } dfs(0,0,0,0); System.out.println(minCount); } public static void dfs(int x0,int y0,int curSum,int count) { visited[x0][y0]=true; curSum+=gezi[x0][y0]; count++; if(curSum==sum/2) { if(count<minCount) { minCount=count; } } int nextx=0; int nexty=0; for(int i=0;i<4;i++) { nextx=x0+x[i]; nexty=y0+y[i]; if(nextx>=0&&nextx<n&&nexty>=0&&nexty<m&&!visited[nextx][nexty]) { dfs(nextx,nexty,curSum,count); visited[nextx][nexty]=false;//to flash back } } } }