JAVA 树中 按最长路径行驶
类别: JAVA教程
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;
}
}
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;
}
}
- 上一篇: Java Media简介
- 下一篇: java defunct产生的原因和解决办法
-= 资 源 教 程 =-
文 章 搜 索