活跃农民-感谢提供高质量信息和讨论

- 积分
- 373
- 学分
- 个
- 大米
- 升
- 人参
- 枚
- 水井
- 尺
- 小麦
- 颗
- 萝卜
- 根
- 小米
- 粒
- UID
- 107278
- 注册时间
- 2014-1-13
- 最后登录
- 1970-1-1
- 在线时间
- 小时
- 好友
- 收听
- 听众
- 日志
- 相册
- 帖子
- 主题
- 分享
- 精华
|
本帖最后由 TonyJang 于 2014-7-2 17:14 编辑
就是找到所有可以从起点到终点的路径,我搜了一下代码:- package org.stack;
- public class Maze {
- private static int startI,startJ;//入口坐标
- private static int endI,endJ;//出口坐标
-
- public void start(int startI,int startJ){
- this.startI=startI;
- this.startJ=startJ;
- }
- public void end(int endI,int endJ){
- this.endI=endI;
- this.endJ=endJ;
- }
- public static void main(String[] args) {
- int maze[][] ={{2, 2, 2, 2, 2, 2, 2, 2, 2},
- {2, 0, 0, 0, 0, 0, 0, 0, 2},
- {2, 0, 2, 2, 0, 2, 2, 0, 2},
- {2, 0, 2, 0, 0, 2, 0, 0, 2},
- {2, 0, 2, 0, 2, 0, 2, 0, 2},
- {2, 0, 0, 0, 0, 0, 2, 0, 2},
- {2, 2, 0, 2, 2, 0, 2, 2, 2},
- {2, 0, 0, 0, 0, 0, 0, 0, 2},
- {2, 2, 2, 2, 2, 2, 2, 2, 2}};
-
- create(maze);
- Maze cell=new Maze();
- cell.start(1, 1);
- cell.end(7, 7);
-
- visited(maze, startI, startJ);
- }
-
- public static void visited(int[][] cell,int i,int j){
- cell[i][j]=1;
- if(i==endI&&j==endJ){
- System.out.println("走完一条路线");
- for(int m=0;m<cell.length;m++){
- for(int n=0;n<cell[0].length;n++){
- if(cell[m][n]==2){
- System.out.print("#");
- }else if (cell[m][n]==1) {
- System.out.print("*");
- }else {
- System.out.print(" ");
- }
- }
- System.out.println();
- }
- }
-
- //向右
- if(cell[i][j+1] == 0){
- visited(cell, i, j+1);
- }
- //向下
- if(cell[i+1][j] == 0){
- visited(cell, i+1, j);
- }
- //向左
- if(cell[i][j-1] == 0){
- visited(cell, i, j-1);
- }
- //向上
- if(cell[i-1][j] == 0){
- visited(cell, i-1, j);
- }
-
- cell[i][j]=0;
- }
-
-
- //打印迷宫图
- public static void create(int[][] maze){
- for(int i=0;i<maze.length;i++){
- for(int k=0;k<maze.length;k++){
- if(maze[i][k]==2){
- System.out.print("#");
- }else {
- System.out.print(" ");
- }
- }
- System.out.println();
- }
- }
- }
复制代码 路径张贴之后就不好看了,大家可以再eclipse里运行一下,Here is my Q:
第一次遍历数组到了点A[5,7],再往下就不是0了,接下来改怎么运行呢?我感觉卡住了
|
|