迷宫问题

对于走迷宫,人们提出过很多计算机上的解法。深度优先搜索、广度优先搜索是使用最广的方法。生活中,人们更愿意使用“紧贴墙壁,靠右行走”的简单规则。
下面的代码则采用了另一种不同的解法。它把走迷宫的过程比做“染色过程”。假设入口点被染为红色,它的颜色会“传染”给与它相邻的可走的单元。这个过程不断进行下去,如果最终出口点被染色,则迷宫有解。

  1 //
  2 //迷宫问题
  3 //
  4 //对于走迷宫,人们提出过很多计算机上的解法。深度优先搜索、广度优先搜索是使用最广的方法。生活中,人们更愿意使用“紧贴墙壁,靠右行走”的简单规则。
  5 //下面的代码则采用了另一种不同的解法。它把走迷宫的过程比做“染色过程”。假设入口点被染为红色,它的颜色会“传染”给与它相邻的可走的单元。这个过程不断进行下去,如果最终出口点被染色,则迷宫有解。
  6 //
  7 //仔细分析代码中的逻辑,填充缺少的部分。
  8 //
  9 //把填空的答案(仅填空处的答案,不包括题面)存入考生文件夹下对应题号的“解答.txt”中即可。
 10 //
 11 //public class Maze
 12 //{
 13 //    class Cell
 14 //    {
 15 //        private int row;
 16 //        private int col;
 17 //        private Cell from;
 18 //        
 19 //        public Cell(int row, int col, Cell from)
 20 //        {
 21 //            this.row = row;
 22 //            this.col = col;
 23 //            this.from = from;
 24 //        }
 25 //    }
 26 //        
 27 //    char[][] maze = 
 28 //    {{'#','#','#','#','B','#','#','#','#','#','#','#'},
 29 //    {'#','#','#','#','.','.','.','.','#','#','#','#'},
 30 //    {'#','#','#','#','.','#','#','#','#','.','.','#'},
 31 //    {'#','.','.','.','.','#','#','#','#','#','.','#'},
 32 //    {'#','.','#','#','#','#','#','.','#','#','.','#'},
 33 //    {'#','.','#','#','#','#','#','.','#','#','.','#'},
 34 //    {'#','.','#','#','.','.','.','.','.','.','.','#'},
 35 //    {'#','.','#','#','.','#','#','#','.','#','.','#'},
 36 //    {'#','.','.','.','.','#','#','#','.','#','.','#'},
 37 //    {'#','#','.','#','.','#','#','#','.','#','.','A'},
 38 //    {'#','#','.','#','#','#','.','.','.','#','#','#'},
 39 //    {'#','#','#','#','#','#','#','#','#','#','#','#'}};
 40 //    
 41 //    public void show()
 42 //    {
 43 //        for(int i=0; i<maze.length; i++)
 44 //        {
 45 //            for(int j=0; j<maze[i].length; j++) 
 46 //System.out.print(" " + maze[i][j]);
 47 //            System.out.println();
 48 //        }
 49 //    }
 50 //    
 51 //    //把与from集合中相邻的可染色节点染色,被染色节点记入 dest
 52 //    //一旦发现出口将被染色,则返回当前的“传播源”节点
 53 //    public Cell colorCell(Set<Cell> from, Set<Cell> dest)
 54 //    {
 55 //        Iterator<Cell> it = from.iterator();
 56 //        while(it.hasNext())
 57 //        {
 58 //            Cell a = it.next();
 59 //            Cell[] c = new Cell[4];
 60 //            c[0] = new Cell(a.row-1, a.col, a);
 61 //            c[1] = new Cell(a.row, a.col-1, a);
 62 //            c[2] = new Cell(a.row+1, a.col, a);
 63 //            c[3] = ___________________________;
 64 //                        
 65 //            for(int i=0; i<4; i++)
 66 //            {
 67 //                if(c[i].row < 0 || c[i].row >= maze.length) continue;
 68 //                if(c[i].col < 0 || c[i].col >= maze[0].length) continue;
 69 //                
 70 //                char x = maze[c[i].row][c[i].col];
 71 //                if(x=='B') return a;
 72 //                if(x=='.') 
 73 //                {
 74 //                    maze[c[i].row][c[i].col] = '?';
 75 //                    ____________________;
 76 //                }
 77 //            }
 78 //        }
 79 //        return null;
 80 //    }
 81 //    
 82 //    public void resolve()
 83 //    {
 84 //        Set<Cell> set = new HashSet<Cell>();
 85 //        set.add(new Cell(9,11,null));
 86 //        
 87 //        for(;;)
 88 //        {
 89 //            Set<Cell> set1 = new HashSet<Cell>();
 90 //            Cell a = colorCell(set, set1);
 91 //            if(a!=null)
 92 //            {
 93 //                System.out.println("找到解!");
 94 //                while(a!=null)
 95 //                {
 96 //                    maze[a.row][a.col] = '+';
 97 //                    ______________;
 98 //                }
 99 //                break;
100 //            }
101 //            if(set1.isEmpty())
102 //            {
103 //                System.out.println("无解!");
104 //                break;
105 //            }
106 //            set = set1;
107 //        }        
108 //    }
109 //    
110 //    public static void main(String[] args)
111 //    {
112 //        Maze m = new Maze();
113 //        m.show();
114 //        m.resolve();
115 //        m.show();    
116 //    }
117 //}
118 
119 
120 import java.util.*;
121 
122 public class Maze
123 {
124     class Cell
125     {
126         private int row;
127         private int col;
128         private Cell from;
129         
130         public Cell(int row, int col, Cell from)
131         {
132             this.row = row;
133             this.col = col;
134             this.from = from;
135         }
136     }
137         
138     char[][] maze = 
139     {{'#','#','#','#','B','#','#','#','#','#','#','#'},
140     {'#','#','#','#','.','.','.','.','#','#','#','#'},
141     {'#','#','#','#','.','#','#','#','#','.','.','#'},
142     {'#','.','.','.','.','#','#','#','#','#','.','#'},
143     {'#','.','#','#','#','#','#','.','#','#','.','#'},
144     {'#','.','#','#','#','#','#','.','#','#','.','#'},
145     {'#','.','#','#','.','.','.','.','.','.','.','#'},
146     {'#','.','#','#','.','#','#','#','.','#','.','#'},
147     {'#','.','.','.','.','#','#','#','.','#','.','#'},
148     {'#','#','.','#','.','#','#','#','.','#','.','A'},
149     {'#','#','.','#','#','#','.','.','.','#','#','#'},
150     {'#','#','#','#','#','#','#','#','#','#','#','#'}};
151     
152     public void show()
153     {
154         for(int i=0; i<maze.length; i++)
155         {
156             for(int j=0; j<maze[i].length; j++) 
157 System.out.print(" " + maze[i][j]);
158             System.out.println();
159         }
160     }
161     
162     //把与from集合中相邻的可染色节点染色,被染色节点记入 dest
163     //一旦发现出口将被染色,则返回当前的“传播源”节点
164     public Cell colorCell(Set<Cell> from, Set<Cell> dest)
165     {
166         Iterator<Cell> it = from.iterator();
167         while(it.hasNext())
168         {
169             Cell a = it.next();
170             Cell[] c = new Cell[4];
171             c[0] = new Cell(a.row-1, a.col, a);
172             c[1] = new Cell(a.row, a.col-1, a);
173             c[2] = new Cell(a.row+1, a.col, a);
174             c[3] = new Cell(a.row, a.col+1, a);
175                         
176             for(int i=0; i<4; i++)
177             {
178                 if(c[i].row < 0 || c[i].row >= maze.length) continue;
179                 if(c[i].col < 0 || c[i].col >= maze[0].length) continue;
180                 
181                 char x = maze[c[i].row][c[i].col];
182                 if(x=='B') return a;
183                 if(x=='.') 
184                 {
185                     maze[c[i].row][c[i].col] = '?';
186                     dest.add(c[i]) ;
187                 }
188             }
189         }
190         return null;
191     }
192     
193     public void resolve()
194     {
195         Set<Cell> set = new HashSet<Cell>();
196         set.add(new Cell(9,11,null));
197         
198         for(;;)
199         {
200             Set<Cell> set1 = new HashSet<Cell>();
201             Cell a = colorCell(set, set1);
202             if(a!=null)
203             {
204                 System.out.println("找到解!");
205                 while(a!=null)
206                 {
207                     maze[a.row][a.col] = '+';
208                     a = a.from ;
209                 }
210                 break;
211             }
212             if(set1.isEmpty())
213             {
214                 System.out.println("无解!");
215                 break;
216             }
217             set = set1;
218         }        
219     }
220     
221     public static void main(String[] args)
222     {
223         Maze m = new Maze();
224         m.show();
225         m.resolve();
226         m.show();    
227     }
228 }
View Code
posted @ 2014-02-18 18:17  JL-JackyLee  阅读(333)  评论(0编辑  收藏  举报