"Blue Bridge Cup" PREV-4 cut lattice

Source: Internet
Author: User

Title Link: http://lx.lanqiao.org/problem.page?gpid=T27


Previous questions cut lattice time limit: 1.0s memory limit: 256.0MBProblem 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.

The output format outputs 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
10 1 52
20 30 1
1 2 3 Sample Output 13 sample input 24 3
1 1 1 1
1 30 80 2
1 1 1 100 Sample output 210

Puzzle: Deep Search

AC Code:

#include <iostream> #define N 11using namespace Std;int n,m,maze[n][n],sum=0,res=0;int visit[n][n]={0};int dir[][ 2]={{-1,0},{0,-1},{1,0},{0,1}};void dfs (int x,int y,int cnt,int Step) {if (cnt==sum) {if (res>step| | res==0) Res=step;return;} if (step>res&&res!=0| | Cnt>sum) return, for (int i=0;i<4;i++) {int tx=x+dir[i][0],ty=y+dir[i][1];if (tx>=0&&ty>=0& &tx<n&&ty<m&& (Cnt+maze[tx][ty]) &&!visit[tx][ty] {Visit[tx][ty]=1;dfs (tx,ty, cnt+maze[tx][ty],step+1); visit[tx][ty]=0;}}} int main () {cin>>n>>m;for (int i=0;i<m;i++) for (int j=0;j<n;j++) {cin>>maze[i][j]; sum+=maze[i ][J];} Sum/=2;visit[0][0]=1;dfs (0,0,maze[0][0],1); Cout<<res<<endl;return 0;}

Source: http://blog.csdn.net/mummyding

Author: mummyding


"Blue Bridge Cup" PREV-4 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.