1st experiments-NPC problems (backtracking algorithm and clustering analysis)

Source: Internet
Author: User
Tags print print

Question:The eight queens issue is an ancient and famous issue and is a typical case of backtracking algorithms. This problem was raised by the international chess player Max besell in 1848: Eight queens were placed on 8x8 gags of chess, so that they could not attack each other, that is to say, neither queen can be in the same row, column, or diagonal line. Gauss thinks there are 76 solutions. In 1854, different authors published 40 different solutions in the Berlin chess magazine. Later, they used Graph Theory to solve 92 solutions. After the invention of the computer, there are many ways to solve this problem.

Please program and implement the eight queens problem, and output the first three solutions of the 92 solutions to the screen (the two-dimensional matrix of 8x8, Q represents the queen, and X represents null ). And the process of solving this problem is extended to the n queen problem.

Solution: we begin to solve this problem with the idea of backtracking, starting with the first queen in the first row, then, the queen can be placed in the second row in sequence, until the maximum value of the column is reached, the queen has not been completed, and repeated operations are performed by re-tracing to the second column, otherwise, the corresponding Queen's emission method will be obtained.

The following code can solve the n queen problem.

Source code

 

Package review; import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstreamreader; public class queens {public static int num = 0; // total number of accumulative solutions public static int queensnumber; // Number of Queens, at the same time, it is also the total number of rows and columns on the board. Public static int [] Cols; // defines the Cols array, which indicates the placement of n columns. Public static void main (string ARGs []) {system. out. println ("Enter the number of queens to be displayed"); bufferedreader strin = new bufferedreader (New inputstreamreader (System. in); try {queensnumber = integer. parseint (strin. readline (); If (queensnumber <= 0) {system. out. println ("incorrect input");} else {system. out. println ("display the first three methods"); Queens Queen = new queens (queensnumber);} catch (exception e) {system. out. println ("incorrect input") ;}} public queens (INT queensnumber) {Cols = new int [queensnumber]; getarrangement (0, queensnumber); system. out. println (queensnumber + "Queen Problem" + num + "placement method. ");} Public void getarrangement (int n, int queensnumber) {// traverse all invalid rows in the column and record them using the rows array, invalid: rows [I] = trueboolean [] rows = new Boolean [queensnumber]; for (INT I = 0; I <n; I ++) {rows [Cols [I] = true; // set it to trueint d = n-I; If (Cols [I]-D> = 0) rows [Cols [I]-D] = true; If (Cols [I] + D <= queensnumber-1) rows [Cols [I] + D] = true ;} for (INT I = 0; I <queensnumber; I ++) {// deduce whether the row is legal if (rows [I]) continue; // set the row Cols [N] = I for the valid queen of the current column; // If (n <queensnumber-1) when the current column is not the last column) {getarrangement (n + 1, queensnumber); // recursive} else {// cumulative solution count num ++; // print Print print () ;}} public void print () {If (Num> = 4) // print the first three solutions {return;} else {system. out. println ("Number" + num + "method"); For (INT I = 0; I <queensnumber; I ++) {for (Int J = 0; j <queensnumber; j ++) {if (I = Cols [J]) {system. out. print ("Q");} elsesystem. out. print ("*");} system. out. println ("");}}}}

 

The result is as follows:

Queen four:

Queen eight


In order to achieve the goal of teaching students in accordance with their aptitude, The Office of Academic Affairs plans to thoroughly investigate and classify students. If K-means clustering algorithm is used, students may be divided into four categories, they are "active", "xueba", "Game life", and "confused ". Now you are the project owner. (1) Please design a complete project implementation plan; (2) Do you agree to classify students? (3) According to your given implementation plan and the elements that require attention (such as the day study time), please try to answer according to your own situation, as well as its own evaluation and positioning and efforts.

Analysis ideas: Because we use the K-means clustering algorithm for clustering, the following steps are the steps of this algorithm, so we should carry out and implement it according to the steps.

General steps of K-means clustering algorithm:

  1. Initialization. Input gene expression matrix as object set X, input the specified number of clustering classes N, and randomly select n objects as the initial cluster center in X. Set the iteration stop conditions, such as the maximum number of cycles or the convergence error margin of the cluster center.
  2. Iteration. Data Objects are allocated to the closest cluster center based on the similarity criterion to form a class. Initialize the membership matrix.
  3. Update the cluster center. Then, we use the mean vectors of each class as the new cluster center and allocate Data Objects again.
  4. Repeat steps 2 and 3 until the conditions for suspension are met.

 

(1) Detailed Implementation Scheme:1. preparations:After we confirm to carry out this activity, we first need to select the object to form the object set X, here our object is the school student, then we need to specify the number of clustering classes N, this is the number of student types and the total number of students. Then, we can randomly extract n objects (students) from X as the center of the cluster and set the corresponding iteration stop conditions.

2. IterationWe started to conduct a large number of actual investigations on our objects to obtain the corresponding data, and then distributed the data objects to the closest cluster center based on the similarity criteria to form a class, initialize the membership matrix. In fact, this is to classify the collected data through iteration, so as to classify several different student data.

3. Update the cluster centerAnd then use the average of each category as the new cluster center to allocate Data Objects again. This advantage is that our implementation scheme is constantly updated through the real-time data in the Survey and Statistics process, which can reduce the error rate of statistics.

4. Repeat Step 2., Until the conditions for suspension are met. That is to say, when we conduct a survey, the number reaches a certain level (we have estimated enough to complete our statistical work, and we will be able to stop the investigation if we set it up in advance .)

During the detailed implementation process, we can investigate the students' daily learning time, daily game time, class time, library time, community activity time, and extracurricular books ............

With the above ideas, we can use the following methods to conduct this survey: online surveys and on-site surveys. In order to achieve the effect of everyone's attention, we can add this to the teaching evaluation module of the educational administration system to judge everyone. In order to get a correct understanding and correct attitude to complete this survey, we must do a good job in the early stage. Otherwise, we will not receive reliable data and our investigation will be compromised.

(2) assume that this classification is only used as a survey and then conducts teaching activities based on the survey, which classifies students, it does not classify and treat students by 369. However, we should also have a lot of precautions, such as anonymous work, to protect the privacy of students. In the future, we assume that teachers and others will share the same view of students. Therefore, we cannot add the student type to the personal tag, which is detrimental to the development of students.

(3) Self-evaluation:

Day learning time

1. class time: 3 hours

2. Homework completion time: 1 hour

3. Reading a textbook: 0.2 hours

4. Other study time: 2 hours


Entertainment time

1. Sports: 0.3 hours

2. watching news, TV series, and movies: 2 hours


Student working hours

1. Assist the teacher in completing the work: 0.5 hours

2. Class/community Student Union: 0.5 hours


Living time

1. Get up: 0.5 hours

2. meal: 1 hour

......

Self-evaluation: In general, I am a more steadfast person, do not play games, shujia activities are more active than others. Every day, I feel that I am busy with more things than others. However, the professional knowledge is not solid enough, and the professional hands-on ability is poor. Although I am a junior and have little time, I will continue to learn it. Do your best to improve yourself. If you want to limit yourself with the above four types, I think either of them does not belong to me. Because I think that I sometimes take the initiative, sometimes not take the initiative, and have a correct learning attitude, but it is not the kind of student master, strong learning ability. I don't play games, but I also have some goals. If you give yourself a type: I think I am a progressive one.

 

 

 

 

1st experiments-NPC problems (backtracking algorithm and clustering analysis)

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.