Blue Bridge Cup: Title: Cut lattice

Source: Internet
Author: User

Title: Cut lattice

P1.jpg p2.jpg

As shown in P1.jpg, a number of integers are filled in 3 x 3 squares.

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
(See P2.jpg)
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 explicitly #include <xxx&gt in the source file, and the common header files cannot be omitted from the project settings.

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

Thinking:

First of All,you has to realize, the grid which located in the upper left must is a part of the The and part Divided,thus , we can start DFS from coordinate (0,0).

Detail in the code notes.


#include <iostream> #include <string> #include <algorithm>using namespace Std;int visited[10][10]={0 },sum=0,direction[4][2]={{-1,0},{0,-1},{1,0},{0,1}},total=0;int row,col,num[10][10]={0},nseq=0;string reserve[50 ];bool judge (int count)//to judge whether the sequence is repeat. {int i,j,k=0,l;for (i=0;i<row;i++) for (j=0;j<col;j++) if (visited[i][j]==1) reserve[nseq].push_back (I*COL+J); /push the coordinate into Reserve[]sort (Reserve[nseq].begin (), Reserve[nseq].end ());//and sort it smaller to bigger. Nseq++;//nseq represent n sequences.if (nseq!=1)//if nseq is equal to 1,you dont ' has to judge it. {for (l=0;l<nseq-1;l++) if (Reserve[l]==reserve[nseq-1]) {reserve[nseq-1].clear (); Nseq--;return false;}} return true;} int MinLength ()//if the reserve[] has more than 1 sequence,you has to find the shortest sequence. {int i,j,minlen=999;for (i=0;i<nseq;i++)//easy to understand,no more Explains.if (Reserve[i].length () <minLen) Minlen=reserve[i].length (); return minlen;} void DFS (int x, int y,int count) {int next_x,next_y,i;if (count==sum) {if (Judge (count)) Total++;return;} else if (count>sum) return;for (i=0;i<4;i++) {next_x=x+direction[i][0];next_y=y+direction[i][1];if (next_x< 0| | next_y<0| | next_x>=row| | Next_y>=col)//boundary Conditioncontinue;else if (visited[next_x][next_y]==0)//if the point hasn ' t been visited. {Visited[next_x][next_y]=1;//set The point has been visited. DFS (next_x,next_y,count+num[next_x][next_y]); Visited[next_x][next_y]=0;//set the point hasn ' t been visited.}}} int main () {int i,j;cin>>row>>col;for (i=0;i<row;i++) for (j=0;j<col;j++) {cin>>num[i][j];sum +=NUM[I][J];} if (sum%2!=0)//if sum is odd, quit. {cout<< "0" <<endl;exit (0);} Sum/=2;visited[0][0]=1;dfs (0,0,num[0][0]); if (total!=0)//if the Path Existcout<<minlength () <<endl; elsecout<< "0" <<endl;return 0;}


Blue Bridge Cup: Title: 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.