Blue Bridge's shearing lattice

Source: Internet
Author: User

//

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.