9*9数独

尝试把csdn当一个写题的云端笔记,用来书写写题时候的思路。

数独

首先考虑思路,是一个暴搜问题,9*9方格,数据范围较小

计算时间复杂度,如果所有点都需要搜一遍每个点有9种,那么裸搜需要9^81的时间复杂度,显然超时,这个时候需要一个最容易看出来的1.顺序优化,2.正确性剪枝,3.最优性剪枝,4.冗余性剪枝。

然后需要进行一定的“常数级”优化,比如最常用的二进制优化。二进制怎么进行优化,我们需要把我们的st存成我们的二进制,然后对所有的二进制1代表可以使用,对于0代表不可以使用。get函数对于这些取一个 & 这样只有全为1才可以填入,大大减少了需要写的代码量而且一个&缩短了时间复杂程度,相当巧妙。

然后根据我们的二进制优化,怎么完成我们的顺序优化呢,我们需要使用一个ones【】数组来完成我们的找到有多少个1,然后根据这些一来规划我们的顺序优化,优先进行选择最少的dfs搜索。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=9,M=1<<N;
char str[100];
int row[N],col[N],cell[3][3];
int map[M],ones[M];
int lowbit(int x)
{
    return x&(-x);
}
void init()
{
    for(int i=0;i<N;i++) col[i]=row[i]=(1<<N)-1;
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            cell[i][j]=(1<<N)-1;
        }
    }
}
int get(int x,int y)
{
    return row[y]&col[x]&cell[x/3][y/3];
}
bool dfs(int u)
{
    if(u==0) return true;
    
    int minv=10,x,y;
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
        {
            if(str[i*9+j]=='.')
            {
                int t=ones[get(i,j)];
                if(t<minv)
                {
                    minv=t;
                    x=i;
                    y=j;
                }
            }
        }
    }
    
    for(int i=get(x,y);i;i-=lowbit(i))
    {
        int t=map[lowbit(i)];
        
        //修改状态
        row[y]-=1<<t;
        col[x]-=1<<t;
        cell[x/3][y/3]-=1<<t;
        str[x*9+y]='1'+t;
        
        if(dfs(u-1)) return true; //最优性剪枝
        
        //恢复现场
        row[y]+=1<<t;
        col[x]+=1<<t;
        cell[x/3][y/3]+=1<<t;
        str[x*9+y]='.';
    }
    return false;
}
int main()
{
    for(int i=0;i<N;i++) map[1<<i]=i;
    for(int i=0;i<1<<N;i++)
    {
        int s=0;
        for(int j=i;j;j-=lowbit(j)) s++;
        ones[i]=s;
    }
    
    
    while(cin >> str,str[0]!='e')
    {
        init();
        int cnt=0;
        for(int i=0,k=0;i<N;i++)
        {
            for(int j=0;j<N;j++,k++)
            {
                int t=str[k]-'1';
                if(str[k]!='.')
                {
                    row[j]-=1<<t;
                    col[i]-=1<<t;
                    cell[i/3][j/3]-=1<<t;
                }
                else cnt++;
            }
        }
        dfs(cnt);
        cout << str << endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Python 的第三方库 `pygame` 来画出 9*9 数独表。以下是一个示例代码: ```python import pygame # 定义颜色 BLACK = (0, 0, 0) WHITE = (255, 255, 255) GRAY = (128, 128, 128) # 初始化 Pygame pygame.init() # 设置窗口大小和标题 size = (540, 600) screen = pygame.display.set_mode(size) pygame.display.set_caption("数独表") # 设置字体 font = pygame.font.SysFont('fangsong', 40) # 定义要填入的数独表 sudoku = [ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0] ] # 定义数独表的单元格位置和大小 cell_width = 60 cell_height = 60 cell_margin = 5 # 定义数独表的位置 x_offset = 30 y_offset = 60 # 游戏循环 done = False while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True # 清屏 screen.fill(WHITE) # 画数独表格子 for row in range(9): for col in range(9): # 计算单元格位置 x = x_offset + col * (cell_width + cell_margin) y = y_offset + row * (cell_height + cell_margin) # 画单元格 pygame.draw.rect(screen, GRAY, [x, y, cell_width, cell_height]) # 如果单元格有值,就在单元格中央显示该值 value = str(sudoku[row][col]) if value != "0": text = font.render(value, True, BLACK) text_rect = text.get_rect(center=(x + cell_width / 2, y + cell_height / 2)) screen.blit(text, text_rect) # 更新屏幕 pygame.display.flip() # 退出 Pygame pygame.quit() ``` 运行上述代码,即可显示一个空的 9*9 数独表。你可以在代码中修改 `sudoku` 变量的值来显示不同的数独题目。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值