Rails 把 PHP 能犯的错误都犯了一遍啊,虽然说到 Rails 4 已经不是这样了。
所以,赶紧都升 Rails 4 吧...
“Ruby on Rails, the PHP of a new generation.” — beefhooked
Rails 浑身都是洞啊...
为啥非要学 Rails ...
不过我尝试 prolog 写的数独的效率比我自己实现的差得太远太远了。。难道现在改进 prolog 的人不是这样做的?
sudoku,Prolog 肯定比不过 DLX 的,的确是差好远好远啊 ...
其实运行速度,还好吧,我用下面这样的代码算过,简单的题目也不慢啊
写成这副样子是为了让不会写代码的人自己搞定...
:- use_module(library(clpfd)).
sudoku(Puzzle, Solution) :-
Puzzle = Solution,
Puzzle = [
[ A1, A2, A3, A4, A5, A6, A7, A8, A9 ],
[ B1, B2, B3, B4, B5, B6, B7, B8, B9 ],
[ C1, C2, C3, C4, C5, C6, C7, C8, C9 ],
[ D1, D2, D3, D4, D5, D6, D7, D8, D9 ],
[ E1, E2, E3, E4, E5, E6, E7, E8, E9 ],
[ F1, F2, F3, F4, F5, F6, F7, F8, F9 ],
[ G1, G2, G3, G4, G5, G6, G7, G8, G9 ],
[ H1, H2, H3, H4, H5, H6, H7, H8, H9 ],
[ I1, I2, I3, I4, I5, I6, I7, I8, I9 ]
],
maplist(all_different,
[
[ A1, A2, A3, A4, A5, A6, A7, A8, A9 ],
[ B1, B2, B3, B4, B5, B6, B7, B8, B9 ],
[ C1, C2, C3, C4, C5, C6, C7, C8, C9 ],
[ D1, D2, D3, D4, D5, D6, D7, D8, D9 ],
[ E1, E2, E3, E4, E5, E6, E7, E8, E9 ],
[ F1, F2, F3, F4, F5, F6, F7, F8, F9 ],
[ G1, G2, G3, G4, G5, G6, G7, G8, G9 ],
[ H1, H2, H3, H4, H5, H6, H7, H8, H9 ],
[ I1, I2, I3, I4, I5, I6, I7, I8, I9 ],
[ A1, B1, C1, D1, E1, F1, G1, H1, I1 ],
[ A2, B2, C2, D2, E2, F2, G2, H2, I2 ],
[ A3, B3, C3, D3, E3, F3, G3, H3, I3 ],
[ A4, B4, C4, D4, E4, F4, G4, H4, I4 ],
[ A5, B5, C5, D5, E5, F5, G5, H5, I5 ],
[ A6, B6, C6, D6, E6, F6, G6, H6, I6 ],
[ A7, B7, C7, D7, E7, F7, G7, H7, I7 ],
[ A8, B8, C8, D8, E8, F8, G8, H8, I8 ],
[ A9, B9, C9, D9, E9, F9, G9, H9, I9 ],
[ A1, A2, A3, B1, B2, B3, C1, C2, C3 ],
[ A4, A5, A6, B4, B5, B6, C4, C5, C6 ],
[ A7, A8, A9, B7, B8, B9, C7, C8, C9 ],
[ D1, D2, D3, E1, E2, E3, F1, F2, F3 ],
[ D4, D5, D6, E4, E5, E6, F4, F5, F6 ],
[ D7, D8, D9, E7, E8, E9, F7, F8, F9 ],
[ G1, G2, G3, H1, H2, H3, I1, I2, I3 ],
[ G4, G5, G6, H4, H5, H6, I4, I5, I6 ],
[ G7, G8, G9, H7, H8, H9, I7, I8, I9 ]
]),
append(Puzzle, Rows),
Rows ins 1..9,
labeling([ff], Rows).
见到大牛了多多请教下
我不是什么大牛 ...
当我第一次用 prolog 尝试写个 sudoku solver 的时候我就对 prolog 深表失望了,
其实还好吧,简单的不到一秒就能解出来的 ... 太难的,人脑就更解不出了。
1..你看楼上那个 prolog 代码,完全是充满了重复,完全可以用一个循环来表示那些逻辑嘛
我想写得土一点 ... 对于一个不熟悉 Prolog 的人来说这样容易猜出代码啥意思,反正就这么几行...
即使是最好的 prolog 编译器,实现的自动搜索比得上你自己去发掘出的剪枝或者说其他更高效的算法有效吗?
不能啊,Prolog 可是通用的,必须比不过专门的算法 ...
Prolog 就特别适合解这类脑残的题 ...
没看代码,不知道现在能不能支持多种游戏类型?
还是 Prolog 了
:- use_module(library(clpfd)).
count([], _, 1).
count([H|T], N, C) :-
N #= H,
C #= C1 + 1,
count(T, N, C1).
count([H|T], N, C) :-
N #\= H,
count(T, N, C).
problem(Solution) :-
Solution = [C0, C1, C2, C3, C4, C5, C6, C7, C8, C9],
Solution ins 0..9,
count(Solution, 0, C0),
count(Solution, 1, C1),
count(Solution, 2, C2),
count(Solution, 3, C3),
count(Solution, 4, C4),
count(Solution, 5, C5),
count(Solution, 6, C6),
count(Solution, 7, C7),
count(Solution, 8, C8),
count(Solution, 9, C9).
难道 6,2,1,0,0,0,1,0,0,0
不对?
不该自己拼字符串吧,VALUES (?, ?, ?)
才对吧