Title Description
As shown, a number of integers are filled in 3 x 3 squares.
+--*--+--+
|10* 1|52|
+--****--+
|20|30* 1|
*******--+
| 1| 2| 3|
+--+--+--+
We cut along the star Line in the diagram and get two parts, each with a number of 60.
The requirement of the subject is to ask you to determine whether the integer in the given m x n lattice can be divided into two parts, making the numbers and the two regions equal.
If there are multiple answers, output the minimum number of squares contained in the area containing the upper-left lattice.
If it cannot be split, the output is 0.
Input format
The program reads in two integer m n with a space partition (M,N<10).
Next is n rows, each m positive integer, separated by a space. Each integer is not greater than 10000.
Output format
Outputs an integer representing the smallest number of squares that may be included in the upper-left corner of the partition in all solutions.
Sample Input 1
3 3
10 1 52
20 30 1
1 2 3
Sample Output 1
3
Sample Input 2
4 3
1 1 1 1
1 30 80 2
1 1 1 100
Sample Output 2
10
Import Java.util.scanner;public class Main {private static int n;private static int m;private static int min = 100;private static int gezi[][];p ublic static void Main (String args[]) {Scanner cin = new Scanner (system.in); m = Cin.nextint (); n = ci N.nextint (); gezi = new Int[n][m];int sum = 0;for (int i = 0; i < n; i++) {for (int j = 0; J < m; J + +) {Gezi[i][j] = Cin.nextint (); sum + = Gezi[i][j];}} Cal (0, 0, SUM/2, 0, New int[n][m]); System.out.println (min);} private static Boolean cal (int nn, int mm, int s, int c, int ed[][]) {if (s = = 0) {if (C < min) min = C;return true;} if (s < 0) return False;if (mm < m && NN < n && mm >= 0 && nn >= 0) {if (ed[nn][mm] = = 0) {s-= gezi[nn][mm];ed[nn][mm] = 1;c++;cal (nn + 1, mm, S, c, ed), Cal (Nn-1, MM, S, c, ed), Cal (NN, mm + 1, S, c, ed); Cal (NN, mm-1, S, c, ed); ed[nn][mm] = 0;}} return false;}}
Blue Bridge Cup: Previous questions cut Lattice "Java algorithm implementation"