[Daily Blue Bridge] 10, 13 years of provincial competition Java group real question "cut the grid"

Hello, I am the little gray ape, a programmer who can write bugs!

Welcome everyone to pay attention to my column " Daily Blue Bridge ". The main function of this column is to share with you the real questions of the Blue Bridge Cup provincial competitions and finals in recent years, analyze the algorithm ideas, data structures and other content that exist in it, and help you learn To more knowledge and technology!

Title: Cut the grid

As shown in figure p1.jpg, some integers are filled in the 3 x 3 grid

We cut along the red line in the picture to get two parts, the sum of the numbers in each part is 60,

The requirement of this question is to allow you to programmatically determine whether the integer in the given mxn grid can be divided into two parts so that the sum of the numbers in these two areas is equal.

If there are multiple solutions, please output the minimum number of grids contained in the area containing the top left grid.

Program input and output format requirements: the
program first reads two integers mn separated by spaces (m,n<10)

Indicates the width and height of the table

Next are n lines, each line of m positive integers, separated by spaces, each integer is not greater than 10000

Program output: Among all the solutions, the smallest number of grids that may be contained in the upper left corner is included

If it cannot be divided, it will output 0

E.g:

User input:

3 3

10 1 52

20 30 1

1 2 3

The program output:

3

 

Another example:

User input:

4 3

1 1 1 1

1 30 80 2

1 1 1 100

The program output:

10

Resource agreement:
peak memory consumption <64M

CPU consumption <1000ms

Please output in strict accordance with the requirements, and do not print superfluous content similar to: "Please enter...".

All the codes are placed in the same source file. After the test passes, copy and submit the source code.

Note: the main function needs to return 0

Note: Only use ANSI C/ANSI C++ standards, do not call special functions that depend on the compilation environment or operating system

Note: All dependent functions must be clearly in the source file, #include<xxx> can not be set through the project to ignore common header files

When submitting, pay attention to selecting the desired compiler type

Problem-solving ideas:

This question should be searched according to the structure of the tree in the data structure. Starting from the first grid in the upper left corner, the surrounding grids are traversed in depth until the sum of the values ​​in the grid is equal to half of the total value. Record the number of grids at this time. Finally, find the method with the least number of grids based on the comparison.

Answer source code:

package 一三年省赛真题;

import java.util.Scanner;

public class Year2013_t10 {

	static int[][] g;
	static int[][] sign;
	static int m;
	static int n;
	static int s=0;	//记录格子中元素的总和
	static int answer = Integer.MAX_VALUE;	//最终格子数
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		m = scanner.nextInt();		//输入格子的宽
		n = scanner.nextInt();		//输入格子的高
		g = new int[n][m];	
		sign = new int[n][m];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				g[i][j] = scanner.nextInt();	//为格子赋值
				s+=g[i][j];
			}
		}
		move(0, 0, 0, 0);
		System.out.println(answer);
	}
	
	/**
	 * 记录格子的遍历过程
	 * @param i 移动的横坐标
	 * @param j 移动的纵坐标
	 * @param step 步数
	 * @param sum 格子中元素的总和
	 * */
	public static void move(int i,int j,int step,int sum) {
		//如果该格子坐标不在范围内,或该格子已经走过,则返回
		if (i==n||i<0||j<0||j==m||sign[i][j]==1) {
			return;
		}
		// 如果当前数值和是总和的一半
		if (sum*2==s) {
			answer = Math.min(answer, step);	//对格子数(步数)与符合要求的格子数比较,取出最小值
		}
		sign[i][j] = 1;		//对走过的格子进行标记,表示格子已经走过
		move(i+1, j, step+1, sum+g[i][j]);	//down
		move(i-1, j, step+1, sum+g[i][j]);	//up
		move(i, j-1, step+1, sum+g[i][j]);	//left
		move(i, j+1, step+1, sum+g[i][j]);	//right
		sign[i][j] = 0;		//将该格子重新置于未走过的状态(回溯算法)
		
	}

}
 

Sample output:

 

There are deficiencies or improvements, and I hope my friends will leave a message and learn together!

Interested friends can follow the column!

Little Grey Ape will accompany you to make progress together!

Finally, I am participating in the selection of the 2020 Blog Star, please help me to vote for it!

Vote link: https://bss.csdn.net/m/topic/blog_star2020/detail?username=weixin_44985880

Guess you like

Origin blog.csdn.net/weixin_44985880/article/details/112909679