/*
* 馬踏棋盤問題:(貪婪法求解)
* 棋盤有64個位置,“日”字走法,剛好走滿整個棋盤
*/ //下一個走法的方向類
class Direction{
int x;
int y;
int wayOutNum;
} public class Hores_chessboard_1 {
static final int[] dx = { -2, -1, 1, 2, 2, 1, -1, -2 }; // x方向的增量
static final int[] dy = { 1, 2, 2, 1, -1, -2, -2, -1 }; // y方向的增量
static final int N = 8;
static int[][] chessboard = new int[N][N]; // 棋盤 /**
*
* @param nami
* @param x,y爲棋子的位置
* @return 如果棋子的位置不合法,則返回一個大於8的數。
* 否則返回棋子的下個出路的個數
*/
static int wayOut(int x, int y){
int count = 0;
int tx, ty, i;
//判斷是否超出棋盤邊界,該位置是否已經下過
if(x<0 || x>7 || y<0 || y>7 || chessboard[x][y]!=0){
return 9;
}
for(i=0; i<N; i++){
tx = x+dx[i];
ty = y+dy[i];
//如果棋子的下個出路可行,則出路數自加一次
if(tx>-1 && tx<8 && ty>-1 && ty<8 && chessboard[tx][ty]==0)
count++;
}
return count;
} /**
* 按照棋子的下個出路的個數從低到高排序
* @param next 棋子的八個位置的數組
*/
static void sort(Direction[] next){
int i, j, index;
Direction temp = null;
//這裏用的選擇排序
for(i=0; i<N; i++){
index = i;
for(j=i+1; j<N; j++){
if(next[index].wayOutNum > next[j].wayOutNum)
index = j;
}
if(i != index){
temp = next[i];
next[i] = next[index];
next[index] = temp;
}
}
} static void Move(int x, int y, int step){
int i, j;
int tx, ty;
//如果step==64,則說明每個棋格都走到了,現在只需要打印結果就完了
if(step == N*N){
for(i=0; i<N; i++){
for(j=0; j<N; j++){
System.out.printf("%3d", chessboard[i][j]);
}
System.out.println();
}
System.exit(0);
} //下一個棋子的N個位置的數組
Direction[] next = new Direction[N]; for(i=0; i<N; i++){
Direction temp = new Direction();
temp.x = x+dx[i];
temp.y = y+dy[i];
next[i] = temp;
//循環得到下個棋子N處位置的下個出路的個數
next[i].wayOutNum = wayOut(temp.x, temp.y);
} //配合貪婪算法,按下個棋子的下個出路數排序後,next[0]就是下個出路數最少的那個
sort(next); for(i=0; i<N; i++){
tx = next[i].x;
ty = next[i].y;
chessboard[tx][ty] = step;
Move(tx, ty, step+1);
/*如果上面Move()往下一步走不通,則回溯到這裏
重置chessboard[tx][ty]爲0,接着i++,又循環...... */
chessboard[tx][ty] = 0;
}
} public static void main(String[] args) {
int i, j;
//初始化棋盤
for(i=0; i<8; i++){
for(j=0; j<8; j++){
chessboard[i][j] = 0;
}
}
System.out.println("請輸入棋子開始位置(0-7):");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
//第一步不用比較,賦值第一步
chessboard[x][y] = 1;
Move(x, y, 2);
}
}

