packagecom.mingrui.recursion; importjava.awt.event.*; importjava.awt.*; importjavax.swing.*;
publicclassTest19_8extendsJFrame{ privateboard=new Cell[8][8]; privateJButtonjbFindPath=newJButton("找到出路"); privateJButtonjbClearPant=newJButton("清除路线"); privateJPaneljpUp,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(int j=0;j<8;j++){ board[i][j]=new Cell(); jpUp.add(board[i][j]); } } add(jpUp,BorderLayout.CENTER); jpBut.add(jbFindPath); jpBut.add(jbClearPant); add(jpBut,BorderLayout.SOUTH); jbFindPath.addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEvent e){ findPath(); } }); jbClearPant.addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEvent e){ clearPath(); } }); setVisible(true);
} publicstaticvoid main(String [] args){ new Test19_8(); } publicvoidfindPath(){ if(findPath(0,0)){ //JOptionPane.showMessageDialog(null, "找到了,我找到了"); System.out.println("找到路了"); } else //JOptionPane.showMessageDialog(null,"没有了出路,人生就是悲哀"); System.out.println("没有出路"); } publicbooleanfindPath(introw,int col){ board[row][col].visit(); if(row==7&;&;col==7){ board[row][col].selectedCell(); returntrue; } if((row>0)&;&;!board[row-1][col].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+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-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].selectedCell(); returntrue;
} unblock(row,col); } returnfalse; } publicvoidclearPath(){ for(inti=0;i<8;i++){ for(int j=0;j<8;j++){ board[i][j].desSelectedCell(); } } } publicvoid block(introw,int col){ if(row>0) board[row-1][col].block(); if(row<7) board[row+1][col].block(); if(col>0) board[row][col-1].block(); if(col<7) board[row][col+1].block(); } publicvoid unblock(introw,int col){ if(row>0) board[row-1][col].unblock(); if(row<7) board[row+1][col].unblock(); if(col>0) board[row][col-1].unblock(); if(col<7) board[row][col+1].unblock(); } }
classCellextendsJPanel{ privatebooleanvisited=false; privatebooleanmarked=false; privatebooleancellSelected=false; privateboolean=false; Cell(){ setBackground(Color.red); addMouseListener(newMouseAdapter(){ publicvoidmousePressed(MouseEvent e) { marked = !marked; repaint(); } }); } protectedvoidpaintComponent(Graphics g){ super.paintComponent(g); if(marked){ g.drawLine(0, 0, getSize().width, getSize().height); g.drawLine(getSize().width, 0, 0, getSize().height); } } publicboolean visited(){ returnvisited; }
}
publicvoid visit(){ visited=true; }
publicboolean marked(){ returnmarked; }
publicvoid block(){ blocked=true