Blue Bridge Cup-cut lattice (simple Dfs)

Source: Internet
Author: User

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




Do not know why re once.


AC Code:

#include <cstdio> #include <cstring> #include <algorithm> #define INF 0x3f3f3f3fusing namespace std; int N, m;int a[15][15];int vis[15][15];int ans;//min grid number int sum;//all integers and half int cnt;//current lattice number int xx[4] = {-1, 0, 1, 0}, Yy[4 ] = {0, 1, 0, -1};void dfs (int x, int y, int cnt, int value) {if (x < 1 | | y < 1 | | x > N | | y > M) return;if (v Alue = = sum) {if (cnt < ans) ans = cnt;return;} else if (value > Sum) return;else {for (int i = 0; i < 4; i++) {int p = x + xx[i], q = y + yy[i];if (!vis[p][q]) {vis[p ][Q] = 1;dfs (p, Q, Cnt+1, value+a[p][q]); Vis[p][q] = 0;}}} int main () {while (scanf ("%d%d", &m, &n)! = EOF) {sum = 0;for (int i = 1; I <= n; i++) {for (int j = 1; J <= M ; J + +) {scanf ("%d", &a[i][j]); Vis[i][j] = 0;sum + = A[i][j];}} Sum/= 2;ans = inf;vis[1][1] = 1;dfs (1, 1, 1, a[1][1]), if (ans = = INF) printf ("0\n"), Else printf ("%d\n", ans); }return 0;}












Blue Bridge Cup-cut lattice (simple Dfs)

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.