java实现走迷宫
来源:互联网 发布:无限网络 封优酷 编辑:程序博客网 时间:2024/05/21 17:15
package cn.heima;
import java.util.LinkedList;
import java.util.Scanner;
/**
* 本程序主要运用递归思想,
* 为了防止走回头路或者走死循环我就把它所经过的路都修改为1及不能再走
* 由于其要走完所有可能所以要走过后回来
* 有4个方向
* @author Administrator
*
*/
class Point{//定义坐标里面有x,y
public int x;
public int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Point() {
}
}
public class Demomg {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入坐标数用空格分开:");
int a = sc.nextInt();
int b= sc.nextInt();
Point start =new Point(0,0);
Point end = new Point(a,b);
int[][] maze = null;
LinkedList<Point> list = new LinkedList<Point>();
maze=init();//初始化迷宫
long startTime = System.currentTimeMillis();
findWay(list,maze,start,end);//开始寻找出口
long endTime = System.currentTimeMillis();
System.out.println("用时为"+(endTime-startTime)/1000f+"秒");
}
public static void findWay(LinkedList<Point> list, int[][] maze, Point start,Point end) {
// TODO Auto-generated method stub
if(start.x==end.x&&start.y==end.y){//如果现在位子等于终点位子输出
printOut(maze,list);
return; //结束这一步
}else{
for(int i=0;i<4;i++){//有4个方向使其都可以走到
Point p2 = dothere(i,start,maze);//试探的在某个方向上走动
if(p2==null&&i!=3)//走不了但还有几个或一个方向没试探
continue;
if(p2==null&&i==3){//走不了并且所有的方向都试探了
return;
}
list.addLast(p2);
findWay(list,maze,p2,end);//走的了在开始下一步走
if(list.size()!=0){//把原先该变走过得路径还原
Point ppt=list.removeLast();
maze[ppt.x][ppt.y]=0;
}
}
}
}
private static void printOut(int[][] maze,LinkedList<Point> list) {
System.out.print("("+0+","+0+")");
for(Point p:list){
System.out.print("-->"+"("+p.x+","+p.y+")");
}
System.out.println();
return;
}
public static int[][] init() {
// TODO Auto-generated method stub
int[][] arr={
{0,1,0,1,0,0,0},
{0,0,1,0,0,1,0},
{1,0,0,1,0,1,0},
{0,0,1,0,0,1,0},
{0,0,1,1,0,1,0},
{1,0,0,0,0,0,0}
};
return arr;
}
public static Point dothere(int i,Point p,int[][] arr){
boolean flag = false;
int x=p.x,y=p.y;
if(i==0){ //向着走试探走
if(x-1>=0&&arr[x-1][y]==0){//走的了
arr[x][y]=1; //把上一步路径设为不能走
x--; //走动
}else{
flag = true; //不能走则将不能走标记设为true
}
}else if(i==1){
if(y-1>=0&&arr[x][y-1]==0){
arr[x][y]=1;
y--;
}else{
flag = true;
}
}else if(i==2){
if(x+1<arr.length&&arr[x+1][y]==0){
arr[x][y]=1;
x++;
}else{
flag = true;
}
}else if(i==3){
if(y+1<arr[1].length&&arr[x][y+1]==0){
arr[x][y]=1;
y++;
}else{
flag = true;
}
}
if(flag) return null; //如果不能走返回null
return new Point(x,y);//如果能走返回新的坐标
}
}
import java.util.LinkedList;
import java.util.Scanner;
/**
* 本程序主要运用递归思想,
* 为了防止走回头路或者走死循环我就把它所经过的路都修改为1及不能再走
* 由于其要走完所有可能所以要走过后回来
* 有4个方向
* @author Administrator
*
*/
class Point{//定义坐标里面有x,y
public int x;
public int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Point() {
}
}
public class Demomg {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入坐标数用空格分开:");
int a = sc.nextInt();
int b= sc.nextInt();
Point start =new Point(0,0);
Point end = new Point(a,b);
int[][] maze = null;
LinkedList<Point> list = new LinkedList<Point>();
maze=init();//初始化迷宫
long startTime = System.currentTimeMillis();
findWay(list,maze,start,end);//开始寻找出口
long endTime = System.currentTimeMillis();
System.out.println("用时为"+(endTime-startTime)/1000f+"秒");
}
public static void findWay(LinkedList<Point> list, int[][] maze, Point start,Point end) {
// TODO Auto-generated method stub
if(start.x==end.x&&start.y==end.y){//如果现在位子等于终点位子输出
printOut(maze,list);
return; //结束这一步
}else{
for(int i=0;i<4;i++){//有4个方向使其都可以走到
Point p2 = dothere(i,start,maze);//试探的在某个方向上走动
if(p2==null&&i!=3)//走不了但还有几个或一个方向没试探
continue;
if(p2==null&&i==3){//走不了并且所有的方向都试探了
return;
}
list.addLast(p2);
findWay(list,maze,p2,end);//走的了在开始下一步走
if(list.size()!=0){//把原先该变走过得路径还原
Point ppt=list.removeLast();
maze[ppt.x][ppt.y]=0;
}
}
}
}
private static void printOut(int[][] maze,LinkedList<Point> list) {
System.out.print("("+0+","+0+")");
for(Point p:list){
System.out.print("-->"+"("+p.x+","+p.y+")");
}
System.out.println();
return;
}
public static int[][] init() {
// TODO Auto-generated method stub
int[][] arr={
{0,1,0,1,0,0,0},
{0,0,1,0,0,1,0},
{1,0,0,1,0,1,0},
{0,0,1,0,0,1,0},
{0,0,1,1,0,1,0},
{1,0,0,0,0,0,0}
};
return arr;
}
public static Point dothere(int i,Point p,int[][] arr){
boolean flag = false;
int x=p.x,y=p.y;
if(i==0){ //向着走试探走
if(x-1>=0&&arr[x-1][y]==0){//走的了
arr[x][y]=1; //把上一步路径设为不能走
x--; //走动
}else{
flag = true; //不能走则将不能走标记设为true
}
}else if(i==1){
if(y-1>=0&&arr[x][y-1]==0){
arr[x][y]=1;
y--;
}else{
flag = true;
}
}else if(i==2){
if(x+1<arr.length&&arr[x+1][y]==0){
arr[x][y]=1;
x++;
}else{
flag = true;
}
}else if(i==3){
if(y+1<arr[1].length&&arr[x][y+1]==0){
arr[x][y]=1;
y++;
}else{
flag = true;
}
}
if(flag) return null; //如果不能走返回null
return new Point(x,y);//如果能走返回新的坐标
}
}
1 0
- java实现走迷宫
- java实现走迷宫算法
- 走迷宫回溯算法(Java实现)
- c++实现走迷宫
- DFS实现走迷宫
- 【数据结构】走迷宫实现
- java走迷宫
- 用遗传算法走迷宫 [Java语言实现]
- 老鼠走迷宫 (栈实现)
- 用栈实现走迷宫
- Java - Nuc - 走迷宫 ( bfs )
- 最近都在纠结走迷宫啊~~java走迷宫,会回滚的走迷宫。。。
- 用栈实现的自动走迷宫
- python 使用分水岭算法实现走迷宫
- 算法:Python递归实现走迷宫
- 【c++】利用递归实现走迷宫
- C++实现简单的走迷宫
- 一道走迷宫算法题python实现
- SSH Secure File Transfer上传文件错误:encountered 1 errors during the transfer解决办法
- 实验二 分支程序实验和循环程序实验
- android 在eclipse中使用RecyclerView
- java path classpath JAVA_HOME
- 2016/3/14 开始自己的编程之路
- java实现走迷宫
- 统计难题
- LeetCode题解-6. ZigZag Conversion
- 关于代理、通知和KVO区别
- Valid Parentheses
- python show-me-the-code 第0004
- Android中5大数据存储(一)---SharedPreferences存储
- 在vs2008中怎么把一个项目复制到另一个项目中
- maven 快照