Java演算法-「馬踏棋盤問題」的更多相关文章

  1. Java演算法-「雞兔同籠問題」

    /** * 雞兔同籠問題:窮舉算法思想 */ import java.util.*; public class ChichenAndHabbit { static int chichenNum,hab ...

  2. Java演算法之堆排序(HeapSort)

    import java.util.Arrays; publicclass HeapSort { inta[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,9 ...

  3. Java演算法之快速排序法

    1 * 快速排序法(Quick Sort),遞迴版本. 2 * 3 * @param array 傳入要排序的陣列 4 * @param start 傳入要排序的開始位置 5 * @param end ...

  4. JAVA演算法---約瑟夫問題

    1 public class Josephus { public static int[] arrayOfJosephus( int number, int per) { 3 int[] man = ...

  5. 何解決 LinqToExcel 發生「無法載入檔案或組件」問題何解決 LinqToExcel 發生「無法載入檔案或組件」問題

    在自己的主機上透過 Visual Studio 2013 與 IISExpress 開發與測試都還正常,但只要部署到測試機或正式機,就是沒辦法順利執行,卡關許久之後找我協助.我發現錯誤訊息確實很「一般 ...

  6. 【JAVA今法修真】 第一章 今法有万象 百家欲争鸣

    大家好,我是南橘,因为这段时间很忙,忙着家里的事情,忙着工作的事情,忙着考试的事情,很多时候没有那么多经历去写新的东西,同时,也是看了网上一些比较新颖的文章输出方式,自己也就在想,我是不是也可以这样写 ...

  7. Android内存管理(4)*官方教程 含「高效内存的16条策略」 Managing Your App's Memory

    Managing Your App's Memory In this document How Android Manages Memory Sharing Memory Allocating and ...

  8. 八皇后問題 (C語言递归實現 回溯法)

    八皇后问题是一个以国际象棋为背景的问题:怎样可以在 8×8 的国际象棋棋盘上放置八个皇后,使得不论什么一个皇后都无法直接吃掉其它的皇后?为了达到此目的.任两个皇后都不能处于同一条横行.纵行或斜线上.現 ...

  9. 【Java算法學習】斐波那契數列問題-兔子產子經典問題

    /** * 用遞推算法求解斐波那契數列:Fn = Fn-2 +Fn-1; */ import java.util.*; public class Fibonacci { public static v ...

随机推荐

  1. 在deepin 15.5中安装vs code并配置c/c++环境

    原文地址:https://blog.csdn.net/DefetC/article/details/79946100 参考了以下几篇文章: https://www.zhihu.com/question ...

  2. Lua中的语句

    [赋值] 赋值的基本含义是修改一个变量或一个table中字段的值,这个和其它语言没有多少区别,但是对于Lua,有一个特性,它允许“多重赋值”,也就是一下子将多个值赋予多个变量,例如以下代码: , pr ...

  3. SpringBoot入门基础

    目录 SpringBoot入门 (一) HelloWorld. 2 一 什么是springboot 1 二 入门实例... 1 SpringBoot入门 (二) 属性文件读取... 16 一 自定义属 ...

  4. 【原创】Linux基础之linux常用命令之文本替换

    linux常用命令之文本替换 1 vi vi test_file :%s/h/h1/g 注释:全文替换,将h替换为h1 :1,4s/h/h1/g 注释:将第1行到第4行的h替换为h1 :%s/\n/, ...

  5. 解决 CentOS7 安装完成后ifconfig命令不能用

    今天用VMWare安装了CentOS7,选择了最小安装包模式,安装完毕之后想查看一下本机的ip地址,发现报错 # ifconfig -bash: ifconfig: command not found ...

  6. js通过高德地图获取当前位置的经度纬度

    效果图如下: 已经获取到了经度纬度了 代码如下: <!doctype html> <html> <head> <meta charset="utf- ...

  7. 详解 CAP 定理 Consistency(一致性)、 Availability(可用性)、Partition tolerance(分区容错性)

    CAP原则又称CAP定理,指的是在一个分布式系统中,Consistency(一致性). Availability(可用性).Partition tolerance(分区容错性),三者不可得兼. 分布式 ...

  8. (四)Knockout 表单

    click <div> You've clicked <span data-bind="text: numberOfClicks"></span> ...

  9. 树莓派做coolpy服务器

    安装前需要了解的 1. coolpy是一个基于NodeJS的物联网平台(官网http://icoolpy.com). 注:国内物联网平台有乐联网,yeelink等,但只有coolpy是开源的. 2. ...

  10. BZOJ3257 [Zjoi2014]力 多项式 FFT

    原文链接http://www.cnblogs.com/zhouzhendong/p/8762639.html 题目传送门 - BZOJ3527 题意 给出长度为$m$的序列$q_{1..m}$,让你输 ...