//
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 0
program input and output format requirements: The program reads two integers m n with a space partition (M,N<10) to indicate the width and height of the table next is n lines, each row m positive integer, separated by a space. Each integer is not greater than 10000 program output: In all solutions, the partition containing the upper-left corner may contain the smallest number of squares.
For example: User input: 3 3 10 1 52 20 30 1 1 2 3
The program output: 3
For example: User input: 4 3 1 1 1 1 1 30 80 2 1 1 1 100
The program output: 10
Ideas:
Dfs
Code
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace Std;
int a[11][11];
int vis[11][11];
int m, n;
int sum = 0, S;
int dir[4][2] = {1, 0, 0, 1,-1, 0, 0,-1};
int res = 0;
int dfs (int x, int y, int temp)
{
if (temp = = s) {
return 1;
}
int r = 0;
for (int i = 0; i < 4; i++) {
int row = x + dir[i][0];
int col = y + dir[i][1];
if (row >= 0 && row < n && Col >= 0 && Col < m) {
if (!vis[row][col] && temp + a[row][col] <= s) {
Vis[row][col] = 1;
R = Dfs (row, col, temp + a[row][col]);
if (r) {
return r + 1;
}
Vis[row][col] = 0;
}
}
}
return 0;
}
int main ()
{
memset (A, 0, sizeof (a));
memset (Vis, 0, sizeof (VIS));
scanf ("%d%d", &m, &n);
for (int i = 0; i < n; i++) {
for (int j = 0; J < m; J + +) {
scanf ("%d", &a[i][j]);
Sum + = A[i][j];
}
}
if (sum% 2 = = 1) {
printf ("0");
}
else {
s = SUM/2;
if (a[0][0] = = s) {
printf ("1");
}
else {
Vis[0][0] = 1;
res = DFS (0, 0, a[0][0]);
printf ("%d", res);
}
}
return 0;
}
Blue Bridge's shearing lattice