Java brute force recursive backtracking algorithm

Source: Internet
Author: User

Today this problem is I have been trying to solve, remember the first time in the Blue Bridge Cup of the class, that is, a high number of years in the simulation test, the afternoon to the Blue Bridge Cup class, encountered this problem, then wrote, there is no train of thought, and then to a freshman's mock examination went. Impressive Ah, has not written out. Let's take a look at the topic first.

1. Description of the problem:
As shown in the number triangle, write a program that calculates a path from the top to the bottom of a certain place, making the path number and maximum, output path and maximum value.

7

3 8

8 1 0

2 7 4 4

4 5 2) 6 5

Of course, what is a path, the path is can be connected, but can not skip, such as 7-3-8-7-2 is a path.

2. Enter:

5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5

3. Output:

Path: 7-3-8-7-5, Maximum: 30

4. Algorithm thought: This question ah, actually and horse walk Day thought very much resembles, first from 0,0 this position embarks, has traveled throughout the board (regards the whole as a chessboard)

7

3 8

8 1 0

2 7 4 4

4 5 2) 6 5

The chessboard moved over to form such a shape. I enter in the qipan[][] array, and then use a temporary array temp[][] and the size of the chessboard, the temporary array is all initialized to 0, take a step to update the array to 1 (imagined as horse walking day) all go to the end, then traverse temp[][] Array, if temp[][] Not 0, that is, to go, then output the chessboard on the board of the corresponding number. Use the variable sum, and MAXV to record the maximum value, when the maximum value to save the state, that is, the maximum output walk is what step. Finally I use a hashmap to save all the values, in the main function, if the value of HashMap = MAXV, then the output, and finally get the result. This kind of violence solution is in the case of the data quantity is not many, but the data quantity is many, uses the dynamic plan to solve.

5. code example:

Package com.zzl.zt;

/*
* 7
* 3 8
* 8 1 0
* 2 7 4 4
* 4 5 2 6 5
*/
Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.Map;
Import Java.util.Map.Entry;
Import Java.util.Scanner;

public class Routetest {
static int qipan[][] = new INT[20][20];
static int temp[][] = new INT[20][20];
static int weizhi[][] = {{1,0},{1,1}}; Just go down, or go right down.
static int step = 1;
static int sum = 0;
static int maxv = 0;
Static Map<stringbuffer, integer> Map = new Hashmap<stringbuffer, integer> ();


public static void Main (string[] args) {
Scanner SCN = new Scanner (system.in);
System.out.println ("Please enter a total number of lines and enter data:");
int n = scn.nextint (); Total number of rows
for (int i=0;i<n;i++) {
for (int j=0;j<=i;j++) {
QIPAN[I][J] = Scn.nextint ();
TEMP[I][J] = 0;
}
}
TEMP[0][0] = step;
step++;
Move (0,0,n); Pass the number of n rows in, or you don't know the number of rows

Traverse the Map collection, once output, as a sample can be
Iterator it = Map.entryset (). Iterator ();
while (It.hasnext ()) {
Entry Entry = (Entry) it.next ();
if (entry.getvalue () = = Integer.valueof (MAXV)) {
System.out.println (Entry.getkey () + "Maximum value:" + entry.getvalue ());
}
}
}
public static void Move (int x, int y, int n) {
int next_x = 0;
int next_y = 0;
if (step>n) {//recursion to the bottom
sum = 0;
StringBuffer sb = new StringBuffer ();
Sb.append ("Path:");
for (int i=0;i<n;i++) {
for (int j=0;j<=i;j++) {
if (Temp[i][j]!=0) {///That means there's been a walk.
This is a representation of the output
if (i==n-1) {
Sb.append (Qipan[i][j] + ",");
}else{
Sb.append (Qipan[i][j] + "-");
}
Calculate the total value of this trip sum
sum = Sum+qipan[i][j];
}
}
}
Determine if sum is larger or larger, update the data
if (SUM&GT;MAXV) {
MAXV = sum;
}
Map.put (SB, sum);
}else{
for (int i=0;i<2;i++) {//Only two locations can walk, and both positions can go, no restrictions
next_x = x+weizhi[i][0];
next_y = y+weizhi[i][1];
TEMP[NEXT_X][NEXT_Y] = step;
step++;
Move (next_x, next_y, N);
Temp[next_x][next_y] = 0;
step--;
}
}
}
}

Java brute force recursive backtracking algorithm

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.