package testss;
import java.lang.reflect.Array;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

import javafx.animation.Animation;
import javafx.animation.PathTransition;
import javafx.animation.SequentialTransition;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.stage.*;
import javafx.scene.*;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.text.*;
import javafx.scene.layout.Pane;
import javafx.scene.shape.StrokeLineCap;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
public  class app extends Application
{
	Rectangle rec=new Rectangle(0,0,30,30);
	int maze[][]=new int[500][500];
	int vis[][]=new int[500][500];
	int dir[][]= {{-30,0},{30,0},{0,-30},{0,30},{-30,-30},{-30,30},{30,-30},{30,30}};
	node f[][]=new node [500][500];
	
	SequentialTransition link=new SequentialTransition();
	public void start(Stage stage)throws Exception
	{
		for(int i=0;i<500;i+=30) {
			for(int j=0;j<500;j+=30) {
		         maze[i][j]=(int)((Math.random()*100)%2);
				 //maze[i][j]=0;
			}
		}
		for(int i=0;i<500;i+=30) {
			for(int j=0;j<500;j+=30) {
				if(i==2*j||3*j==i||i==j||i+j==60)maze[i][j]=0;
			}
		}
		
		Pane pane=new Pane();
		
		for(int i=0;i<500;i+=30) {
			for(int j=0;j<500;j+=30) {
				Rectangle r=new Rectangle(i,j,30,30);
				if(maze[i][j]==1) {
					r.setFill(Color.PINK);
				}else {
					r.setFill(Color.YELLOW);
				}
				pane.getChildren().add(r);
			}
		}
		rec.setFill(Color.BLACK);
		pane.getChildren().add(rec);
		link.setNode(rec);
		Queue<node> queue = new ArrayBlockingQueue<node>(1000);
		node e=new node();
		e.set(0,0);
		queue.add(e);
		vis[0][0]=1;
		while(!queue.isEmpty()) {
			node now=queue.remove();
			for(int i=0;i<4;i++) {
				int fx=now.x+dir[i][0];
				int fy=now.y+dir[i][1];
				if((inside(fx,fy)&&(vis[fx][fy]==0)&&maze[fx][fy]==0)) {
					vis[fx][fy]=1;
					f[fx][fy]=new node(now.x,now.y);
					queue.add(new node(fx,fy));
				}
				if(fx==480&&fy==480){
					node ans[]=new node[1000];
					int cnt=0;int t1,t2;
					ans[cnt]=new node(fx,fy);
					
					while(f[fx][fy].x!=0||f[fx][fy].y!=0){
						t1=fx;t2=fy;//这里注意一下 
						cnt++;
						ans[cnt]=new node(f[fx][fy].x,f[fx][fy].y);
						fx=f[t1][t2].x;//如果不用中间变量会导致下一行出问题 
						fy=f[t1][t2].y;
					}
					ans[++cnt]=new node(0,0);
					for(int l=cnt;l>=0;l--) {
						System.out.println("x: "+ans[l].x+" y:"+ans[l].y);
						TranslateTransition tt=new TranslateTransition();
						if(l>=1) {
							tt.setFromX(ans[l].x);
							tt.setToX(ans[l-1].x);
							tt.setFromY(ans[l].y);
							tt.setToY(ans[l-1].y);
						}else {
							tt.setFromX(ans[l].x);
							tt.setToX(ans[l].x);
							tt.setFromY(ans[l].y);
							tt.setToY(ans[l].y);
						}
						
						link.getChildren().add(tt);
					}
					
					break;
				} 
			}
		}
		link.play();
		Scene scene=new Scene(pane,500,500);
		stage.setScene(scene);
		stage.centerOnScreen();
		stage.setTitle("BFS迷宫最短路径--李杰");
		stage.show();
	}

	static boolean inside(int fx, int fy) {
		return (fx>=0&&fx<=499&&fy>=0&&fy<=499);
	}
	
}
package testss;

public class node {
	public int x,y;
	node(){
		
	}
	node(int a,int b){
		x=a;y=b;
	}
	void set(int a,int b){
		x=a;y=b;
	}
}

package testss;

import javafx.application.Application;

public class testss {
	public static void main(String[] args) 
	{
		Application.launch(app.class,args);
	}
}