楼主: 765220349
527 1

[统计软件] Python 题 [推广有奖]

  • 0关注
  • 0粉丝

大专生

28%

还不是VIP/贵宾

-

威望
0
论坛币
503 个
通用积分
0.0600
学术水平
3 点
热心指数
7 点
信用等级
0 点
经验
585 点
帖子
7
精华
0
在线时间
89 小时
注册时间
2015-10-5
最后登录
2021-11-12

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
我在做leetcode 37 时, 不知道自己的码错在哪儿了,已经卡了3天了,求大神指点。万分感谢!!!网上的正确答案我也懂了,但是就是找不到自己哪里出错了,谢谢大家!原题是:

Write a program to solve a Sudoku puzzle by filling the empty cells.

A sudoku solution must satisfy all of the following rules:

  • Each of the digits 1-9 must occur exactly once in each row.
  • Each of the digits 1-9 must occur exactly once in each column.
  • Each of the the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.

Empty cells are indicated by the character '.'.


A sudoku puzzle...


...and its solution numbers marked in red.

Note:

  • The given board contain only digits 1-9 and the character '.'.
  • You may assume that the given Sudoku puzzle will have a single unique solution.
  • The given board size is always 9x9.

我的代码如下:
def checks(board):
    seen = []
    for i, row in enumerate(board):
        for j, c in enumerate(row):
            if c != '.':
                seen += [(c,j),(i,c),(i/3,j/3,c)]
    return len(seen) == len(set(seen))
board=[["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]]
cboard=[[board[j] for i in range(9)] for j in range(9)]
f=[]
for i in range(9):
    for j in range(9):
        if cboard[j]=='.':
            f+=[[i,j]]


for m in f:
    h=[]
    i,j=m

    cboard[j]='123456789'
    for b in range((i//3)*3,(i//3+1)*3):
        for v in range((j//3)*3,(j//3+1)*3):
            if [b,v] not in f:
                h.append(board[v])
    for b in range(9):
        if [b,j] not in f:
            h.append(board[j])
    for v in range(9):
        if [i,v] not in f:
            h.append(board[v])
    for x in list(set(h)):
        cboard[j]=cboard[j].replace(x,'')
f=[]
for i in range(9):
    for j in range(9):
        if len(cboard[j])>1:
            f+=[[i,j]]        
        l=[]

def final(f):
    if len(f)==0:
        l.append(1)
        return
    if len(f)>0:
        i,j=f[0]
        for x in cboard[j]:
            board[j]=x
            if checks(board):
                print(board)
                print('')
                final(f[1:])

                if len(l) !=0:
                    break
        if not checks(board):
            board[j]='.'
            return                             
    if len(l) !=0:
        return                    

final(f)


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:python Character following Solution indicate python运算 Python处理 python函数 python源码剖析 python语言

沙发
escaflowne1985 在职认证  发表于 2019-7-19 14:05:46 |只看作者 |坛友微信交流群
数独啊 看着就头疼哈哈 努力吧

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加JingGuanBbs
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-19 18:59