查看: 1225| 回复: 2
跳转到指定楼层
上一主题 下一主题
收起左侧

小迷宫问题,用文件读入数据出错了

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
我的代码:
  1. package org.stack;
  2. import java.io.*;


  3. public class Maze {
  4.         private static int startI,startJ;//入口坐标
  5.         private static int endI,endJ;//出口坐标
  6.        
  7.         public void start(int startI,int startJ){
  8.                 this.startI=startI;
  9.                 this.startJ=startJ;
  10.         }
  11.         public void end(int endI,int endJ){
  12.                 this.endI=endI;
  13.                 this.endJ=endJ;
  14.         }

  15.         public static void main(String[] args) {
  16.                
  17.                 int maze[][]={};
  18.                  java.util.Scanner scanner=null;
  19.                  String filename="d:/1.txt";
  20.                  
  21.                  
  22.                  try{
  23.                       scanner=new java.util.Scanner(new File(filename));
  24.                       for(int i=0;i<9;i++){
  25.                                         for(int j=0;j<9;j++){
  26.                                                 maze[i][j]=scanner.nextInt();
  27.                                                 }
  28.                                         }
  29.                   }catch(FileNotFoundException e){
  30.                    e.printStackTrace();
  31.                    System.out.println("Error openning the file"+filename);
  32.                   }
  33.                
  34.                
  35.                 create(maze);

  36.                 Maze cell=new Maze();
  37.                 cell.start(1, 1);
  38.                 cell.end(7, 7);
  39.                
  40.                 visited(maze, startI, startJ);
  41.         }
  42.        
  43.         public static void visited(int[][] cell,int i,int j){
  44.                 cell[i][j]=1;
  45.                 if(i==endI&&j==endJ){
  46.                         System.out.println("走完一条路线");
  47.                         for(int m=0;m<cell.length;m++){
  48.                                 for(int n=0;n<cell[0].length;n++){
  49.                                         if(cell[m][n]==2){
  50.                                                 System.out.print("#");
  51.                                         }else if (cell[m][n]==1) {
  52.                                                 System.out.print("*");
  53.                                         }else {
  54.                                                 System.out.print(" ");
  55.                                         }
  56.                                 }
  57.                                 System.out.println();
  58.                         }
  59.                 }
  60.                
  61.                  //向右
  62.                 if(cell[i][j+1] == 0){
  63.                         visited(cell, i, j+1);
  64.                 }
  65.                 //向下
  66.                 if(cell[i+1][j] == 0){
  67.                         visited(cell, i+1, j);
  68.                 }
  69.                 //向左
  70.                 if(cell[i][j-1] == 0){
  71.                         visited(cell, i, j-1);
  72.                 }
  73.                 //向上
  74.                 if(cell[i-1][j] == 0){
  75.                         visited(cell, i-1, j);
  76.                 }
  77.                
  78.                 cell[i][j]=0;
  79.         }
  80.        
  81.        
  82.         //打印迷宫图
  83.         public static void  create(int[][] maze){
  84.                 for(int i=0;i<maze.length;i++){
  85.                         for(int k=0;k<maze.length;k++){
  86.                                 if(maze[i][k]==2){
  87.                                         System.out.print("#");
  88.                                 }else {
  89.                                         System.out.print(" ");
  90.                                 }
  91.                         }
  92.                         System.out.println();
  93.                 }
  94.         }
  95. }
复制代码
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

目录没错,咋就不行呢?**求大牛指点


上一篇:【第三轮】7.7-7.13 CareerCup 4.6
下一篇:leetcode从来没自己做对过
🔗
donnice 2014-7-8 22:45:05 | 只看该作者
全局:
我觉得不只是文件读入的问题。LZ能告诉我这个程序是为了什么吗?设计迷宫?还是自动找出一条路?
回复

使用道具 举报

🔗
 楼主| TonyJang 2014-7-9 14:55:07 | 只看该作者
全局:
donnice 发表于 2014-7-8 22:45
我觉得不只是文件读入的问题。LZ能告诉我这个程序是为了什么吗?设计迷宫?还是自动找出一条路?

自动找路径啊
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表