经典程序代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

一:八皇后问题package八皇后;importjava.awt.BorderLayout;importjava.awt.Container;importjava.awt.GridLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JOptionPane;importjavax.swing.JPanel;publicclassQueenextendsJFrameimplementsActionListener{/***@paramargs*/JButton[][]button=newJButton[8][8];int[][]chess=newint[8][8];intcount=0;publicQueen(){Containerc=this.getContentPane();JPanelpanel1=newJPanel();panel1.setLayout(newGridLayout(8,8));for(inti=0;i8;i++)for(intj=0;j8;j++){button[i][j]=newJButton();button[i][j].addActionListener(this);panel1.add(button[i][j]);}init();JPanelpanel2=newJPanel();JButtonstartButton=newJButton(开始);JButtonstopButton=newJButton(结束);panel2.add(startButton);startButton.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEventarg0){//TODOAuto-generatedmethodstubinit();}});stopButton.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEventarg0){//TODOAuto-generatedmethodstubSystem.exit(0);}});panel2.add(stopButton);c.setLayout(newBorderLayout());c.add(panel1,BorderLayout.CENTER);c.add(panel2,BorderLayout.SOUTH);setSize(500,600);setVisible(true);}privatevoidinit(){//TODOAuto-generatedmethodstubfor(inti=0;i8;i++)for(intj=0;j8;j++){chess[i][j]=0;button[i][j].setText();}count=0;}publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubQueenq=newQueen();q.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}@OverridepublicvoidactionPerformed(ActionEvente){//TODOAuto-generatedmethodstubJButtonb=(JButton)e.getSource();booleanflag=true;for(inti=0;i8;i++)for(intj=0;j8;j++){if(button[i][j]==b){for(ints=0;s8;s++)for(intt=0;t8;t++){if((s==i&&chess[s][t]==1)||(t==j&&chess[s][t]==1)||(Math.abs(i-s)==Math.abs(j-t)&&chess[s][t]==1)){flag=false;break;}}if(flag){chess[i][j]=1;button[i][j].setText(皇后);count++;System.out.print(count);isWin();}else{JOptionPane.showMessageDialog(null,请选择正确位置,提示,JOptionPane.INFORMATION_MESSAGE);}}}}privatevoidisWin(){//TODOAuto-generatedmethodstubif(count==8){JOptionPane.showMessageDialog(null,恭喜您,游戏结束,恭喜,JOptionPane.INFORMATION_MESSAGE);init();}}}package八皇后;publicclassEightQueen{/***@paramargs*/staticintcount=0;publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubint[][]num=newint[8][8];queen(num,0);}privatestaticvoidqueen(int[][]num,inti){//TODOAuto-generatedmethodstubif(i==num.length){count++;System.out.println(=====+count+======);for(ints=0;snum.length;s++){for(intt=0;tnum.length;t++)System.out.print(num[s][t]+);System.out.println();}}else{for(intj=0;jnum.length;j++){booleanflag=true;for(ints=0;snum.length;s++)for(intt=0;tnum.length;t++){if(s==i||t==j||Math.abs(s-i)==Math.abs(t-j)){if(num[s][t]==1){flag=false;break;}}}if(flag){num[i][j]=1;queen(num,i+1);num[i][j]=0;}}}}}package八皇后;importjava.io.BufferedOutputStream;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;publicclassQueenWithFile{intcol[];intdiagonal[];intundiagonal[];intql[];intq;//记录已经放入的皇后数目intresult;//记录解数intn;//总皇后数publicQueenWithFile(intm){n=m;q=0;result=0;ql=newint[n];diagonal=newint[2*n-1];undiagonal=newint[2*n-1];col=newint[n];}publicvoidstoreResult(Stringdir)throwsFileNotFoundException{Filefile=newFile(dir);FileOutputStreamfileOutputStream=newFileOutputStream(file,true);BufferedOutputStreamout=newBufferedOutputStream(fileOutputStream);inti,j;try{for(i=0;in;i++){for(j=0;jn;j++)if(ql[i]==j)out.write(1\t.getBytes());elseout.write(0\t.getBytes());out.write(\r\n.getBytes());}result++;out.write(---------------------------------------------------\r\n.getBytes());out.flush();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}publicvoidsolve(inti)throwsFileNotFoundException{intj;for(j=0;jn;j++)if(col[j]==0&&diagonal[i+j]==0&&undiagonal[n-1+i-j]==0){col[j]=1;diagonal[i+j]=1;undiagonal[n-1+i-j]=1;ql[i]=j;q++;if(q==n)storeResult(d:/test.txt);elseif(in-1)solve(i+1);elsesolve(0);q--;ql[i]=0;col[j]=0;diagonal[i+j]=0;undiagonal[n-1+i-j]=0;}}publicstaticvoidmain(Stringargs[]){QueenWithFileq=newQueenWithFile(8);try{Filefile=newFile(d:/test.txt);if(!file.exists())try{file.createNewFile();}catch(IOExceptione1){//TODOAuto-generatedcatchblocke1.printStackTrace();}q.solve(0);}catch(FileNotFoundExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}二:黑白棋问题package黑白棋;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Container;importjava.awt.Graphics;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.MouseEvent;importjava.awt.event.MouseListener;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JOptionPane;importjavax.swing.JPanel;publicclassBlackWhiteChessextendsJFrame{/***@paramargs*/int[][]chess=newint[8][8];intturn=2;MyPanelpanel=null;publicBlackWhiteChess(){Containerc=this.getContentPane();init();panel=newMyPanel();JButtonstartButton=newJButton(开始);startButton.addActionListener(newActionListene

1 / 75
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功