资源描述:
《java解决迷宫问题,gui界面,非常实用》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、packagecom.mingrui.recursion;importjava.awt.event.*;importjava.awt.*;importjavax.swing.*;publicclassTest19_8extendsJFrame{privateCell[][]board=newCell[8][8];privateJButtonjbFindPath=newJButton("找到出路");privateJButtonjbClearPant=newJButton("清除路线");privateJ
2、PaneljpUp,jpBut;Test19_8(){setSize(345,256);setResizable(false);setDefaultCloseOperation(3);setLocationRelativeTo(null);jpUp=newJPanel();jpBut=newJPanel();jpUp.setLayout(newGridLayout(8,8,2,2));for(inti=0;i<8;i++){for(intj=0;j<8;j++){board[i][j]=newCell(
3、);jpUp.add(board[i][j]);}}add(jpUp,BorderLayout.CENTER);jpBut.add(jbFindPath);jpBut.add(jbClearPant);add(jpBut,BorderLayout.SOUTH);jbFindPath.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){findPath();}});jbClearPant.addActi
4、onListener(newActionListener(){publicvoidactionPerformed(ActionEvente){clearPath();}});setVisible(true);}publicstaticvoidmain(String[]args){newTest19_8();}publicvoidfindPath(){if(findPath(0,0)){//JOptionPane.showMessageDialog(null,"找到了,我找到了");System.out.
5、println("找到路了");}else//JOptionPane.showMessageDialog(null,"没有了出路,人生就是悲哀");System.out.println("没有出路");}publicbooleanfindPath(introw,intcol){board[row][col].visit();if(row==7&&col==7){board[row][col].selectedCell();returntrue;}if((row>0)&&!board[row-1][col
6、].blocked()&&!board[row-1][col].marked()&&!board[row-1][col].visited()){block(row,col);if(findPath(row-1,col)){board[row][col].selectedCell();returntrue;}unblock(row,col);}if((row<7)&&!board[row+1][col].blocked()&&!board[row+1][col].marked()&&!board[row+
7、1][col].visited()){block(row,col);if(findPath(row+1,col)){board[row][col].selectedCell();returntrue;}unblock(row,col);}if((col>0)&&!board[row][col-1].blocked()&&!board[row][col-1].marked()&&!board[row][col-1].visited()){block(row,col);if(findPath(row,col
8、-1)){board[row][col].selectedCell();returntrue;}unblock(row,col);}if((col<7)&&!board[row][col+1].blocked()&&!board[row][col+1].marked()&&!board[row][col+1].visited()){block(row,col);if(findPath(row,col+1)){board[row][col].