/**
* 先來一些測試用例
* 0 0 0 0 0 0 0 0 0
* 0 0 0 0 0 0 0 0 0
* 0 0 0 0 0 0 0 0 0
* 0 0 0 0 0 0 0 0 0
* 0 0 0 0 0 0 0 0 0
* 0 0 0 0 0 0 0 0 0
* 0 0 0 0 0 0 0 0 0
* 0 0 0 0 0 0 0 0 0
* 0 0 0 0 0 0 0 0 0
* * 6 0 0 4 0 0 0 8 0
* 0 0 1 0 0 7 9 0 6
* 0 0 9 2 0 0 4 0 0
* 0 0 5 3 1 0 2 0 0
* 0 9 0 0 0 8 0 1 0
* 0 0 3 7 0 0 5 0 0
* 0 0 6 0 0 3 1 0 0
* 7 0 8 6 0 0 3 0 0
* 0 3 0 0 0 5 0 0 2
* * 7 2 6 9 0 4 0 5 1
* 0 8 0 6 0 7 4 3 2
* 3 4 1 0 8 5 0 0 9
* 0 5 2 4 6 8 0 0 7
* 0 3 7 0 0 0 6 8 0
* 0 9 0 0 0 3 0 1 0
* 0 0 0 0 0 0 0 0 0
* 9 0 0 0 2 1 5 0 0
* 8 0 0 3 0 0 0 0 0
* * 8 0 0 0 0 0 0 0 0
* 0 0 3 6 0 0 0 0 0
* 0 7 0 0 9 0 2 0 0
* 0 5 0 0 0 7 0 0 0
* 0 0 0 0 4 5 7 0 0
* 0 0 0 1 0 0 0 3 0
* 0 0 1 0 0 0 0 6 8
* 0 0 8 5 0 0 0 1 0
* 0 9 0 0 0 0 4 0 0
*/
看到這個是華為的面試題,覺得有點意思,就花了點時間寫了(最後花了大概4個小時t_t慚愧t_t)。大學剛畢業,做個紀念,以後越來越好!!!public
class
test
}long start = system.nanotime();
execute(arr, true);
system.out.println("總共有:" + count + " 種組合!嘗試了:" + trys + "種組合.其中嘗試" + trys2 + "種找到第乙個結果!總耗時:" + (system.nanotime() - start) / 1000000 + "ms," +
"其中第一例耗時:" + (end - start) / 1000000 + "ms");
}/**
* h 縱
* w 橫
**@param arr
*@return
*/public
static string execute(int arr, boolean first)
integer canuse = getsomething(arr, zero);
if (canuse == null || canuse.length < 1)
for (int num : canuse)
string result = execute(arr, false);
if (!"error".equals(result))
found = false;
}arr[zero[0]][zero[1]] = 0;//還原
}return
"error";
}private
static
int getzero(int arr) ;}}
}return
null;
}private
static string print(int arr)
}return sb.tostring().substring(0, sb.length() - 2);
}private
static integer getsomething(int arr, int zero) ;
int temp = getblock(arr, zero[0], zero[1]);
for (int i = 0; i < arr.length; i++)
listlist = new arraylist<>();
for (int i = 0; i < all.length; i++)
}object objects = list.toarray();
integer ret = new integer[objects.length];
return list.toarray(ret);
}private
static
int getblock(int arr, int h, int w)
}return temp;
}private
static
intgetblockid(int h, int w)
}
DFS求解數獨演算法
以前也想過很久解數獨的演算法,但是沒有得到很簡單的方法,某天看到某位學長的 恍然大悟,本以為暴力搜尋會很花時間 10 81種可能 沒想到實際上由於各種限制,列舉次數竟然普遍小於10000次,用dfs便可實現每種可能都列舉。這樣計算乙個數獨就很快了 不到1ms 下面附上自己理解改動並加了注釋的 dev...
用SQL求解數獨 Sudoku
看來用xslt解數獨已經沒有挑戰性了,因為xslt是披著xml外衣的函式程式語言。所以閒人中的閒人們用新時代的彙編 sql,來解決數獨問題。當然不是sql 92了,而是用了oracle10g裡介紹的model語句。這麼看來,鄒鍵老大用sql解簡單演算法題也不算離譜。d oracle的model語句允...
C 演算法 解數獨 條件遞迴
leetcode相關c 演算法解答 編寫乙個程式,通過已填充的空格來解決數獨問題。乙個數獨的解法需遵循如下規則 數字 1 9 在每一行只能出現一次。數字 1 9 在每一列只能出現一次。數字 1 9 在每乙個以粗實線分隔的 3x3 宮內只能出現一次。首先提取當前有的元素,放在三個vector 然後在使...