⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tansuo.java

📁 数据结构---堆栈的使用。蓝色小方块在一个自己添加的红色迷宫中找到一个出口。利用数据结构堆栈的特性来。
💻 JAVA
字号:
package Lily;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Tansuo extends Applet implements MouseListener,Runnable {
    ZuJian z1=new ZuJian(0,0);
    Tansuozhe tan=new Tansuozhe(30,30);
    int w=900;
    int h=600;
    int x,y;
    int dx,dy;
    int gex,gey;
    int nowx=1;
    int nowy=1;
    boolean biaozhi[][]=new boolean[30][20];
    boolean biaozhi1[][]=new boolean[30][20];
    Stack jilu=new Stack();
    Thread newThread;
   // ArrayList b=new ArrayList();
    public void init(){
    	setLayout(null);
    	setSize(w,h);
    	setBackground(Color.white);
    	add(tan); biaozhi[1][1]=true;biaozhi1[1][1]=true;
    	tan.setLocation(30,30);
    	addMouseListener(this);
    }
    
    public void start(){
    newThread=new Thread(this);
    newThread.start();
    }
    
   public void stop(){
	   newThread=null;
   }
    
 public void run(){
	 while(newThread!=null){
		if(biaozhi1[nowx+1][nowy]==false){
			int j[]=new int[2];
			j[0]=nowx;j[1]=nowy;
			jilu.push(j);
			
			nowx=nowx+1; 
			tan.setLocation(30*nowx,30*nowy);biaozhi1[nowx][nowy]=true;
			try{
				newThread.sleep(300);
			}catch(InterruptedException e){}
		}
		 
		else if(biaozhi1[nowx][nowy+1]==false){
			int j[]=new int[2];
			j[0]=nowx;j[1]=nowy;
			jilu.push(j);
			
			nowy=nowy+1; 
			tan.setLocation(30*nowx,30*nowy);biaozhi1[nowx][nowy]=true;
			try{
				newThread.sleep(300);
			}catch(InterruptedException e){}	
		}
		 
		else if(biaozhi1[nowx-1][nowy]==false){
			int j[]=new int[2];
			j[0]=nowx;j[1]=nowy;
			jilu.push(j);
			
			nowx=nowx-1; 
			tan.setLocation(30*nowx,30*nowy);biaozhi1[nowx][nowy]=true;
			try{
				newThread.sleep(300);
			}catch(InterruptedException e){}
		}
		else if(biaozhi1[nowx][nowy-1]==false){
			int j[]=new int[2];
			j[0]=nowx;j[1]=nowy;
			jilu.push(j);
			
			nowy=nowy-1; 
			tan.setLocation(30*nowx,30*nowy);biaozhi1[nowx][nowy]=true;
			try{
				newThread.sleep(300);
			}catch(InterruptedException e){}
		}
		else {
			int k[]=new int[2];
			k=(int[])jilu.pop();
			nowx=k[0];
			nowy=k[1];
			tan.setLocation(30*nowx,30*nowy);
			try{
				newThread.sleep(20);
			}
		catch(InterruptedException e){}
		 
		 
	 }
 }
 }   
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
	public void mousePressed(MouseEvent e){
		while(jilu.empty()!=true){
			jilu.pop();

		}		
		for(int i=0;i<30;i++){
			for(int j=0;j<20;j++){
				biaozhi1[i][j]=false;
			}
		}
			
		for(int i=0;i<30;i++){
			for(int j=0;j<20;j++){
			biaozhi1[i][j]=biaozhi[i][j];	
			}
		}
			
			
			
			
			
			
			
			
			
			
			
			
			
			
		x=e.getX();y=e.getY();
		ZuJian p=new ZuJian(x,y);
		dx=x%30;dy=y%30;
		int u=x-dx;int v=y-dy;
		gex=u/30;gey=v/30;               //System.out.println("baiozhi["+gex+"]"+"["+gey+"]="+biaozhi[gex][gey]);
		if(biaozhi[gex][gey]==false){
		add(p);
		p.setLocation(u,v);
	   // b.add(p); 
	    biaozhi[gex][gey]=true; biaozhi1[gex][gey]=true;   // System.out.println("baiozhi["+gex+"]"+"["+gey+"]="+biaozhi[gex][gey]);
	    }
	   /* int k=getComponentCount();
	      Component[] xilie=getComponents();                                                        
	     for(int i=0;i<k;i++) {                                                  
	         ZuJian ppp=(ZuJian)xilie[i];
	        
	    	 if(ppp.click==true) {                                                        
	    	                                                        
	          remove(ppp) ;
	        biaozhi[gex][gey]=false; }                                                     
	                                           
	                                                            
	                }*/
		
		
		
	      }
		
	
	public void mouseReleased(MouseEvent e){}
	public void mouseClicked(MouseEvent e){}
	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e){}	
	
}













class ZuJian extends Button  {// implements MouseListener{
	int w=30;
	int h=30;
	int x,y;
	//boolean click=false;
	ZuJian(int x,int y  ){
		setSize(w,h);
		setBackground(Color.red);
		this.x=x;this.y=y;
	//	addMouseListener(this);
		
	}
	
	/*public void mousePressed(MouseEvent e){
		 if( click==true)
			 click=false;
		 else
			 click=true;
	}
	public void mouseReleased(MouseEvent e){}
	public void mouseClicked(MouseEvent e){}
	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e){}*/
}

class Tansuozhe extends Button{
	int w=30;int h=30;
	int x,y;
	Tansuozhe(int x,int y){
		super("?&*%#");
		setSize(w,h);
		setBackground(Color.blue);
		this.x=x;this.y=y;
	}
}






⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -