java地图路径染色寻找
废话不多说,直接上代码:
package lanqiaobei;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Maze
{
class Cell
{
private int row;
private int col;
private Cell from;
public Cell(int row, int col, Cell from)
{
this.row = row;
this.col = col;
this.from = from;
}
}
char[][] maze =
{{'#','#','#','#','B','#','#','#','#','#','#','#'},
{'#','#','#','#','.','.','.','.','#','#','#','#'},
{'#','#','#','#','.','#','#','#','#','.','.','#'},
{'#','.','.','.','.','#','#','#','#','#','.','#'},
{'#','.','#','#','#','#','#','.','#','#','.','#'},
{'#','.','#','#','#','#','#','.','#','#','.','#'},
{'#','.','#','#','.','.','.','.','.','.','.','#'},
{'#','.','#','#','.','#','#','#','.','#','.','#'},
{'#','.','.','.','.','#','#','#','.','#','.','#'},
{'#','#','.','#','.','#','#','#','.','#','.','A'},
{'#','#','.','#','#','#','.','.','.','#','#','#'},
{'#','#','#','#','#','#','#','#','#','#','#','#'}};
public void show()
{
for(int i=0; i
from, Set
dest) { Iterator
it = from.iterator(); while(it.hasNext()) { Cell a = it.next(); Cell[] c = new Cell[4]; c[0] = new Cell(a.row-1, a.col, a); c[1] = new Cell(a.row, a.col-1, a); c[2] = new Cell(a.row+1, a.col, a); c[3] = new Cell(a.row, a.col+1, a);; //填空 for(int i=0; i<4; i++) { //越出地图就跳过 if(c[i].row < 0 || c[i].row >= maze.length) continue; if(c[i].col < 0 || c[i].col >= maze[0].length) continue; char x = maze[c[i].row][c[i].col]; if(x=='B') return a; if(x=='.') { maze[c[i].row][c[i].col] = ' '; dest.add(c[i]); } } } return null; } public void resolve() { Set
set = new HashSet
(); set.add(new Cell(9,11,null)); for(;;) { Set
set1 = new HashSet
|
(); Cell a = colorCell(set, set1); if(a!=null) { System.out.println("找到解!"); while(a!=null) { maze[a.row][a.col] = '+'; a = a.from ; } break; } if(set1.isEmpty()) { System.out.println("无解!"); break; } set = set1; } } public static void main(String[] args) { Maze m = new Maze(); m.show(); m.resolve(); m.show(); } }
|
|
|
|
|
|
输出内容:
-----------------------------------------------------
# # # # B # # # # # # #
# # # # . . . . # # # #
# # # # . # # # # . . #
# . . . . # # # # # . #
# . # # # # # . # # . #
# . # # # # # . # # . #
# . # # . . . . . . . #
# . # # . # # # . # . #
# . . . . # # # . # . #
# # . # . # # # . # . A
# # . # # # . . . # # #
# # # # # # # # # # # #
找到解!
# # # # B # # # # # # #
# # # # + . . . # # # #
# # # # + # # # # #
# + + + + # # # # # #
# + # # # # # # # #
# + # # # # # # # #
# + # # + + + + + + + #
# + # # + # # # # + #
# + + + + # # # # + #
# # # # # # # + +
# # # # # # # #
# # # # # # # # # # # #
小树技术博客 http://www.qiushurong.cn