Blue Bridge Cup programming cut lattice

Source: Internet
Author: User

We cut along the red line in the figure, get two parts, each part of the number and is 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 0

program input and output format requirements:

The program reads in two integers m n separated by a space (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
Program output: In all solutions, the smallest number of squares that may be included in the upper-left corner of the split area.

For example:
User input:
3 3
10 1 52
20 30 1
1 2 3

The program output:
3

Again for example:
User input:
4 3
1 1 1 1
1 30 80 2
1 1 1 100

The program output:
10

Resource contract:
Peak memory Consumption < 64M
CPU Consumption < 5000ms

Please strictly according to the requirements of the output, do not use the superfluous printing similar: "Please enter ..." Redundant content.

All the code is placed in the same source file, after debugging passed, the copy is submitted to the source.

Note: The main function needs to return 0
Note: Use only ANSI c/ansi C + + standards, and do not invoke special functions that depend on the compilation environment or operating system.
Note: All dependent functions must be explicitly #include in the source file and cannot be omitted from the common header file by engineering Setup.

When committing, be careful to choose the type of compiler you expect.

#include <stdio.h>
#include <string.h>
int a[11][11];
int isv[11][11];
Record grid in four directions left upper right Bottom
int dx[4]={-1,0,1,0};
int dy[4]={0,-1,0,1};
int minstep;
int sum;
int n,m;
int is_ok (int x,int y,int num) {
if (x<1 | | x>m | | y<1 | | y>n)
return 0;
if (A[X][Y]+NUM&GT;SUM/2)
return 0;
if (isv[x][y]==1)
return 0;
return 1;

}
Records the coordinates of the current grid, and records the number of current lattices traversed
void Dfs (int x,int y,int Num,int cnt) {
When the number of squares is exactly half, find exactly half of the lattice, ending
if (NUM==SUM/2) {
if (cnt+1<minstep)
minstep=cnt+1; Record the minimum number of squares
Return
}
for (int i=0;i<4;i++) {//starting from the first lattice, looking for possible lattice points in four directions
int nx=x+dx[i];
int ny=y+dy[i];
if (!IS_OK (nx,ny,num))//Determine if this lattice is not and requirements, do not meet then re-search
Continue
Isv[nx][ny]=1; If the requirement is marked as a sought-after point and the record is 1
DFS (NX,NY,NUM+A[NX][NY],CNT+1); Continue depth first search to meet the requirements of the lattice
isv[nx][ny]=0; Another node, place this node 0, re-locate
}
}
int main () {
Depth and width of the deposited lattice
while (scanf ("%d%d", &n,&m) ==2) {
Initializes the sum of all the squares
sum=0;
MINSTEP=0X7FFFFFFF;
An ISV is a tag array that records whether the current grid has been used
memset (isv,0,sizeof (ISV));
Record the depth and width of the lattice starting from 1
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++) {
scanf ("%d", &a[i][j]);
SUM+=A[I][J];
}
If the sum of the lattice is odd, it is impossible to divide by 2
if (sum%2) {
printf ("0\n");
}else{
Start deep Search from the first grid and the record grid has been recorded.
Isv[1][1]=1;
DFS (1,1,a[1][1],0);
printf ("%d\n", minstep);
}
}
return 0;
}

Blue Bridge Cup programming cut 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.