java实现走迷宫算法
2015-11-27 11:47
615 查看
代码实现了 读取文件中迷宫地图,打印迷宫地图并找到一条出口。第一次写java代码,留着纪念。
用一个文件存储 N*N的迷宫地图,E代表的是入口,X代表的是出口、*代表可以走,1代表不可以走。每次在原坐标的基础上,搜寻上下左右四个方向的路径,是否可行。直到找到出口。找到一条路径就结束,最后打印一条路径(不一定是最优路径)。
6
1 * E 1 * *
1 * * * * *
1 * 1 * * X
* * 1 1 * 1
* 1 * * * *
* * * 1 1 1
代码如下:
用一个文件存储 N*N的迷宫地图,E代表的是入口,X代表的是出口、*代表可以走,1代表不可以走。每次在原坐标的基础上,搜寻上下左右四个方向的路径,是否可行。直到找到出口。找到一条路径就结束,最后打印一条路径(不一定是最优路径)。
6
1 * E 1 * *
1 * * * * *
1 * 1 * * X
* * 1 1 * 1
* 1 * * * *
* * * 1 1 1
代码如下:
</pre><pre name="code" class="java">package com.maze.path; import java.util.Stack; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; class Step{ int x,y; public Step(int x,int y) { this.x = x; this.y = y; } } public class Path { private int inx,iny; //the location of Entrance and Exit // private char map[][]; static int dir[][] = {{0,1},{0,-1},{1,0},{-1,0}}; //4 directories each point static int N; //maze size=N*N public static char[][] readMap(String filePath){ try { String encoding="GBK"; File file=new File(filePath); if(file.isFile() && file.exists()){ InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding); BufferedReader bReader = new BufferedReader(read); String lineTxt = null; String array = ""; while((lineTxt = bReader.readLine()) != null){ array += lineTxt; } read.close(); array = array.replace(" ", ""); int i = 0; int n = Integer.parseInt(array.charAt(i++)+""); Path.N = n; //System.out.println("n:6 "+n+"i:0 "+i); char[][] cArray = new char ; for(int col=0;col<6;col++){ for(int row=0;row<6;row++){ char a = array.charAt(i); cArray[col][row] = a; i++; //System.out.println(i+"a:"+a); if(i>array.length()) { System.out.println("Sorry,this maze is not N*N standard size"); break; } } } return cArray; } else { System.out.println("can't find the file"); } } catch (Exception e) {// readLine() method must handle java.io.IOException Exception System.out.println("run error"+e.getMessage()); e.printStackTrace(); } return null; } public static void main(String[] args) { String filePath = "E:\\files\\Maze.txt"; int dirs[][] = Path.dir; Path path = new Path(); Stack<Step> ss = new Stack<Step>(); char maps[][] = readMap(filePath); if(maps == null || maps.length == 0) System.out.println("read map failed "); path.printmaze(maps); path.setEntrance(maps); int inxs = path.inx; int inys = path.iny; path.findPath(maps, inxs, inys, dirs, ss); } public void setEntrance (char[][] map) { for(int i=0;i<N;i++) for(int j=0;j<N;j++) if(map[i][j]=='E' || map[i][j]=='e') { inx = j; iny = i; } } public void printmaze(char[][] map){ for(int i=0;i<N;i++) { for(int j=0;j<N;j++){ System.out.print(map[i][j]+" "); } System.out.println(""); } } public void printmaze(int[][] map){ for(int i=0;i<4;i++) { for(int j=0;j<2;j++){ System.out.print(map[i][j]+" "); } System.out.println(""); } } public void findPath(char[][] map,int inx, int iny,int[][] dir,Stack<Step> s) { Step temp = new Step(inx,iny); s.push(temp); while (!s.empty()) { //System.out.println("k = " + k++ ); int d = 0; int mark = 0;//mark the first pushStack temp = s.pop(); int x = temp.x; int y = temp.y; //System.out.println("temp.x = "+ temp.x+ ", temp.y = "+ temp.y); while (d < 4) { //System.out.println("d = " + d); int j = x + dir[d][0]; int i = y + dir[d][1]; if(i < 0 || i >= N || j < 0 ||j >= N ) { d++; } else if (map[i][j] == 'X' || map[i][j] == 'x'){ if(s.empty()){ System.out.println("no valid path"); return; } else { if(mark == 0){ temp = new Step(x,y); s.push(temp); } System.out.print("("+j+","+i+") "); while(!s.empty()){ Step stp = s.pop(); System.out.print("("+stp.x+","+stp.y+") "); } return ; } } else if (map[i][j]=='*') { if(mark == 0){ temp = new Step(x,y); s.pus 4000 h(temp); } x = j; y = i; temp = new Step(x,y); s.push(temp); d++; mark=1; map[i][j] = 'v';//change the status of the point which has been visited } else d++; } } if(s.empty()) System.out.println("no valid path"); } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统