Java implementation of several more classical algorithm problems

Source: Internet
Author: User

1. Eight Queen's question
1  Public classEightqueen {2 3     Private Static Final intROW = 16;4     Private Static Final intCOL = 16;5 6     Private Static intCount = 0;//Eight the number of the Queen's Solutions7 8     Private Static Boolean[] maps =New Boolean[ROW] [COL];//Initializes a two-dimensional array, simulates the 8*8 checkerboard, and the default value is false to indicate no queen9 Ten     //How to put the Queen?  One     /** A * Prevent the Queen of row row -      * @paramRow -      */ the      Public Static voidPutqueen (introw) { -  -         //Export of recursion -         if(row = = row) {//row=8, the 9th row, then the front 8 rows is ok + Printqueen (); -             return; +         } A  at         //Put the Queen in column J of row No. -          for(intj = 0; J < COL; J + +) { -             //If you can put it, put the Queen at that point. -             if(IsOK (Row, J)) { -MAPS[ROW][J] =true; -Putqueen (row + 1);//after the line is put down and put down a line "recursion to put the Queen", the algorithm has jumped inMAPS[ROW][J] =false;//backtracking, when the row+1 line piece is not satisfied, will go back to the first row -             } to         } +     } -  the     //if you want to place the Queen at the (x, y) point, you need to first determine whether the point can be placed *      Public Static BooleanIsOK (intXinty) { $ Panax Notoginseng         //Traverse a two-dimensional array to determine whether there are queens in 4 directions -          for(inti = 0; i < ROW; i++) { the              for(intj = 0; J < COL; J + +) { +                 //positive hypotenuse x-y fixed value, inverse bevel x+y as fixed value A                 if(i = = x | | j = = Y | | i-j = = XY | | i + j = = x +y) { the                     //judge whether there is a queen in 4 directions +                     if(Maps[i][j]) {//returns False if a queen is placed at that point -                         return false; $                     } $                 } -             } -         } the  -         return true;Wuyi     } the  -      Public Static voidPrintqueen () { Wu  -System.out.println ("++count" + "One Solution")); AboutSystem.out.println ("*******************"); $          for(inti = 0; i < ROW; i++) { -              for(intj = 0; J < COL; J + +) { -                 if(Maps[i][j]) { -System.out.print ("Q"); A}Else { +System.out.print ("*"); the                 } -             } $ System.out.println (); the         } the     } the  the      Public Static voidMain (string[] args) { -  inPutqueen (0); the     } the}
2. Bean Machine (plum bottle, Galton bottle problem) 3.

Java implementation of several more classical algorithm problems

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.