如何通过图的方法来寻找一张地图的所有可达路径

package path.plan;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Stack;




public class TestAvailableEx {


/**
* @param args
*/

static private int coorXsignal = 1;


static private int coorYsignal = 1;

static private NodeEx[][] nodeNet;

static private final int TARGET_VALUE=100;

static private Stack<NodeEx> pathStack = new Stack<NodeEx>();

static private ArrayList<Point> pathList = new ArrayList<Point>();

static boolean findFlag=false;

static int mapModel[][] = {    { 0, 0, 1, 1, 1, 0, 0, 0, 1, 0 },
{ 0, 0, 0, 0, 1, 0, 0, 0, 1, 0 },
{ 0, 0, 0, 0, 1, 0, 0, 0, 1, 0 },
{ 0, 0, 1, 0, 1, 0, 0, 0, 1, 0 },
{ 0, 1, 1, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 1, 1, 0, 0, 1, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 1, 0, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 0, 1, 0, 1, 0, 0 },
{ 0, 1, 0, 0, 0, 0, 0, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 1, 0, 1, 0, 0 },
{ 0, 1, 0, 0, 0, 1, 0, 1, 0, 0 },
{ 0, 1, 0, 1, 0, 0, 0, 1, 0, 0 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 100 } };

//map[row][colume]

public static void main(String[] args) {
// TODO Auto-generated method stub
Point myPoint = new Point(0,0);
Point target = new Point(9, 14);

getAvailablePathEx(mapModel,myPoint,target);

System.err.println("OK...");

}


static public ArrayList<Point> getAvailablePathEx(int[][] localMap, Point myPoint, Point target){
nodeNet=createNet(mapModel,myPoint,target);
printPath(null,nodeNet[myPoint.y][myPoint.x],nodeNet[target.y][target.x]);
return pathList;
}

//地图信息;
static private  NodeEx[][] createNet(int[][] localMap, Point myPoint, Point target) {


int startRow = myPoint.y;
int startColumn = myPoint.x;


int row = target.y;
int column = target.x;

int netRowSize=Math.abs(myPoint.y - target.y)+1;

int netColumnSize=Math.abs(myPoint.x - target.x)+1;


coorYsignal = Math.abs(row - startRow) / (row - startRow);


coorXsignal = Math.abs(column - startColumn) / (column - startColumn);


//新建一个二维数组存放一个图,数组[0][0]就是起点,[m][n]即是目标点,这个地方对应关系有点乱,需要仔细考虑一下;
NodeEx[][] nodeNet=new NodeEx[netRowSize][netColumnSize];

for (int i = 0; i < netRowSize; i++)
for (int j = 0; j < netColumnSize; j++) {
nodeNet[i] [j]= new NodeEx();
Point curPt=new Point(myPoint.x+coorXsignal*j,myPoint.y+coorYsignal*i);
nodeNet[i] [j].setPoint(curPt);
nodeNet[i] [j].setValue(getMapValue(localMap,curPt));
}

createNetRelation(nodeNet,netRowSize,netColumnSize);
return nodeNet;
}

static private int getMapValue(int[][] localMap,Point pt){
return localMap[pt.y][pt.x];
}

static private void createNetRelation(NodeEx[][]nodeNet, int row,int column){
for (int i = 0; i < row; i++)
for (int j = 0; j < column; j++) {

ArrayList<NodeEx> relatedNodes=new ArrayList<NodeEx>();
if(isReachable(nodeNet[i][j].getValue())){
int m,n;
//Y+1;
m=i;
n=j+1;
if(n<column&&isReachable(nodeNet[m][n].getValue())){
relatedNodes.add(nodeNet[m][n]);
}
//Y-1;
m=i;
n=j-1;
if(n>=0&&isReachable(nodeNet[m][n].getValue())){
relatedNodes.add(nodeNet[m][n]);
}
//X+1;
m=i+1;
n=j;
if(m<row&&isReachable(nodeNet[m][n].getValue())){
relatedNodes.add(nodeNet[m][n]);
}
//X-1;
m=i-1;
n=j;
if(m>=0&&isReachable(nodeNet[m][n].getValue())){
  relatedNodes.add(nodeNet[m][n]);
}
}
nodeNet[i][j].setRelationNodes(relatedNodes);
}
}


private static boolean isReachable(int value) {
return value==0||value==TARGET_VALUE;
}



private static void printPath(NodeEx lastNode,NodeEx nodeStart,NodeEx nodeTarget){
//首先判断是否出现环路;出现则返回,不再往这条路走下去;
Iterator<NodeEx> pathPt = pathStack.iterator();
while (pathPt.hasNext()) {
NodeEx node = (NodeEx) pathPt.next();
if(node.equals(nodeStart)){
return;
}
}


//没有返回的话,则将这个路径保存
pathStack.push(nodeStart);

//如果这个点就是目标的话,则保存当前路径,返回;
if(nodeStart.equals(nodeTarget)){
findFlag=true;
Iterator<NodeEx> pathPoint = pathStack.iterator();
StringBuilder strApp = new StringBuilder();
while (pathPoint.hasNext()) {
NodeEx node = (NodeEx) pathPoint.next();
Point ele=node.getPoint();
pathList.add(ele);
strApp.append("(" + ele.x + "," + ele.y + ")").append("->");
}
System.err.println(strApp);
System.err.println("pathLenth="+pathStack.size());
return;

}else{
//如果没有找到的话,则继续往前搜索;
if(findFlag==false){
ArrayList<NodeEx> relats=nodeStart.getRelationNodes();

for (Iterator iterator = relats.iterator(); iterator.hasNext();) {
NodeEx nodeEx = (NodeEx) iterator.next();
if(!nodeEx.equals(lastNode)){
printPath(nodeStart,nodeEx,nodeTarget);
}

}

}
}
//这条路走到头了,需要弹出这个Node
pathStack.pop();


}




}





package path.plan;


import java.awt.Point;
import java.util.ArrayList;  
  
/* 表示一个节点以及和这个节点相连的所有节点 */  
public class NodeEx  
{  
    @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((point == null) ? 0 : point.hashCode());
return result;
}


@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
NodeEx other = (NodeEx) obj;
if (point == null) {
if (other.point != null)
return false;
} else if (!point.equals(other.point))
return false;
return true;
}


public Point point = null;  
    
    public String name;
    
    private int value;
    
    public int getValue() {
return value;
}


public void setValue(int value) {
this.value = value;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public ArrayList<NodeEx> relationNodes = new ArrayList<NodeEx>();  
  
    public Point getPoint() {  
        return point;  
    }  
  
    public void setPoint(Point name) {  
        this.point = name;  
    }  
  
    public ArrayList<NodeEx> getRelationNodes() {  
        return relationNodes;  
    }  
  
    public void setRelationNodes(ArrayList<NodeEx> relationNodes) {  
        this.relationNodes = relationNodes;  
    }  
}  

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值