Discuz! Board

 找回密码
 立即注册
查看: 220|回复: 0

mmm

[复制链接]

7

主题

7

帖子

21万

积分

论坛元老

Rank: 8Rank: 8

积分
213525

大王燕 Lv:90
发表于 2025-1-19 16:23:09 | 显示全部楼层 |阅读模式
include<bits/stdc++.h>
using namespace std;
int cnt=0;
int P[9][9]={
8,0,0,0,0,0,0,0,0,
0,0,3,6,0,0,0,0,0,
0,7,0,0,9,0,2,0,0,
0,5,0,0,0,7,0,0,0,
0,0,0,0,4,8,7,0,0,
0,0,0,1,0,0,0,3,0,
0,0,1,0,0,0,0,6,8,
0,0,8,5,0,0,0,1,0,
0,9,0,0,0,0,4,0,0
};
bool check(int x,int y,int n)
{
        for(int i=0;i<=8;i++)
        {
                if(P[x][i]==n)
                {
                        return false;
                }
        }
        for(int i=0;i<=8;i++)
        {
                if(P[i][y]==n)
                {
                        return false;
                }
        }  
        int tx=x/3*3;
        int ty=y/3*3;
        for(int i=0;i<=2;i++)
        {
                for(int j=0;j<=2;j++)
                {
                        if(P[tx+i][ty+j]==n)
                        {
                                return false;
                        }
                }
        }
        return true;
}
void dfs(int x,int y)
{
        if(P[x][y]==0)
        {
                for(int i=1;i<=9;i++)
                {
                        if(check(x,y,i))
                        {
                                P[x][y]=i;
                                if(x==8 && y==8)
                                {
                                        for(int j=0;j<=8;j++)
                                        {
                                                for(int k=0;k<=8;k++)
                                                {
                                                        cout<<P[j][k]<<" ";
                                                }
                                        cout<<endl;
                                        }
                                        cnt++;
                                        cout<<endl;
                                }
                            else
                            {       
                                    dfs(x+(y+1)/9,(y+1)%9);
                                }
                                P[x][y]=0;
                        }       
                }       
        }       
        else
        {
                dfs(x+(y+1)/9,(y+1)%9);
        }
}
int main()
{
        dfs(0,0);       
        cout<<cnt;
}

回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|DiscuzX

GMT+8, 2025-5-29 05:16 , Processed in 0.052423 second(s), 27 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.. 技术支持 by 巅峰设计

快速回复 返回顶部 返回列表