从MSN上看到这个签名,耐不住性子简单测试下,结果确实挺令人兴奋。
测试代码(暴力搜索求解soduku):
$count = 0 def valid?(state, x, y) # check in col and row 0.upto(8) do |i| return false if i != y and state[x][i] == state[x][y] return false if i != x and state[i][y] == state[x][y] end # check in box x_from = (x / 3) * 3 y_from = (y / 3) * 3 x_from.upto(x_from + 2) do |xx| y_from.upto(y_from + 2) do |yy| return false if (xx != x or yy != y) and state[xx][yy] == state[x][y] end end true end def next_state(state, x, y) $count = $count + 1 y = 0 and x = x + 1 if y == 9 return true if x == 9 unless state[x][y].zero? return false unless valid?(state, x, y) return next_state(state, x, y + 1) else 1.upto(9) do |i| state[x][y] = i return true if valid?(state, x, y) and next_state(state, x, y + 1) end end state[x][y] = 0 false end start = [ [ 0, 0, 0, 4, 0, 5, 0, 0, 1 ], [ 0, 7, 0, 0, 0, 0, 0, 3, 0 ], [ 0, 0, 4, 0, 0, 0, 9, 0, 0 ], [ 0, 0, 3, 5, 0, 4, 1, 0, 0 ], [ 0, 0, 7, 0, 0, 0, 4, 0, 0 ], [ 0, 0, 8, 9, 0, 1, 0, 0, 0 ], [ 0, 0, 9, 0, 0, 0, 6, 0, 0 ], [ 0, 8, 0, 0, 0, 0, 0, 2, 0 ], [ 4, 0, 0, 2, 0, 0, 0, 0, 0 ] ] start_time = Time.new if next_state(start, 0, 0) puts "time elapsed: #{Time.new - start_time} sec." puts "count: #{$count}" start.each do |val| puts val.join(" ") end else puts "Not solveable!" end
CPP代码:
/* * File: newmain.cpp * Author: xhan * * Created on 2009年1月3日, 下午3:11 */ #include <stdlib.h> #include <cstdio> #include <iostream> using namespace std; /* *a simple soduku calculater */ int matrix[9][9]= { { 0, 0, 0, 4, 0, 5, 0, 0, 1 }, { 0, 7, 0, 0, 0, 0, 0, 3, 0 }, { 0, 0, 4, 0, 0, 0, 9, 0, 0 }, { 0, 0, 3, 5, 0, 4, 1, 0, 0 }, { 0, 0, 7, 0, 0, 0, 4, 0, 0 }, { 0, 0, 8, 9, 0, 1, 0, 0, 0 }, { 0, 0, 9, 0, 0, 0, 6, 0, 0 }, { 0, 8, 0, 0, 0, 0, 0, 2, 0 }, { 4, 0, 0, 2, 0, 0, 0, 0, 0 } }; int cnt =0; int valid(int x, int y) { int posVal = matrix[x][y]; for(int i=0; i <9 ; ++i) { //col and row if(i != y && posVal == matrix[x][i]) return 0; if(i != x && posVal == matrix[i][y]) return 0; } // check in 3*3 area int xfrom = (x/3)*3; int yfrom = (y/3)*3; for(int j=xfrom; j < xfrom+3 ; ++j) for(int k=yfrom; k < yfrom+3 ; ++k) if( posVal == matrix[j][k] && (j != x || k != y) ) return 0; return true; } // recursion calculate each sequencing value whether is valid int check(int x,int y) { ++cnt; //next row if(y==9){ y=0; ++x; } //get solution if( x == 9) { return true; } if(matrix[x][y] != 0) { if( valid(x,y) ) return check(x,y+1); else return false; } else for(int rnd=1; rnd <=9 ;++rnd) { matrix[x][y] = rnd; if(valid(x,y)) if(check(x,y+1)) return true; } matrix[x][y] = 0; return false; } int main(int argc, char** argv) { if(check(0,0)) { for(int j=0; j<9; ++j) for(int k=0; k<9; ++k){ printf(" %d",matrix[j][k]); if(k ==8) putchar('\n'); } cout<<"end count:"<<cnt<<endl; } else puts("cannot get the solution!"); return (EXIT_SUCCESS); }
最终测试结果(本人机器CPU AMD3000 +1G DDRII):
ruby 1.8.7 (2008-08-11 patchlevel 72) [i686-linux] | real 14.155s |
ruby 1.9.1 (2008-12-30 patchlevel-0 revision 21203) [i686-linux] | real 4.390s |
G++ | real 0m0.119s |
有此可见,ruby新版性能有了极大的飞跃,不过还是有很大的改进空间。