·您的位置: 首页 » 资源教程 » 编程开发 » JAVA、JSP » JAVA 树中 按最长路径行驶

JAVA 树中 按最长路径行驶

类别: JAVA教程  评论数:0 总得分:0
private Vector findRoad(int posX,int posY,int dir){
Vector roadUPVec=null,roadRIGHTVec=null,roadDOWNVec=null,roadLEFTVec=null;
boolean tempUP=false,tempRIGHT=false,tempLEFT=false,tempDOWN=false;
int upNum=0,downNum=0,rightNum=0,leftNum=0,maxNum=0;

if(posY>0 && m_nowMap[posX][posY-1]!=0 && dir!=DIR_UP){
roadUPVec=new Vector();
roadUPVec.addElement(new Integer(posX));
roadUPVec.addElement(new Integer(posY-1));
Vector tempVec=findRoad(posX,posY-1,DIR_DOWN);
if(tempVec!=null){
for(int i=0;i<tempVec.size();i++)
roadUPVec.addElement(tempVec.elementAt(i));
}
}
else
tempUP=false;
if(posX<COLUMN-1 && m_nowMap[posX+1][posY]!=0 && dir!=DIR_RIGHT){
roadRIGHTVec=new Vector();
roadRIGHTVec.addElement(new Integer(posX+1));
roadRIGHTVec.addElement(new Integer(posY));
Vector tempVec=findRoad(posX+1,posY,DIR_LEFT);
if(tempVec!=null){
for(int i=0;i<tempVec.size();i++)
roadRIGHTVec.addElement(tempVec.elementAt(i));
}
}
else
tempRIGHT=false;
if(posY<ROW && m_nowMap[posX][posY+1]!=0 && dir!=DIR_DOWN){
roadDOWNVec=new Vector();
roadDOWNVec.addElement(new Integer(posX));
roadDOWNVec.addElement(new Integer(posY+1));
Vector tempVec=findRoad(posX,posY+1,DIR_UP);
if(tempVec!=null){
for(int i=0;i<tempVec.size();i++)
roadDOWNVec.addElement(tempVec.elementAt(i));
}
}
else
tempDOWN=false;
if(posX<0 && m_nowMap[posX-1][posY]!=0 && dir!=DIR_LEFT){
roadLEFTVec=new Vector();
roadLEFTVec.addElement(new Integer(posX-1));
roadLEFTVec.addElement(new Integer(posY));
Vector tempVec=findRoad(posX-1,posY,DIR_RIGHT);
if(tempVec!=null){
for(int i=0;i<tempVec.size();i++)
roadLEFTVec.addElement(tempVec.elementAt(i));
}
}
else
tempLEFT=false;
if(!tempUP && !tempRIGHT && !tempDOWN && !tempLEFT)
return null;
else{
if(roadUPVec!=null)
upNum=roadUPVec.size();
if(roadRIGHTVec!=null)
rightNum=roadUPVec.size();
if(roadDOWNVec!=null)
downNum=roadUPVec.size();
if(roadLEFTVec!=null)
leftNum=roadUPVec.size();
maxNum=rightNum;
if(upNum>maxNum)
maxNum=upNum;
if(leftNum>maxNum)
maxNum=leftNum;
if(downNum>maxNum)
maxNum=downNum;

if(maxNum==upNum)
return roadUPVec;
else
if(maxNum==downNum)
return roadDOWNVec;
else
if(maxNum==rightNum)
return roadRIGHTVec;
else
if(maxNum==leftNum)
return roadLEFTVec;
else
return null;
}
}



-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1