Problem 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).
Represents the width and height of a table.
Next is n rows, each m positive integer, separated by a space. Each integer is not greater than 10000.
The 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 13 3
10 1 52
20 30 1
1 2 3 Sample Output 13 sample input 24 3
1 1 1 1
1 30 80 2
1 1 1 100 sample output 210 The DFS back in red fruit is a bit over. Attached code:
1 /*2 seems to be very straightforward dfs+ backtracking3 */4 5#include <stdio.h>6#include <string.h>7#include <iostream>8#include <queue>9 #defineINF 100000000Ten using namespacestd; One A intmp[ -][ -]; - intans, sum; - intN, M; the - intdir[4][2] = {1,0, -1,0,0,1,0, -1}; - intvis[ -][ -]; - + BOOLCheckintAintb) { - if(A >=0&& a < n && b >=0&& b < m &&!Vis[a][b]) { + return true; A } at return false; - } - - voidDfsintXintYintNuminttsum) { - if(Tsum = = sum/2) { - if(Ans >num) { inAns =num; - return; to } + } - the for(intI=0; i<4; ++i) { * inttx = x + dir[i][0]; $ intty = y + dir[i][1];Panax Notoginseng if(Check (TX, Ty)) { -Vis[tx][ty] =1; theDFS (TX, Ty, num+1, tsum+Mp[tx][ty]); +Vis[tx][ty] =0; A } the } + } - $ intMain () { $ while(Cin >> M >>N) { -Ans =inf; -sum =0; thememset (Vis,0,sizeof(Vis)); - Wuyi for(intI=0; i<n; ++i) { the for(intj=0; j<m; ++j) { -CIN >>Mp[i][j]; WuSum + =Mp[i][j]; - } About } $ -vis[0][0] =1; -Dfs0,0,1, mp[0][0]); -cout << ans <<Endl; A } + return 0; the}
View Code
Blue Bridge Cup practice system previous questions cut lattice Dfs