Dijkstra the shortest path of the weighted graph; the trial, don't miss the enumeration money guarantee will not be repeated; chess eight queens question

Source: Internet
Author: User

The shortest path for two nodes. For an unauthorized graph, it can be solved by the breadth-first traversal of graphs. The weighted graphs are generally solved by Dijkstra algorithm.

Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;
public class Shortest {
Static Class cell{
int node;//which node to connect to
The weight of the int weight;//edge
Public Cell (int node,int weight) {
This.node=node;
This.weight=weight;
}
}
@SuppressWarnings ("Unchecked")
public static void Main (string[] args) {
List[] G=new list[11];
for (int i=0;i<g.length;i++) g[i]=new ArrayList ();
adjacency Table Form
G[0].add (New Cell (1,3));
G[0].add (New Cell (4,1));
G[1].add (New Cell (2,1));
G[1].add (New Cell (6,3));
G[1].add (New Cell (9,4));
G[1].add (New Cell (5,5));
G[1].add (New Cell (0,3));
G[2].add (new Cell);
G[2].add (New Cell (3,1));
G[2].add (New Cell (6,7));
G[3].add (New Cell (2,1));
G[3].add (New Cell (10,2));
G[4].add (new Cell (0,1));
G[4].add (New Cell (5,2));
G[5].add (New Cell (4,2));
G[5].add (new Cell (1,5));
G[5].add (New Cell (7,2));
G[5].add (New Cell (8,3));
G[6].add (new Cell (2,3));
G[6].add (New Cell (3,7));
G[6].add (New Cell (8,2));
G[6].add (New Cell (10,1));
G[7].add (New Cell (5,2));
G[8].add (New Cell (5,3));
G[8].add (New Cell (6,2));
G[9].add (New Cell (1,4));
G[9].add (New Cell (10,2));
G[10].add (New Cell (3,2));
G[10].add (New Cell (6,1));
G[10].add (New Cell (9,2));


Find all the minimum paths starting at node No. 0
Map map=new HashMap ();
while (true) {
int min=integer.max_value;//Minimum Path value
int min_no=-1;//corresponding node number
All connected to node No. 0 and not in map
for (int i=0;i<g[0].size (); i++) {
Cell t= (cell) g[0].get (i);
if (Map.get (T.node) ==null&&t.weight<min) {
Min_no=t.node;
Min=t.weight;
}
}
Nodes that are not in the map at the midpoint of the map, which may experience multiple points less than the distance from the directly adjacent points
Iterator It=map.keyset (). Iterator ();
while (It.hasnext ()) {
int k= (Integer) it.next ();
int w= (Integer) map.get (k);//The corresponding minimum path value for the node in the collection
for (int i=0;i<g[k].size (); i++) {
Cell t= (cell) g[k].get (i);
if (Map.get (T.node) ==null&&t.weight+w<min) {
Min_no=t.node;
Min=t.weight+w;
}
}
}
if (Min<integer.max_value) {
Map.put (min_no,min);
}
else{
Break
}
}
System.out.print (map);
}
}

Results: {0=2, 1=3, 2=4, 3=5, 4=1, 5=3, 6=6, 7=5, 8=6, 9=7, 10=7} 0 The distance to itself is calculated according to 4, only from 4 back to 0. So equals 2.


The so-called "traversal" or "enumeration" is a list of all cases. The main point is to meet two requirements: 1 cannot be repeated; 2 cannot be omitted.

"Cannot be repeated" requires that we should be methodical in our traversal, according to some design route. Heuristics and backtracking are the most frequently used and easy-to-understand design ideas.

The eight Queens question has multiple solutions.


public class Noattack {


/**
* Eight queens question, here does not need to use the 8*8 chess board, must each Empress not the same line, can attack the same time not to be able in the diagonal position, the attack distance not limited
*/


/**
* Check if the new queen has been put into conflict
*/
Static Boolean check (int[] A, int row, int col) {
for (int i = 0; i < row; i++) {
Whether the vertical conflict
if (col = = A[i])
Return false;//conflict with previous queen's column
Diagonal check
if (row-i = = Math.Abs (Col-a[i]))
return false;
}
return true;
}


static void Show (int[] a) {
for (int i=0;i<a.length;i++) {
System.out.print (a[i]+ "");
}
System.out.println ();

}
/**
* Place K queens on an array
*/
static void F (int[] A, int k) {
if (k = = 8) {
Show (a);
return;//jumps out of recursion

}
Test each of the 8 locations
for (int i = 0; i < 8; i++) {
A[k] = i;
Place the K queen in the first place, and check
if (check (a, k, i))
F (A, k + 1);
}
}


public static void Main (string[] args) {
Int[] A = new int[8];//record the position of the Queen in each row
F (A, 0);
}
}

Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

Dijkstra the shortest path of the weighted graph; the trial, don't miss the enumeration money guarantee will not be repeated; chess eight queens question

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.