求两节点的最短通路。对于无权图,能够通过图的广度优先遍历求解。含权图一般通过Dijkstra算法求解。

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;//连接到哪个节点

int weight;//边的权值

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();

//邻接表形式

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(1,1));

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));





//求0号节点開始的全部最小路径

Map map=new HashMap();

while(true){

int min=Integer.MAX_VALUE;//最小路径值

int min_no=-1;//相应节点号

//全部与0号节点相连接的且不在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;

}

}

//与map中点邻接的,全部不在map中的节点(可能经历多个点的距离低于和直接相邻的点的距离)

Iterator  it=map.keySet().iterator();

while(it.hasNext()){

int k=(Integer)it.next();

int w=(Integer)map.get(k);//集合中的节点相应的最小路径值

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);

}

}

结果:{0=2, 1=3, 2=4, 3=5, 4=1, 5=3, 6=6, 7=5, 8=6, 9=7, 10=7}        0到本身的距离这里计算是依照到4,才从4回到0。所以等于2

所谓”遍历”或“枚举”即是要逐一列出全部情况。其要点是要满足两个要求:1不能反复;2不能遗漏。

“不能反复”要求我们在遍历时要有章法,依照某种设计的路线来进行。试探与回溯是最为经常使用的、易于理解的设计思路。

八皇后问题有多个解

public class NoAttack {





/**

* 八皇后问题,这里不须要用8*8的棋盘,必须每一个皇后不在同一行,横竖能够攻击 同一时候不能够在对角线的位置,攻击距离不限

*/





/**

* 检验新皇后放入后,是否冲突

*/

static boolean check(int[] a, int row, int col) {

for (int i = 0; i < row; i++) {

// 纵向上是否冲突

if (col == a[i])

return false;// 与先前皇后的列冲突

// 对角线检验

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();



}

/**

* 对数组放置第K个皇后

*/

static void f(int[] a, int k) {

if (k == 8) {

show(a);

return;// 跳出递归



}

// 对8个位置逐一试探

for (int i = 0; i < 8; i++) {

a[k] = i;

// 将第k个皇后放在第i个位置,进行检查

if (check(a, k, i))

f(a, k + 1);

}

}





public static void main(String[] args) {

int[] a = new int[8];// 记录每行皇后的位置

f(a, 0);

}

}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

Dijkstra含权图最短路径;审判,不要错过枚举退款保证不会重复;国际象棋八皇后问题的更多相关文章

  1. 带权图的最短路径算法(Dijkstra)实现

    一,介绍 本文实现带权图的最短路径算法.给定图中一个顶点,求解该顶点到图中所有其他顶点的最短路径 以及 最短路径的长度.在决定写这篇文章之前,在网上找了很多关于Dijkstra算法实现,但大部分是不带 ...

  2. 图之单源Dijkstra算法、带负权值最短路径算法

    1.图类基本组成 存储在邻接表中的基本项 /** * Represents an edge in the graph * */ class Edge implements Comparable< ...

  3. Dijkstra 算法,用于对有权图进行搜索,找出图中两点的最短距离

    Dijkstra 算法,用于对有权图进行搜索,找出图中两点的最短距离,既不是DFS搜索,也不是BFS搜索. 把Dijkstra 算法应用于无权图,或者所有边的权都相等的图,Dijkstra 算法等同于 ...

  4. Java数据结构——带权图

    带权图的最小生成树--Prim算法和Kruskal算法 带权图的最短路径算法--Dijkstra算法 package graph; // path.java // demonstrates short ...

  5. Dijkstra求解单源最短路径

    Dijkstra(迪杰斯特拉)单源最短路径算法 Dijkstra思想 Dijkstra是一种求单源最短路径的算法. Dijkstra仅仅适用于非负权图,但是时间复杂度十分优秀. Dijkstra算法主 ...

  6. 【数据结构与算法Python版学习笔记】图——最短路径问题、最小生成树

    最短路径问题 概念 可以通过"traceroute"命令来跟踪信息传送的路径: traceroute www.lib.pku.edu.cn 可以将互联网路由器体系表示为一个带权边的 ...

  7. 无向带权图的最小生成树算法——Prim及Kruskal算法思路

    边赋以权值的图称为网或带权图,带权图的生成树也是带权的,生成树T各边的权值总和称为该树的权. 最小生成树(MST):权值最小的生成树. 生成树和最小生成树的应用:要连通n个城市需要n-1条边线路.可以 ...

  8. C语言——无向带权图邻接矩阵的建立

    #include <stdio.h> #include "Graph.h" #define MAX_INT 32767 /* #define vnum 20 #defi ...

  9. BZOJ3438:小M的作物 (最大闭合权图->最小割)

    小M在MC里开辟了两块巨大的耕地A和B(你可以认为容量是无穷),现在,小P有n中作物的种子,每种作物的种子 有1个(就是可以种一棵作物)(用1...n编号),现在,第i种作物种植在A中种植可以获得ai ...

随机推荐

  1. MyBatis Generator插件之SerializablePlugin

    org.mybatis.generator.plugins.SerializablePlugin 在generatorConfig.xml中加上配置: <plugin type="or ...

  2. ASP.NET MVC中实现多个button提交的几种方法

    有时候会遇到这样的情况:在一个表单上须要多个button来完毕不同的功能,比方一个简单的审批功能. 假设是用webform那不须要讨论,但asp.net mvc中一个表单仅仅能提交到一个Action处 ...

  3. php实现求对称二叉树(先写思路,谋而后动)

    php实现求对称二叉树(先写思路,谋而后动) 一.总结 1.先写思路,谋而后动 二.php实现求对称二叉树 题目描述: 请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的 ...

  4. php实现求最小的k个数(日常出错很容易是分号或者$符号忘记写了)

    php实现求最小的k个数(日常出错很容易是分号或者$符号忘记写了) 一.总结 日常出错很容易是分号或者$符号忘记写了 二.php实现求最小的k个数 题目描述 输入n个整数,找出其中最小的K个数.例如输 ...

  5. 【t035】收入计划

    Time Limit: 1 second Memory Limit: 32 MB [问题描述] 高考结束后,同学们大都找到了一份临时工作,渴望挣得一些零用钱.从今天起,Matrix67将连续工作N天( ...

  6. Use Word 2010's Navigation Pane to quickly reorganize documents

    Use Word 2010's Navigation Pane to quickly reorganize documents http://www.techrepublic.com/blog/mic ...

  7. 剔除list中相同的结构体数据

    剔除list中相同的结构体数据,有三个思路:1.两层循环,逐个比较 2.使用set容器来剔除 3.使用unique方法去重 // deduplication.cpp : 定义控制台应用程序的入口点. ...

  8. [React Router v4] Render Nested Routes

    With React Router v4 the entire library is built as a series of React components. That means that cr ...

  9. 【codeforces 602D】Lipshitz Sequence

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  10. 开源 RISC-V 架构正在改变 IoT 处理器的游戏规则

    by Paddy McWilliams, Director of Product Marketing, CEVA   在过去的十年里,开源软件已经成为了科技世界最大的催化剂.现在开源的力量带来了自由发 ...