Java string full array problem (classic)

Source: Internet
Author: User

* The original question is as follows: with 1, 2, 2, 3, 4, 6 these six digits, write a main function in Java, print out all the different permutations,
* such as: 612234, 412346, Requirements: "4" can not be in the third place, "3" and "6" can not be connected.
*
*1 the problem to the traversal of the graph structure. In fact, the 6 numbers are six nodes, connecting six nodes into undirected connected graphs, and for each node the traversal path of the graph is obtained,
* The traversal path of all nodes is the result set of the final permutation of these 6 digits.
*2 Obviously this result set has not yet met the subject's requirements. Consider the following:
*1.3,6 can not be connected: the actual requirements of the connected Graph node 3, 5 can not be connected, can be in the structure of the map to meet the change conditions, and then traverse the diagram.
*2. Cannot be duplicated: Considering that there are two 2, there will obviously be duplicate results, and the result set can be placed in TreeSet to filter the duplicate results
*3.4 cannot be in third place: The result that satisfies this condition is still removed from the result set.

Import Java.util.Iterator;
Import Java.util.TreeSet;

public class Sort {

Private string[] B = new string[] {"1", "2", "2", "3", "4", "6"};

private int n = b.length;

Private boolean[] visited = new Boolean[n];

Private int[][] A = new int[n][n];

Private String result = "";

Private treeset<string> set = new treeset<string> ();

public static void Main (string[] args) {
New Sort (). Start ();
}

private void Start () {

Initial the map a[][]
for (int i = 0; i < n; i++) {
for (int j = 0; J < N; j + +) {
if (i = = j) {
A[I][J] = 0;
} else {
A[I][J] = 1;
}
}
}

3 and 5 can not be the neighbor.
A[3][5] = 0;
A[5][3] = 0;

Begin to depth search.
for (int i = 0; i < n; i++) {
This.depthfirstsearch (i);
}

Print result TreeSet.
Iterator it = Set.iterator ();
while (It.hasnext ()) {
String string = (string) it.next ();
System.out.println (string);
}
}

private void Depthfirstsearch (int startIndex) {
Visited[startindex] = true;
result = result + B[startindex];
if (result.length () = = N) {
"4" Can not be the third position.
if (Result.indexof ("4")!= 2) {
Filt the duplicate value.
Set.add (result);
}
}
for (int j = 0; J < N; j + +) {
if (a[startindex][j] = = 1 && visited[j] = = False) {
Depthfirstsearch (j);
}
}

        //Restore the result value and visited value after listing a NODE.
         result = result.substring (0, Result.length ()-1);
         Visited[startindex] = false;
    }
}
 

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.