public class BFS1_6 {
static int[] arrays = {1,2,3,4,5,6,7};
static int[] shwoArrays = new int[arrays.length];
static int[] mark = new int[arrays.length];
static int count = 0 ;
public static void dfs(int step){
if (step == arrays.length){
for(int value : shwoArrays){
System.out.print(value+" ");
}
count++;
System.out.println();
return;
}
for (int i = 0;i<arrays.length;i++){
if (mark[i] == 0){
mark[i] = 1;
shwoArrays[step] = arrays[i];
dfs(step + 1);
mark[i] = 0;
}
}
}
public static void main(String[] args) {
// TODO code application logic here
dfs(0);
System.out.println(count);
}
}
排列種類數量
2 2
3 6
4 24
5 120
7 5040
公式
2 * (N1 * N2*N3...)
如要計算
5 的排列種類數量
2 * 3 * 4 * 5 = 120
開這網站的目的是發現有很多人想了解電腦相關資訊,補習費用也不便宜,下了班很累想休息一下時間無法配合於是想分享我所學 未來會包含課程規劃會有: 程式語言上:Java、C#、PHP.. 手機設計:Android,、ObjectC 認證包含:SCJP、SCWCD、LPI1~LPI2.... 網頁設計:HTML5、JavaScript.... 陸陸續續會再增加! 以上課程都是我本人教學如有任何問題可在此留言我會答覆您的
HTML/JavaScript小工具
HTML/JavaScript小工具
2016年6月24日 星期五
深度優先算法
package 演算法;
public class 深度優先算法_2 {
/*
1 表示可連接的線
0 表示自己
999 表示無法連線
邏輯如下:
1 先找到一個點
2 看看跟哪個點有連線
3 在進到有連線的點
4 依照路徑一層一層找
*/
public static int[][] myTree = { {0,1,1,999,1},
{1,0,999,1,999},
{1,999,0,999,1},
{999,1,999,0,999},
{1,999,1,999,0} };
static int sum = 0;
static int[] mark = new int[myTree.length];//記錄哪些點走過,不需要再走了
public static void dfs(int point){
System.out.println(point);//目前在哪個點
sum++;//記錄一共走了多少點
if (sum == myTree.length){
return;
}
for (int i = 0 ; i < myTree.length; i++){
if (mark[i] == 0 && myTree[point][i] == 1){
mark[i] = 1;
dfs(i);
}
}
}
public static void main(String[] args) {
mark[0] = 1;
dfs(0);
}
}
2016年6月15日 星期三
三消遊戲基本演算法
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package 演算法;
/**
*橫向存點
垂直存點
觸發點
每格都如此
往右邊走
如果 碰到相同
Save 橫向存點
如果 到邊界 或是 不相同
return
回到觸發點
往左邊走
如果 碰到相同
Save 橫向存點
如果 到邊界 或是 不相同
return
回到觸發點
往上邊走
如果 碰到相同
Save 垂直存點
如果 到邊界 或是 不相同
return
回到觸發點
往下邊走
如果 碰到相同
Save 垂直存點
如果 到邊界 或是 不相同
return
完成計算需要消除的點
清空除存點
* @author howard
*/
public class 三消演算dfs_1 {
static int[][] directArrays = {{0,1},
{0,-1},
{-1,0},
{1,0}};
static int[][] threeMatch={
{0,0,0,0,0,0},
{0,0,1,0,0,0},
{1,1,1,0,0,0},
{0,0,1,0,0,0},
{0,0,1,0,0,0}
};
public static void triggerPoint(int x,int y){
for (int i = 0;i<directArrays.length;i++){
int[] direct = directArrays[i];
int nextX = x + direct[0];
int nextY = y + direct[1];
int matchNumber = threeMatch[x][y];
nextPoint(matchNumber,nextX,nextY,direct);
}
}
public static void nextPoint(int matchNumber, int nextX,int nextY,int[] direct){
if (nextX < 0 || nextX == threeMatch.length ||
nextY < 0 || nextY == threeMatch[0].length ||
threeMatch[nextX][nextY] != matchNumber){
System.out.println("=======================");
return;
}
System.out.println("X:"+nextX+"Y:"+nextY);
nextX += direct[0];
nextY += direct[1];
nextPoint(matchNumber,nextX,nextY,direct);
}
public static void main(String[] args) {
triggerPoint(2,2);
}
}
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package 演算法;
/**
*橫向存點
垂直存點
觸發點
每格都如此
往右邊走
如果 碰到相同
Save 橫向存點
如果 到邊界 或是 不相同
return
回到觸發點
往左邊走
如果 碰到相同
Save 橫向存點
如果 到邊界 或是 不相同
return
回到觸發點
往上邊走
如果 碰到相同
Save 垂直存點
如果 到邊界 或是 不相同
return
回到觸發點
往下邊走
如果 碰到相同
Save 垂直存點
如果 到邊界 或是 不相同
return
完成計算需要消除的點
清空除存點
* @author howard
*/
public class 三消演算dfs_1 {
static int[][] directArrays = {{0,1},
{0,-1},
{-1,0},
{1,0}};
static int[][] threeMatch={
{0,0,0,0,0,0},
{0,0,1,0,0,0},
{1,1,1,0,0,0},
{0,0,1,0,0,0},
{0,0,1,0,0,0}
};
public static void triggerPoint(int x,int y){
for (int i = 0;i<directArrays.length;i++){
int[] direct = directArrays[i];
int nextX = x + direct[0];
int nextY = y + direct[1];
int matchNumber = threeMatch[x][y];
nextPoint(matchNumber,nextX,nextY,direct);
}
}
public static void nextPoint(int matchNumber, int nextX,int nextY,int[] direct){
if (nextX < 0 || nextX == threeMatch.length ||
nextY < 0 || nextY == threeMatch[0].length ||
threeMatch[nextX][nextY] != matchNumber){
System.out.println("=======================");
return;
}
System.out.println("X:"+nextX+"Y:"+nextY);
nextX += direct[0];
nextY += direct[1];
nextPoint(matchNumber,nextX,nextY,direct);
}
public static void main(String[] args) {
triggerPoint(2,2);
}
}
訂閱:
文章 (Atom)