Algorithm Note _196: Previous test cut lattice (Java)

Source: Internet
Author: User

Directory

1 Problem Description

2 Solutions

  1 problem description 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.

output Formatoutputs 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
1
1
1 2 3Sample Output 13Sample Input 24 3
1 1 1 1
1 2
1 1 1Sample Output 2Ten

2 Solutions

The specific code is as follows:

ImportJava.util.Scanner; Public classMain { Public Static intN, M;  Public Static intSUM;  Public Static int[] Step = {{ -1,0},{1,0},{0,-1},{0,1}};  Public Static int[] map;  Public Static int[] visited;  Public Static intresult = 10000;  Public voidTestdfs (intXintYint[] v) {//whether the other part of the cut is connected after completionV[x][y] = 2;  for(inti = 0;i < 4;i++) {            intX1 = x + step[i][0]; intY1 = y + step[i][1]; if(X1 < 0 | | X1 >= N | | Y1 < 0 | | Y1 >=m)Continue; if(V[x1][y1] = = 0) Testdfs (x1, y1, v); }    }         Public voidDfsintStartX,intStarty,intCountintsum) {        if(Sum = = SUM/2) {            intx = 0, y = 0, step = 0; int[] v =New int[N][m];  for(inti = 0;i < n;i++) {                 for(intj = 0;j < m;j++) {                    if(Visited[i][j] = = 0) {x=i; Y=J; } V[i][j]=Visited[i][j];            }} testdfs (x, y, v);  for(inti = 0;i < n;i++)                 for(intj = 0;j < m;j++)                    if(V[i][j] = = 2) Step++; if(step + Count = = n *m) {if(Visited[0][0] = = 1) Result=math.min (result, count); Else{result=math.min (result, step); }            }            return; } Else if(Sum > SUM/2)            return;  for(inti = 0;i < 4;i++) {            intX1 = StartX + step[i][0]; intY1 = Starty + step[i][1]; if(X1 < 0 | | X1 >= N | | Y1 < 0 | | Y1 >=m)Continue; if(Visited[x1][y1] = = 1)                Continue; VISITED[X1][Y1]= 1; Count++; Sum+=Map[x1][y1];            DFS (x1, y1, count, sum); Sum-=Map[x1][y1]; Count--; VISITED[X1][Y1]= 0; }    }         Public Static voidMain (string[] args) {main test=NewMain (); Scanner in=NewScanner (system.in); M=In.nextint (); N=In.nextint (); Map=New int[N][m]; SUM= 0;  for(inti = 0;i < n;i++) {             for(intj = 0;j < m;j++) {Map[i][j]=In.nextint (); SUM+=Map[i][j]; }        }        if(SUM% 2 = = 0) {             for(inti = 0;i < n;i++) {                 for(intj = 0;j < m;j++) {visited=New int[N][m]; intsum =Map[i][j]; intCount = 1; VISITED[I][J]= 1;                Test.dfs (i, J, Count, sum); }            }            if(Result! = 10000) System.out.println (result); ElseSystem.out.println ("-1"); } ElseSystem.out.println ("-1"); }}

Algorithm Note _196: Previous test cut lattice (Java)

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.