Java演算法-「馬踏棋盤問題」
/*
* 馬踏棋盤問題:(貪婪法求解)
* 棋盤有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演算法-「馬踏棋盤問題」的更多相关文章
- Java演算法-「雞兔同籠問題」
/** * 雞兔同籠問題:窮舉算法思想 */ import java.util.*; public class ChichenAndHabbit { static int chichenNum,hab ...
- 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 ...
- Java演算法之快速排序法
1 * 快速排序法(Quick Sort),遞迴版本. 2 * 3 * @param array 傳入要排序的陣列 4 * @param start 傳入要排序的開始位置 5 * @param end ...
- JAVA演算法---約瑟夫問題
1 public class Josephus { public static int[] arrayOfJosephus( int number, int per) { 3 int[] man = ...
- 何解決 LinqToExcel 發生「無法載入檔案或組件」問題何解決 LinqToExcel 發生「無法載入檔案或組件」問題
在自己的主機上透過 Visual Studio 2013 與 IISExpress 開發與測試都還正常,但只要部署到測試機或正式機,就是沒辦法順利執行,卡關許久之後找我協助.我發現錯誤訊息確實很「一般 ...
- 【JAVA今法修真】 第一章 今法有万象 百家欲争鸣
大家好,我是南橘,因为这段时间很忙,忙着家里的事情,忙着工作的事情,忙着考试的事情,很多时候没有那么多经历去写新的东西,同时,也是看了网上一些比较新颖的文章输出方式,自己也就在想,我是不是也可以这样写 ...
- Android内存管理(4)*官方教程 含「高效内存的16条策略」 Managing Your App's Memory
Managing Your App's Memory In this document How Android Manages Memory Sharing Memory Allocating and ...
- 八皇后問題 (C語言递归實現 回溯法)
八皇后问题是一个以国际象棋为背景的问题:怎样可以在 8×8 的国际象棋棋盘上放置八个皇后,使得不论什么一个皇后都无法直接吃掉其它的皇后?为了达到此目的.任两个皇后都不能处于同一条横行.纵行或斜线上.現 ...
- 【Java算法學習】斐波那契數列問題-兔子產子經典問題
/** * 用遞推算法求解斐波那契數列:Fn = Fn-2 +Fn-1; */ import java.util.*; public class Fibonacci { public static v ...
随机推荐
- 2.6 if嵌套
if嵌套 通过学习if的基本用法,已经知道了 当需要满足条件去做事情的这种情况需要使用if 当满足条件时做事情A,不满足条件做事情B的这种情况使用if-else 想一想: 坐火车或者地铁的实际情况是: ...
- Coursera, Big Data 3, Integration and Processing (week 1/2/3)
This is the 3rd course in big data specification courses. Data model reivew 1, data model 的特点: Struc ...
- centos7.2 环境下两个数据库的安装部署
首先假如服务器上已经有一个 数据库mysql5.6.29,端口是3306. 接下来在安装一个mysql数据库,端口是3307的. 一:创建mysql编译目录 mkdir /usr/local/mysq ...
- @ReponseBody返回的json中文乱码-遁地龙卷风
我在mvc配置文件中加上下面这个配置就好了 <mvc:annotation-driven></mvc:annotation-driven>,需要在开头引用如下命名空间xmlns ...
- PowerDesigner使用总结(转)
PowerDesigner使用总结一.使用PowerDesigner生成HTML功能 使用PowerDesigner设计数据库关系以后,可以生成HTML,供团队成员进行讨论. Step 1:创建一个n ...
- c++ 智能指针用法详解
本文介绍c++里面的四个智能指针: auto_ptr, shared_ptr, weak_ptr, unique_ptr 其中后三个是c++11支持,并且第一个已经被c++11弃用. 为什么要使用智能 ...
- Machine Schedule poj1325
Machine Schedule Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17454 Accepted: 7327 ...
- Python-Django-Ajax进阶3
1 中间件 -是什么? 中间件顾名思义,是介于request与response处理之间的一道处理过程,相对比较轻量级,并且在全局上改变django的输入与输出.因为改变的是全局,所以需要谨慎实用,用不 ...
- springboot配置Druid数据源
springboot配置druid数据源 Author:SimpleWu springboot整合篇 前言 对于数据访问层,无论是Sql还是NoSql,SpringBoot默认采用整合SpringDa ...
- python3 字典(dictionary)(一)
一.定义:是另一种可变容器模型,可存储任意类型对象:(也被称为关联数组或哈希表:存储的数据是没有顺序的) 语法为: d = {key1 : value1, key2 : value2 } #----- ...