您的位置:首页 > 其它

路径算法 小熊回家问题

2015-09-24 14:41 211 查看
输入:m n char[][]
m、n代表二维数组维度
例如 B--H
--*-
'B'为起点,'H'为终点
'-'代表可通行,'*'代表不可通
每个节点只能向上下左右四个方向走
输出 所有路径个数以及方案


废话不多说,上代码


import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Stack;

public class Bearhome {
static int count;//方案总数
//存放系统所有节点
private static ArrayList<Node> nodelist = new ArrayList<Node>();
//存放路径中已有节点
private static Stack<Node> nodestack = new Stack<Node>();
//起点
static Node snode = null ;
//终点
static Node enode = null;
//内部类
class Node{
//节点坐标,以及相邻点集
int x;
int y;

ArrayList<Node> nearnode = new ArrayList<Node>();
Node(int x,int y){
this.x = x;
this.y = y;
}

}

public static void main(String[] args){

Bearhome bh = new Bearhome();
//输入
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
char[][] ch = new char[x][y];
for(int i=0;i<x;i++){
ch[i]=sc.next().toCharArray();
}
for(int i=0;i<x;i++){
for(int j=0;j<y;j++){
System.out.print(ch[i][j]+" ");
}
System.out.println();

}
//为数组中每个字符创建节点
for(int i = 0;i < x;i++){
for(int j = 0;j < y;j++){
if(ch[i][j]=='B'){
nodelist.add(snode = bh.new Node(i,j));
}
if(ch[i][j]=='-'){
nodelist.add(bh.new Node(i,j));
}
if(ch[i][j]=='H'){
nodelist.add(enode = bh.new Node(i,j));
}
}
}
//为每个点设置相邻点集
for(int i1 = 0; i1 < nodelist.size();i1++){
for(int j1 = 0; j1 < nodelist.size(); j1++){
if((nodelist.get(j1).x==nodelist.get(i1).x+1&&nodelist.get(j1).y==nodelist.get(i1).y)||
(nodelist.get(j1).x==nodelist.get(i1).x-1&&nodelist.get(j1).y==nodelist.get(i1).y)||
(nodelist.get(j1).y==nodelist.get(i1).y+1&&nodelist.get(j1).x==nodelist.get(i1).x)||
(nodelist.get(j1).y==nodelist.get(i1).y-1&&nodelist.get(j1).x==nodelist.get(i1).x)){
nodelist.get(i1).nearnode.add(nodelist.get(j1));
}
}
}
bh.backtrack(snode);
System.out.print(count);

}
//路径寻找,回溯法
public boolean backtrack(Node node){
Node nnode = null;
if (node!=null){
nodestack.add(node);
int k = 0;
if(node.equals(enode)){
count++;
for(int i = 0; i<nodestack.size()-1;i++)
System.out.print(nodestack.get(i).x+""+nodestack.get(i).y+"-->");
System.out.println(nodestack.get(nodestack.size()-1).x+""+nodestack.get(nodestack.size()-1).y);
return true;
}
else {
nnode = node;
while(nnode!=null){
if ((isNodeInStack(nnode))){
if(k>=node.nearnode.size())
nnode = null;
else {

nnode = node.nearnode.get(k++);
continue;
}
}
if(backtrack(nnode)){
nodestack.pop();
}
if(k>=node.nearnode.size())
nnode = null;
else {

nnode = node.nearnode.get(k++);
}
}
nodestack.pop();
return false;
}
}
else return false;

}

//判断节点是否已在栈中
public static boolean isNodeInStack(Node n){
Iterator<Node> it = nodestack.iterator();
while (it.hasNext()) {
Node node1 = (Node) it.next();
if (n == node1)
return true;
}
return false;
}

}



测试:
console输入: 2 4
B--H
----
结果:
B - - H
- - - -
00-->01-->02-->03
00-->01-->02-->12-->13-->03
00-->01-->11-->12-->02-->03
00-->01-->11-->12-->13-->03
00-->10-->11-->01-->02-->03
00-->10-->11-->01-->02-->12-->13-->03
00-->10-->11-->12-->02-->03
00-->10-->11-->12-->13-->03
8
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: