It crAcks

二进制面前,了无秘密
 
 

常用链接

  • 我的随笔
  • 我的评论
  • 我参与的随笔

留言簿(9)

  • 给我留言
  • 查看公开留言
  • 查看私人留言

随笔档案

  • 2012年5月 (1)
  • 2011年8月 (1)
  • 2011年5月 (1)
  • 2010年5月 (2)
  • 2009年10月 (1)
  • 2009年5月 (2)
  • 2009年3月 (1)
  • 2008年12月 (2)
  • 2008年10月 (1)
  • 2008年9月 (1)
  • 2008年8月 (1)
  • 2008年6月 (1)
  • 2008年5月 (1)
  • 2008年4月 (4)
  • 2008年1月 (1)
  • 2007年12月 (3)
  • 2007年11月 (2)
  • 2007年10月 (1)

文章档案

  • 2011年12月 (1)
  • 2011年9月 (1)
  • 2011年8月 (7)
  • 2011年3月 (1)
  • 2010年11月 (1)
  • 2010年4月 (1)
  • 2010年1月 (1)
  • 2009年12月 (1)

搜索

  •  

最新评论

  • 1. re: VS 2010 SP1 C++编译器绿色命令行版本(32位)
  • 107456279@qq.com
    多谢楼主分享!!!
  • --ttany
  • 2. re: VS 2010 SP1 C++编译器绿色命令行版本(32位)
  • 2629267993@qq.com
    感谢楼主的分享
  • --zh
  • 3. re: VS 2010 SP1 C++编译器绿色命令行版本(32位)
  • 26292679@qq.com
    感谢楼主的分享
  • --zh
  • 4. re: VS 2010 SP1 C++编译器绿色命令行版本(32位)
  • 还有这么好的东西?clj00@qq.com
  • --clj00@qq.com
  • 5. re: VS 2010 SP1 C++编译器绿色命令行版本(32位)
  • leafmaple@qq.com
    谢谢
  • --leafmaple

阅读排行榜

  • 1. Mingw GCC 4.3.0 安装与配置(解决CreateProcess问题)(29446)
  • 2. VS 2010 SP1 C++编译器绿色命令行版本(32位)(6365)
  • 3. VS2010的C++10.0编译器(4446)
  • 4. VS2010 beta 2 C++编译器32位 绿色版(4080)
  • 5. Visual Studio 2011 C++ 编译器命令行绿色版(3142)

评论排行榜

  • 1. VS 2010 SP1 C++编译器绿色命令行版本(32位)(44)
  • 2. Mingw GCC 4.3.0 安装与配置(解决CreateProcess问题)(12)
  • 3. VS2010 beta 2 C++编译器32位 绿色版(6)
  • 4. windows crypto API - Random Generator 的 Cpp 封装(5)
  • 5. Visual Studio 2011 C++ 编译器命令行绿色版(4)

Powered by: 博客园
模板提供:沪江博客
C++博客 | 首页 | 发新随笔 | 发新文章 | 联系 | 聚合 | 管理

sudoku auto solver (python version)

把数独写成如下形式,保存成s.txt,放在同目录下
010000087
005074000
000006040
009063050
000400200
640050300
030005071
000900400
507100003

以下是代码部分
---------------------------------------------------------------------------------------------------------------------------------

# sudoku auto solver by RomanGoliard


import copy

#---------------------------------------------------------------------------------------------------------------------------------
full_set = set([1,2,3,4,5,6,7,8,9])
threshold = 3
SUCCESS = 1
REDUCED = 2
NOT_REDUCED = 3
ERROR = 4


#---------------------------------------------------------------------------------------------------------------------------------
# Read sudoku from the file
def get( L ):
    f = open("s.txt", 'r')
    s = f.read(1)
    while s != '':
        L.append( int(s) )
        s = f.read(1)
        if s == '\n':
            s = f.read(1)

# get the brick's row from the num of brick
def get_row ( num ):
    return num / 9

# get the brick's col from the num of brick
def get_col ( num ):
    return num % 9

# get the brick's block from the num of brick
# blocks are defined as follow:
# 0 1 2
# 3 4 5
# 6 7 8
def get_block ( num ):
    return get_row(num) / 3 * 3 + get_col(num) / 3


#---------------------------------------------------------------------------------------------------------------------------------
# print the sudoku
def output_sudoku( L ):
    for i in range(0, 9):
        for j in range(0, 9):
            print L[i*9 + j],
        print
    print

def print_set( col, row, block, sudoku, brick ):
    for i in range(9):
        if len( col[i] ) < threshold and len( col[i] ) > 0:
            print "col:", i, col[i]
        if len( row[i] ) < threshold and len( row[i] ) > 0:
            print "row:", i, row[i]
        if len( block[i] ) < threshold and len( block[i] ) > 0:
            print "block:", i, block[i]
   
    for i in range(81):
        if sudoku[i] == 0:
            if len( brick[i]) < threshold:
                print i, brick[i]


#---------------------------------------------------------------------------------------------------------------------------------

def clean_list( col, row, block ):
    if len( col ) != 0:
        del col[0 : len(col)]
    if len( row ) != 0:
        del row[0 : len(row)]
    if len( block ) != 0:
        del block[0 : len(block)]

def reset_all( col, row, block ):
    clean_list( col, row, block )

    for i in range(9):
        col.append( set([]) )
        row.append( set([]) )
        block.append( set([]) )

# search and inverse the set
def inverse_set( col, row, block, sudoku ):
    for i in range(81):
        if sudoku[i] != 0:
            col[ get_col(i) ].add( sudoku[i] )
            row[ get_row(i) ].add( sudoku[i] )
            block[ get_block(i) ].add( sudoku[i] )

    for i in range(9):
        col[i] = full_set - col[i]
        row[i] = full_set - row[i]
        block[i] = full_set - block[i]

# search the bricks and find those who has only one element
def uni( col, row, block, sudoku ):
    for i in range(81):
        if sudoku[i] == 0:
            temp = col[ get_col(i) ] & row[ get_row(i) ] & block[ get_block(i) ]
            if len( temp ) == 1:
                sudoku[i] = temp.pop()
                col[ get_col(i) ].remove(sudoku[i])
                row[ get_row(i) ].remove(sudoku[i])
                block[ get_block(i) ].remove(sudoku[i])
                # print i, sudoku[i]

# run and solve the sudoko
def search_sudoku( col, row, block, sudoku, brick ):
    reset_all( col, row, block )
    inverse_set( col, row, block, sudoku )
    uni( col, row, block, sudoku )


#---------------------------------------------------------------------------------------------------------------------------------
def check_set( col, row, block, sudoku, brick ):
    flag = SUCCESS
   
    for i in range(81):
        if sudoku[i] == 0:
            flag = NOT_REDUCED

    if flag == SUCCESS:
        return flag

    for i in range(81):
        if sudoku[i] == 0:
            temp = col[get_col(i)] & row[get_row(i)] & block[get_block(i)]
            if len( brick[i] ) > len( temp ):
                brick[i] = temp
                flag = REDUCED
            if len( brick[i] ) == 0:
                return ERROR
    return flag

def loop_search( col, row, block, sudoku, brick ):
    search_sudoku( col, row, block, sudoku, brick )
    value = check_set( col, row, block, sudoku, brick )

    while value == REDUCED:
        search_sudoku( col, row, block, sudoku, brick )
        value = check_set( col, row, block, sudoku, brick )
    return value
   
#---------------------------------------------------------------------------------------------------------------------------------

# sandbox test
def sandbox( col, row, block, sudoku, brick ):
    for i in range( 81 ):
        for j in range( 81 ):
            if sudoku[i] == 0 and sudoku[j] == 0:
                if len( brick[i] ) > 0 and len( brick[j] ) > 0:
                    remain_i, remain_j = set([]), set([])
                    for e in brick[i]:
                        for f in brick[j]:
                            sudoku[i], sudoku[j] = e, f
                            value = loop_search( copy.deepcopy( col ), copy.deepcopy( row ), copy.deepcopy( block ), copy.deepcopy( sudoku ), copy.deepcopy( brick ) )
                            sudoku[i], sudoku[j] = 0, 0
                            if value != ERROR:
                                remain_i.add( e )
                                remain_j.add( f )

                    if len ( remain_i ) < len( brick[i] ) and len( remain_i ) > 0:
                        brick[i] = remain_i
                    if len ( remain_j ) < len( brick[j] ) and len( remain_j ) > 0:
                        brick[j] = remain_j

                    if len( brick[i] ) == 1:
                        for e in brick[i]:
                            sudoku[i] = e
                            loop_search( col, row, block, sudoku, brick )

                    if len( brick[j] ) == 1:
                       for e in brick[j]:
                            sudoku[j] = e
                            loop_search( col, row, block, sudoku, brick )
# main -------------------------------------------------------------------------------------------
cols = []
rows = []
blocks = []
bricks = []
sudokus = []


get(sudokus)

for i in range(81):
    bricks.append(full_set)

if loop_search( cols, rows, blocks, sudokus, bricks ) == SUCCESS:
    print "Found solution!"
else:
    sandbox( cols, rows, blocks, sudokus, bricks )

output_sudoku( sudokus )


# write back to file
# ......
# write back to file

 

 

 

posted @ 2008-01-16 19:55 RomanGol 阅读(445) | 评论 (0) | 编辑 收藏
 
Mingw GCC 4.2.2 + Boost Lib 打包下载
http://www.ipccare.com/romangol/tools/mingw4.2.2.rar
在Windows操作系统下,解压到一个目录,将path设置为该目录下的bin目录,就可以使用4.2.2的GCC了,

http://www.ipccare.com/romangol/tools/Boost_LibTorrent_Zlib.rar
这是一个比较老的Boost的版本的编译后的lib,最近可能会放上新的lib
posted @ 2007-12-28 17:30 RomanGol 阅读(2104) | 评论 (2) | 编辑 收藏
 
为什么会有no newline warning

这是GCC编译器的一个常见的warning

假设有如下关系:

foo.h:
blah blah blah<no newline>

bar.c:
#include "foo.h"
#include "grill.h"

如果预处理器不够聪明,不知道自动加上换行,那么就有...
 blah blah blah#include "grill.h"

grill.h 就有可能没有包含进去

 

posted @ 2007-12-23 10:02 RomanGol 阅读(184) | 评论 (0) | 编辑 收藏
 
静态链接库生成小小备忘

Windows下静态库.lib生成:
1.先将mylib.c生成mylib.obj
cl.exe /c mylib.c  

2.用lib.exe生成mylib.lib文件
lib /OUT:my.lib mylib.obj  

注:如果要生成exe文件,才使用link.exe对obj文件进行编译链接,否则不用link.exe



Linux下静态库.a生成
1.将各函数代码所在的源文件编译成目录文件。例如,对于myfunc.c,可以用如下命令 将其编译成目标文件:
gcc -c myfunc.c
当然在有多个源文件时,只需在gcc 命令行中将其分别列上就可以了。经此一步,将能够得到各源文件的目标文件。对上例,将得到myfunc.o

2.将各目标文件收集起来放到一个静态库文件中。这主要借助于ar命令完成,如:
ar r ~/lib/libtest.a myfunc.o

注:建立动态链接库、并不需要用到其他的工具,借助于gcc命令即可完成。此时需在命令 行中加上-K  PIC和-G这两个选项,如下我们可以建立libtest的动态版本:
gcc -K PIC -G -o $HOME/lib/libtest.so myfunc.c

 

posted @ 2007-12-01 21:35 RomanGol 阅读(1015) | 评论 (1) | 编辑 收藏
 
The mathematical way of thinking
好不容易找到的文章,来自伟大的数学家Hermann Weyl,虽然是1940年的文章,但是值得一读。做计算机的,要好好修炼数学才行。

http://www.cppblog.com/Files/romangol/weyl%20(1940)%20-%20the%20mathematical%20way%20of%20thinking.rar
posted @ 2007-11-30 22:02 RomanGol 阅读(509) | 评论 (1) | 编辑 收藏
 
