游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1397|回复: 0

偶的Java版A*算法源程序

[复制链接]

13

主题

54

帖子

58

积分

注册会员

Rank: 2

积分
58
发表于 2005-3-1 22:33:00 | 显示全部楼层 |阅读模式
/***********************************************\
*
*
*                A*算法的Java版本
*
*
*                作者:Vic
*                QQ:307042976
*
*
*
\***********************************************/
//好不容易玩了,谨奉此。
import java.util.*;
import java.awt.*;
import java.io.*;

class point extends Point{
        int h;                //到本点的步数
        point father;        //前节点
        public point(int nx,int ny,int nh,point p){
                super(nx,ny);
                h=nh;
                father=p;
        }
        public point(point p){
                x=p.x;
                y=p.y;
                h=p.h;
                father=p.father;
        }
        public Point getPoint(){
                return new Point(x,y);
        }
}

class Astar {
        BitSet                        map;
        int                                width;
        int                                height;
       
        Point                        s;
        Point                        e;
       
        Astar(){
        }
       
        //The G function of A*.
        int                                f(point p)        {
                return  (Math.abs(p.x-e.x)+Math.abs(p.y-e.y)+p.h);
        }
        //Final step,Get Way.
        Stack                        getWay(point p){
                Stack way=new Stack();
                while(p.father!=null){
                        way.push(p.getPoint());
                        p=p.father;
                }
                way.push(p.getPoint());
                return way;
        }
        //Juge point.
        boolean                        isOK(Point p)        {
                if(p.x>=width || p.x<0)
                        return false;
                if(p.y>=height || p.y<0)
                        return false;
                if(map.get(p.y*width+p.x))
                        return false;
                return true;
        }
        //Insert at the right position.
        void                        InsertP2V(point p,Vector v){
                if(v.isEmpty()){
                        v.addElement(p);
                        return;
                }
                if(v.size()==1){
                        if(f((point)(v.elementAt(0)))<f(p))
                                v.addElement(p);
                        else
                                v.insertElementAt(p,0);
                        return;
                }
                for(int i=0;i<v.size()-1;i++){
                        //Find the right positon to insert.
                        if(f((point)(v.elementAt(i)))<=f(p) && f(p)<=f((point)(v.elementAt(i+1)))){
                                v.insertElementAt(p,i+1);
                                return;
                        }
                        v.addElement(p);
                }
        }
        //The main function of A*.
        Stack                        findWay()
        {
                Vector open=new Vector(100);
                Vector close=new Vector(100);
                open.addElement(new point(s.x,s.y,0,null));
               
                while(!open.isEmpty()){
                        point p1=new point((point)open.elementAt(0));
                        open.removeElementAt(0);
                       
                        if(p1.equals(e)){
                                return getWay(p1);
                        }
                       
                        int nh=p1.h+1;
                        point[] p2=new point[4];
                        p2[0]=new point(p1.x-1,p1.y,nh,p1);
                        p2[1]=new point(p1.x+1,p1.y,nh,p1);
                        p2[2]=new point(p1.x,p1.y-1,nh,p1);
                        p2[3]=new point(p1.x,p1.y+1,nh,p1);
                       
                        for(int i=0;i<4;i++){
                                if(!isOK(p2)){
                                        continue;
                                }
                                int I_O=open.indexOf(p2);
                                int I_C=close.indexOf(p2);
                                int f=f(p2);
                               
                                if(I_O<0 && I_C<0){//If haven't visited:
                                        InsertP2V(p2,open);
                                }
                                else if(I_O>=0){//If it's in the open:
                                        if(nh<((point)open.elementAt(I_O)).h){
                                                open.removeElementAt(I_O);
                                                InsertP2V(p2,open);
                                        }
                                }
                                else{//If it's in the Close:
                                        if(nh<((point)close.elementAt(I_C)).h){
                                                close.removeElementAt(I_C);
                                                InsertP2V(p2,open);
                                        }
                                }
                        }
                        close.addElement(p1);
                }
                return null;
        }
        //Main Test:
        public static void main(String[]args) throws IOException{
                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                System.out.println("Width & Height");
                int w=Integer.parseInt(br.readLine());
                int h=Integer.parseInt(br.readLine());
                BitSet bs=new BitSet(w*h);
                System.out.println("Map Data:");
                for(int i=0;i<w*h;i++){
                        if(Integer.parseInt(br.readLine())!=0)
                                bs.set(i);
                }
                System.out.println("SXSYEXEY");
                int sx=Integer.parseInt(br.readLine()),
                        sy=Integer.parseInt(br.readLine()),
                        ex=Integer.parseInt(br.readLine()),
                        ey=Integer.parseInt(br.readLine());
                Point start=new Point(sx,sy);
                Point end  =new Point(ex,ey);
               
                Astar as=new Astar();
                as.width=w;
                as.height=h;
                as.map=bs;
                as.s=start;
                as.e=end;
                System.out.println(as.findWay());
        }
}

[em16]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-12-24 12:51

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表