注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
我的代码:- package org.stack;
- import java.io.*;
- 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[][]={};
- java.util.Scanner scanner=null;
- String filename="d:/1.txt";
-
-
- try{
- scanner=new java.util.Scanner(new File(filename));
- for(int i=0;i<9;i++){
- for(int j=0;j<9;j++){
- maze[i][j]=scanner.nextInt();
- }
- }
- }catch(FileNotFoundException e){
- e.printStackTrace();
- System.out.println("Error openning the file"+filename);
- }
-
-
- 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();
- }
- }
- }
复制代码 1.txt文件:
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
目录没错,咋就不行呢?**求大牛指点
|