Algorithm training king, After the legend time limit: 1.0s memory limit: 256.0MB problem Description The earth people know, in chess, after the sun, radiant, imposing eight sides, it can control the horizontal, firm, slash Position.
The Chinese people who have seen the Qing Palace play know that the harem is a dangerous place to Walk. The Queens have their own sphere of influence, but they can always find a way to be peaceful.
All the Chinese know that the imperial power is sacred, accompanied by the tiger, the Touch Longyan die ...
Now there is a n*n palace, where the king occupies a total of 9 squares in his place and around them, and these lattice queens cannot be used (if the king is on the side of the palace, it may occupy less than 9 squares). Of course, the Queen will not attack the King.
Now that the King's position is known (x, Y) (the King is located in row x, column y, The starting line of x, y, and column 1), How many options are there to place n queens so that they cannot attack each other. Input format One line, three integers, the size of the imperial palace and the location of the king represents an integer representing the number of scenarios in which n queens are placed, sample input 8 2 2 sample output 10 data size and Convention N<=12 sample Code:
1 Importjava.io.BufferedReader;2 Importjava.io.IOException;3 Importjava.io.InputStreamReader;4 5 public classMain {6 public Static intn = 0;//the size of the palace7 public Static intx = 0;//the Emperor's horizontal position (note: starting from 1)8 public Static inty = 0;//the position of the Emperor's ordinate (note: starting from 1)9 public Static int[] column_num;//the column number of the Queen putTen public Static int[] palace;//Royal palace one public Static intCount = 0;//several scenarios a - public Static voidMain (string[] Args)throwsIOException { -BufferedReader br =NewBufferedReader (NewInputStreamReader (system.in)); thestring[] str = br.readline (). split (""); -n = integer.parseint (str[0]); -x = Integer.parseint (str[1]); -y = Integer.parseint (str[2]); + - intK = x-2 > 0? x-2:0;//the starting position of the Emperor's line range + intm = y-2 > 0? y-2:0;//the starting position of the Emperor's column range ax = x > n-1? N-1: x;//the end position of the Emperor's line range aty = y > n-1? N-1: y;//the end position of the Emperor column range - -Column_num =New int[n]; -Palace =New int[n][n]; - - for(inti = k; I <= x; I++) {//Emperor Range-1 in for(intj = m; J <= y; J + +){ -palace[i][j] = 1; to } + } - thePlace (0); * $System.out.println (count);//number of output scenariosPanax Notoginseng - } the + //put the Queen a Private Static voidPlaceintNum) { the if(num = = N) {//if It's finished , +count++; -}Else{ $ for(intColumn = 0; Column < n; Column++) {//Column Changes $ if(palace[num][column] = =-1){ - Continue; - } thecolumn_num[num] = column;//Record column number - BooleanFlag =true;Wuyi for(intNumber = 0; Number < num; Number++) {//Verify that it is positioned accurately with a well-placed the if(column_num[num] = = Column_num[number] | |//whether the column -num + column_num[num] = = number + column_num[number] | |//whether the left diagonal wunum-column_num[num] = = Number-column_num[number]) {//is the right diagonal -Flag =false; about break; $ } - } - - if(flag) {//If you put it in place, go to the next Queen. aPlace (num+1); + } the } - } $ } the the}
Blue Bridge Cup algorithm training ALGO-125 wang, HOU legend