数独 RUBY 算法、baltctf-2013-sudoku
来源:互联网 发布:海尔网络电视机 编辑:程序博客网 时间:2024/05/23 17:45
根据牛人PYTHON改的 数独 RUBY 算法,以及牛人的PYTHON数独生成算法。
#!/usr/bin/env rubyrequire "socket"require "pp"s = TCPSocket.new 'localhost', 3s.recv 1024b = s.recv 1024p b if $DEBUGout = []outout = [[],[],[],[],[],[],[],[],[]]size = b.size0.upto(size-1) do | i |if b[i] =~ (/\d/)out << b[i]elsif b[i] =~ (/_/)out << b[i]endendfor x in 0..80 dotmp = x.divmod(9)[0]outout[tmp] << out[x]endback = [[],[],[],[],[],[],[],[],[]]pp outout if $DEBUGguess = -1solved = 0while 1p "guess: #{guess}" if $DEBUGfound = 0 0.upto(8) do | x |print "x: #{x}" if $DEBUG0.upto(8) do | y |all = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]print "y: #{y}" if $DEBUGif outout[x][y] == "_"0.upto(8) do | yy |all.delete(outout[x][yy]) #print "x : #{x}, y: #{yy} "end0.upto(8) do | xx |all.delete(outout[xx][y])end( 3 * (x/3) ).upto( 3 * (x/3) + 2 ) do | zx |( 3 * (y/3) ).upto( 3 * (y/3) + 2 ) do | zy |all.delete(outout[zx][zy])endendif all.size == 1outout[x][y] = all[0]found = 1p x if $DEBUGp y if $DEBUGpp all if $DEBUGpp outout if $DEBUGelsif all.size == 0#guess wrongp "guess failed!!!!!!!!!!!!!!!!!!!!!!!!!" if $DEBUGpp outout if $DEBUG0.upto(8) do | i |0.upto(8) do | j |outout[i][j] = back[i][j]endendp "outout recover: " if $DEBUGpp outout if $DEBUGback = [[],[],[],[],[],[],[],[],[]]p "back . clear" if $DEBUGp back if $DEBUGfound = 0elsif guess != -1 && guess == xr = rand(all.size)p "guess line#{x}," if $DEBUGp all[r] if $DEBUGoutout[x][y] = all[r]guess = -1found = 1endendendendguess = -1if found == 0#if solvedsolution = "solution "outout.flatten.each do | one |solution << oneendp solution if $DEBUGif /_/ =~ solution#unsolved, guessguess = rand(9)p guess if $DEBUGif back == [[],[],[],[],[],[],[],[],[]]0.upto(8) do | i |0.upto(8) do | j |back[i][j] = outout[i][j]endendp "back outout: " if $DEBUGpp back if $DEBUGendelsep "SOLUTION: #{solution}"solved += 1s.send(solution, solution.size)p "Solved: #{solved}x!"if solved == 1002.times dop s.recv(1024)endbreakendwhile 1b = s.recv(1024)if /----/ =~ bbreakendendp b if $DEBUGout = []outout = [[],[],[],[],[],[],[],[],[]]size = b.size0.upto(size-1) do | i | if b[i] =~ (/\d/) out << b[i] elsif b[i] =~ (/_/) out << b[i] endendfor x in 0..80 do tmp = x.divmod(9)[0] outout[tmp] << out[x]endback = [[],[],[],[],[],[],[],[],[]]pp outout if $DEBUGguess = -1nextendendpp "back -----" if $DEBUGpp back if $DEBUGends.close
生成算法:
#!/usr/bin/env python # -*- coding: UTF-8 -*- import random, socket, signal, sys, re, os, time from copy import copy, deepcopy FLAG="328df3b4525e060de963a20d3cc86579" PORT=3 Timeout=10 slow="""__________________________________________________________________ __________________________________________________________________ ______________________________________†888888‡†___________________ _______________________________8†_†††††††††††††††††8†_____________ ___________________________†‡_____††††††††††††††††††††8___________ ________________________‡†_______†‡‡‡‡‡‡‡8††††††††††††_†‡†________ ____________________‡†____†‡‡†_______________‡††††††††††††8_______ _______________________________________________†‡†††††††††‡‡______ _________________________________________________8†††††††††‡‡†____ ___________________________________________________††††††††‡‡†8___ ___________________________________________________8†††††††‡‡‡‡___ ___________________________________________________8†††††††‡‡‡†___ ___________________________________________________8††††††‡‡‡††___ ___________________________†‡‡†††††††††††††‡8†_____††††††‡‡‡‡‡†___ _______________________†8††††††††††††††††††††††8†_8‡††††‡‡‡‡‡‡‡___ ____________________‡††††††††††††††††††††††††††††††‡†††‡‡‡‡‡‡†8___ ________‡‡††‡_†8‡††††††††††88†‡8†††‡8†††††††††††††††‡‡‡‡†‡‡‡‡‡____ ______††††††8††††††††††††††††8†††888††8††††††††††††‡‡†8††††††‡____ ______8†‡†8††††††††††††††††††‡†8††††††8††††††††††††‡‡‡‡¶††††8_____ _______‡‡__8†††††††††8____‡‡††††‡†††††‡†††††††††††‡‡‡‡††8†††______ ________‡¶_8††††††_†8_______†††††‡8888††††††††††‡‡‡‡‡‡††‡8†_______ _______‡†‡†________‡8________‡††††††‡8†††††††‡‡‡‡‡‡‡‡‡‡‡‡‡________ _____8†________________†8‡__‡†††††‡‡‡‡†††‡‡‡‡‡‡‡‡‡‡†‡†‡‡†8________ ___‡________________________‡8†††‡‡‡‡††‡‡†‡‡‡‡‡‡‡‡‡‡‡†‡†‡‡†_______ __†____‡_____‡_______________†8†‡‡‡††††‡‡‡‡‡‡‡‡‡†‡‡8††††‡††_______ ___†_________________________†8††††††††††‡‡‡‡‡‡‡‡‡††††††‡‡†_______ _____8_‡__‡888‡††____††‡¶‡____‡‡†††††††††††‡‡†‡‡†¶†††††‡†‡________ _______†_¶8¶†††††††‡_†††8___†8‡‡††††††††††‡†8‡‡8_¶‡‡†††††‡________ ________‡††¶¶††††††‡†8‡__†‡‡‡‡††8†††††††††‡†8______8‡‡__‡_________ ________8‡‡____††††_____88††††††††‡‡‡†††††‡†8_____________________ ______†††‡‡__‡‡†___‡‡8______________8‡‡‡888‡†_____________________ ______________________________________†¶___‡______________________""" class Sudoku: def __init__(self): a=[[''for j in range(9)]for i in range(9)] t=range(1,10) random.seed() random.shuffle(t) k=0 for i in range(9): if i==3 or i==6: k+=1 for j in range(9): a[i][j]=str(t[(j+k)%9]) k+=3 for t in range(20): i=random.choice([0,3,6]) j=random.choice(filter(lambda x: x!=i,[0,3,6])) for k in range(9): for l in range(3): a[k][i+l],a[k][j+l]=a[k][j+l],a[k][i+l] i=random.choice([0,3,6]) j=random.choice(filter(lambda x: x!=i,[0,3,6])) for k in range(9): for l in range(3): a[i+l][k],a[j+l][k]=a[j+l][k],a[i+l][k] for s in [[0,1,2],[3,4,5],[6,7,8]]: i=random.choice(s) j=random.choice(filter(lambda x: x!=i,s)) for k in range(9): a[i][k],a[j][k]=a[j][k],a[i][k] i=random.choice(s) j=random.choice(filter(lambda x: x!=i,s)) for k in range(9): a[k][i],a[k][j]=a[k][j],a[k][i] self.a=deepcopy(a) #print self.prov(a) #print "solution "+''.join([''.join(i) for i in a]) t=0 k=random.randint(30,60) while t!=k: i=random.randint(0,8) j=random.randint(0,8) if a[i][j]!='_': a[i][j]='_' t+=1 self.field=deepcopy(a) self.game=deepcopy(a) def prov(self, a): b=[0]*27 for i in range(9): for j in range(9): b[i]+=int(a[i][j]) b[9+j]+=int(a[i][j]) b[18+(i/3)*3+j/3]+=int(a[i][j]) return b.count(45)==27 def restart(self): self.game=deepcopy(self.field) def check(self,x,y,t): a=self.game gor=a[x-1] ver=[a[k][y-1] for k in range(9)] i,j=((x-1)/3)*3,((y-1)/3)*3 sq=[a[i][j],a[i][j+1],a[i][j+2],a[i+1][j],a[i+1][j+1],a[i+1][j+2],a[i+2][j],a[i+2][j+1],a[i+2][j+2]] return not(t in gor or t in ver or t in sq) def final_check(self): return ''.join([''.join(i) for i in self.game]).find('_')==-1 def full(self, m): a=[[''for j in range(9)]for i in range(9)] for i in range(9): for j in range(9): if self.field[i][j]==m[i*9+j] or self.field[i][j]=='_': a[i][j]=m[i*9+j] else: return "Error" if self.prov(a): self.game=deepcopy(a) return "Wow!!!" else: return "Error" def step(self,x,y,t): if self.field[x-1][y-1]=='_' and self.check(x,y,t): self.game[x-1][y-1]=t return "Nice step" return 'Impossible step' def write(self, client): a=self.game[:][:] s='' for i in range(9): if i%3==0: s+='-------------\n' s+='|'+''.join(a[i][0:3])+'|'+''.join(a[i][3:6])+'|'+''.join(a[i][6:9])+'|\n' s+='-------------\n' client.send(s) sock = socket.socket() sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(('', PORT)) sock.listen(10) signal.signal(signal.SIGCHLD, signal.SIG_IGN) while 1: client, addr = sock.accept() if os.fork() == 0: break client.close() sock.close() f = Sudoku() client.send( """Have a nice time with sudoku Format: a) "[1-9] [1-9] [1-9]" - coords and input digit b) "solution [1-9]{81}" - full solution Other: "restart" to start current game again "[QqXx]" to exit """) def handle(line): if len(line) < 1: return (True, None) if len(line) == 1 and line[0] in "qxQX": return (False, "Bye") global f if line=='restart': f.restart() return (True,'Start again') m = re.match("^solution [1-9]{81}$",line) if m != None: m=re.findall("[1-9]{81}",line)[0] return(True, f.full(m)) m = re.match("^[1-9] [1-9] [1-9]$",line) if m != None: m=re.findall("[1-9]",line) return (True, f.step(int(m[0]),int(m[1]),m[2])) return (True, "Error input") data = "" client.settimeout(Timeout) games=0 try: while 1: if f.final_check(): games+=1 f = Sudoku() if games==100: client.send("the flag is: "+FLAG+"\n") sys.exit(0) client.send("Solved!!! Have a fun and finally you will get a flag\n") f.write(client) data=client.recv(4096) if len(data)==0: sys.exit(0) cont, msg = handle(data.replace('\n','')) if msg!=None: client.send(msg + "\n") if cont==False: client.close() sys.exit(0) except socket.timeout: client.send("So sloooow\n"+slow+"\n") sys.exit(0) sys.exit(0)
Link http://f00l.de/blog/baltctf-2013-sudoku/
- 数独 RUBY 算法、baltctf-2013-sudoku
- 谈谈数独(Sudoku)
- 数独(sudoku)实现
- PKU2676 Sudoku 数独
- poj2676 Sudoku 数独
- 数独(sudoku)C++
- POJ2676 Sudoku [数独]
- poj2676 Sudoku 数独
- Sudoku 数独
- 数独Sudoku
- sudoku(数独)
- POJ3074 sudoku数独
- 数独游戏(sudoku)算法 回溯+剪枝
- dancing links 算法学习小记 Poj 3074 Sudoku (数独)
- 数独(SuDoku)介绍
- Sudoku Helper 数独助手
- poj 1676 sudoku 数独
- hdu1246 Sudoku Killer 数独
- 寻找带环的单链表的换的入口位置处的节点
- 驱动相关问题
- 字符串倒序
- 计词unigram和bigram的频次
- dev、devfs、 udev和sysfs之间的关系
- 数独 RUBY 算法、baltctf-2013-sudoku
- HIbernate中的乐观锁与悲观锁
- hdu 4710 Balls Rearrangement
- HDU 4708 Rotation Lock Puzzle 解题报告
- 大数据时代的技术hive:hive介绍
- Viewport.js + C_Frame.js
- 指针间的关系
- Android SDK Manager 下载问题
- 自己动手写脚本语言