多语言展示
当前在线:1768今日阅读:126今日分享:42

Java学习之经典算法(二)

本篇介绍了用Java实现老鼠走迷官算法
工具/原料
1

win7

2

MyEclipse

3

jdk1.6

方法/步骤
1

1. 老鼠走迷官(一)        说明老鼠走迷宫是递回求解的基本题型,我们在二维阵列中使用2表示迷宫墙壁,使用1来表示老鼠的行走路径,试以程式求出由入口至出口的路径。解法老鼠的走法有上、左、下、右四个方向,在每前进一格之后就选一个方向前,无法前进时退回选择下一个可前进方向,如此在阵列中依序测试四个方向,直到走到出口为止,这是递回的基本题,请直接看程式应就可以理解。代码如下:public class MainClass {public static void main(String[] args) {//一个二维数组来表示迷宫,2表示墙壁,0表示通路。//老鼠每走到一个格子的时候就将该位置的值置为1,表示老鼠的行走路径包括这个格子。int[][] maze = { { 2, 2, 2, 0, 2, 2, 2, 0, 0 }, { 2, 0, 0, 0, 0, 0, 2, 0, 0 },  { 2, 0, 2, 2, 2, 2, 2, 2, 2 }, { 0, 0, 0, 0, 0, 0, 0, 0, 2 },  { 2, 0, 2, 2, 2, 2, 0, 2, 2 }, { 2, 0, 2, 2, 0, 0, 0, 2, 2 },  { 2, 0, 2, 2, 0, 2, 2, 0, 2 }, { 2, 0, 2, 0, 0, 0, 0, 0, 0 },  { 2, 0, 2, 2, 2, 2, 2, 2, 2 } };Map map = new Map(maze, new Point(7, 8));MainClass.go(map, new Point(0, 3));map.print();}public static void go(Map map, Point p) {map.step(p);//走到哪里则赋值为1if (p.y < map.maze[0].length - 1) {test(map, new Point(p.x, p.y + 1));}if (p.x < map.maze.length - 1) {test(map, new Point(p.x + 1, p.y));}if (p.y >= 1) {test(map, new Point(p.x, p.y - 1));}if (p.x >= 1) {test(map, new Point(p.x - 1, p.y));}if (!map.isArrived())map.empty(p);}public static void test(Map map, Point p) {if (!map.isArrived() && map.isEmpty(p)) {go(map, p);}}}class Point {int x;int y;public Point(int x1, int y1) {x = x1;y = y1;}}class Map {int[][] maze;Point end; // 终点public Map(int[][] maze, Point end) {this.maze = maze;this.end = end;}// 是否到达终点public boolean isArrived() {return maze[end.x][end.y] == 1;}// 当前这一格是否可行public boolean isEmpty(Point p) {return maze[p.x][p.y] == 0;}// 可以理解为当经过Point p 路线不对,把老鼠走过的痕迹抹去,置0public void empty(Point p) {maze[p.x][p.y] = 0;}// 走到Point p   置1public void step(Point p) {maze[p.x][p.y] = 1;}// 打印地图,含老鼠走过的路径public void print() {for (int i = 0; i < maze.length; i++) {for (int j = 0; j < maze[0].length; j++)if (maze[i][j] == 2) {System.out.print('█');} else if (maze[i][j] == 0) {System.out.print(' ');} else if (maze[i][j] == 1) {System.out.print('1');}System.out.println();}}}结果如下:

2

2.老鼠走迷官(二)       说明由于迷宫的设计,老鼠走迷宫的入口至出口路径可能不只一条,如何求出所有的路径呢?解法求所有路径看起来复杂但其实更简单,只要在老鼠走至出口时显示经过的路径,然后退回上一格重新选择下一个位置继续递回就可以了,比求出单一路径还简单,我们的程式只要作一点修改就可以了。代码如下:public class MainClass { public static void main(String[] args) {                   int[][] maze={{2,2,2,0,2,2,2,0,0},                        {2,0,0,0,0,0,2,0,0},                        {2,0,2,2,2,0,2,2,2},                        {0,0,0,0,0,0,0,0,2},                        {2,0,2,2,2,2,0,2,2},                        {2,0,2,2,0,0,0,2,2},                        {2,0,2,2,0,2,2,0,2},                        {2,0,2,0,0,0,0,0,0},                        {2,0,2,2,2,2,2,2,2}                              };          Map map=new Map(maze, new Point(7, 8));          MainClass.go(map, new Point(0,3));          //map.print();            }      public static void go(Map map,Point p){                       map.step(p);             if(map.isArrived())             {                 map.print();                 map.maze[map.end.x][map.end.y]=0;             }                              if(p.y=1)             {                 test(map,new Point(p.x, p.y-1));             }             if(p.x>=1)             {                 test(map,new Point(p.x-1, p.y));             }                              map.empty(p);      }     public static void test(Map map,Point p){                    if(!map.isArrived() && map.isEmpty(p)){              go(map,p);          }      }         }    class Point{            int x;      int y;      public Point(int x1,int y1){                    x=x1;          y=y1;      }  }  class Map{            int[][] maze;      Point end;    //终点      public Map(int[][] maze,Point end){                    this.maze=maze;          this.end=end;                }      //是否到达终点      public boolean isArrived(){                    return maze[end.x][end.y]==1;                }      //当前这一格是否可行      public boolean isEmpty(Point p){                      return maze[p.x][p.y]==0;      }      //可以理解为当经过Point p 无法走到终点时,把老鼠走过的痕迹抹去,表示最终的走法不含p      public void empty(Point p){                    maze[p.x][p.y]=0;                }      //走到Point p      public void step(Point p){                        maze[p.x][p.y]=1;                }      //打印地图,含老鼠走过的路径      public void print(){          for(int i=0;i

推荐信息