5.1.3 代码实现
package study.arithmetic; public class Horses { //棋盘格数 int gridNum = 8; //结果计算器 int count = 0; //行 int[] line = new int[8]; //列 int[] column = new int[8]; int[][] results = new int[gridNum][gridNum]; public void HorsesAction(int x,int y,int count){ results[x][y] = count; //放置第z个棋子 if(count == gridNum*gridNum){ for(int i=0; i < gridNum; i++) { for(int j=0; j < gridNum; j++){ System.out.println(results[i][j]); } } count++; //结果计数 return; } for(int i=0; i<gridNum; i++){ if((x+line[i])>=0 && (y+column[i])>=0 //下一位置能否 && (x+line[i])<gridNum && (y+column[i]) < gridNum //放置棋子的判断 && results[x+line[i]][y+column[i]] == 0) { HorsesAction(x+line[i], y+column[i], count+1); //递归调用 results[x+line[i]][y+column[i]] = 0; //恢复回退前的状态 } } } public static void main(String args[]){ Horses horses = new Horses(); horses.initializeColumn(); horses.initializeLine(); //初始化x,y坐标 int x=0; int y=0; //初始化棋盘 for(int i=0;i<horses.gridNum;i++){ for(int j=0;j<horses.gridNum;j++){ horses.results[i][j] = 0; System.out.println("输入起始坐标点"); horses.HorsesAction(x,y,1); } } System.out.println("最终结果="+horses.count); } //初始化行 public void initializeLine(){ line[0] = -2; line[1] = -2; line[2] = -1; line[3] = -1; line[4] = 1; line[5] = 1; line[6] = 2; line[7] = 2; } //初始化列 public void initializeColumn(){ column[0] =-1; column[1] =1; column[2] =-2; column[3] =2; column[4] =-2; column[5] =2; column[6] =-1; column[7] =1; } }5.1.4 历史和最优实现
原文地址:http://blog.csdn.net/wendeliang06/article/details/25007431