隐写术

原文发在BBS上,转载过来,《密码编码学与网络安全:原理与实践》上的一道题,课本中提示为隐写术

In one of Dorothy Sayers's mysteries, Lord Peter is confronted with the messag
e shown below. He also discovers the key to the message, which is a sequence o
f integers:


7876565434321123434565678788787656543432112343456567878878765654433211234

message:

I thought to see the fairies in the fields, but I saw only the evil elephants
with their black backs. Woe! how that sight awed me! The elves danced all arou
nd and about while I heard voices calling clearly.Ah! how I tried to see-throw
off the ugly cloud-but no blind eye of a mortal was permitted to spy them. So
then came minstrels, having gold trumpets, harps and drums. These played very
loudly beside me, breaking that spell. So the dream vanished, whereat I thank
ed Heaven. I shed many tears before the thin moon rose up, frail and faint as
a sickle of straw. Now though the Enchanter gnash his teeth vainly, yet shall
he return as the spring returns. Oh, wretched man! Hell gapes, Erebus now lies
open. The mouths of Death wait on thy end.

(a) Decrypt the message. [Hint: What is the largest integer value?]
(b) If the algorithm is known but not the key, how secure is the scheme?
(c) If the key is known but not the algorithm, how secure is the scheme?

至少在Google和Baidu上没找到答案
就做第一问了

写了一个小程序

#include <iostream>
#include <string>
#include <fstream>


using namespace std;

int main()
{
   ifstream in("cipher.txt");
   string key;
   string line;
 
   getline(in, key);
 
   int count = 0;
 
   
   while (getline(in, line))
   {
      cout << line.at(key.at(count++) - '1');
   }
 
    in.close();
 
    return 0;
}

同时把密文中所有的空格,标点和换行全部删去,用EmEditor的宏功能将字符串拆分成8个字一行(这个真是个好东东吖,尤其是它的宏录制功能,用它,比用什么perl写个脚本处理字符串爽吖,只管按F4键就好),生成一个文件cipher.txt
7876565434321123434565678788787656543432112343456567878878765654433211234
Ithought
toseethe
fairiesi
nthefiel
dsbutIsa
wonlythe
evilelep
hantswit
htheirbl
ackbacks
Woehowth
atsighta
wedmeThe
elvesdan
cedallar
oundanda
boutwhil
eIheardv
oicescal
lingclea
rlyAhhow
Itriedto
seethrow
offtheug
lycloudb
utnoblin
deyeofam
ortalwas
permitte
dtospyth
emSothen
camemins
trelshav
inggoldt
rumpetsh
arpsandd
rumsThes
eplayedv
eryloudl
ybesidem
ebreakin
gthatspe
llSothed
reamvani
shedwher
eatIthan
kedHeave
nIshedma
nytearsb
eforethe
thinmoon
roseupfr
ailandfa
intasasi
ckleofst
rawNowth
oughtheE
nchanter
gnashhis
teethvai
nlyyetsh
allheret
urnasthe
springre
turnsOhw
retchedm
anHellga
pesErebu
snowlies
openThem
outhsofD
eathwait
onthyend

然后运行程序,得到如下字符
hesittethbetweenthecherubimstheislesmaybegladtHereofastheriversintHesouth

分割一下
he sitteth between the cherubims.the isles may be glad thereof as the rivers in the south.

posted @ 2007-11-02 01:39 RomanGol 阅读(1305) | 评论 (2) | 编辑 收藏
 
内存对齐指令
通常来说内存对齐很能提高速度的,使用如下指令在两个操作系统下面令内存对齐

#ifdef _WIN32
   typedef __declspec(align(16)) struct
#elif _LINUX
   typedef __attribute__((aligned(16))) struct
#endif

这里有一篇讲得很好的文章
http://blog.vckbase.com/panic/archive/2005/04/02/4340.aspx
posted @ 2007-10-03 11:50 RomanGol 阅读(350) | 评论 (0) | 编辑 收藏
 
仅列出标题
共3页: 1 2 3