POJ 2676 Suduku

POJ 2676 Suduku

先直接贴代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 15;

int p[N][N];
bool row[N][N];
bool col[N][N];//column
bool nine[N][N];
int temp;
int temp1;
// int k1 ;//When initial the variable k1 here, wa.Wht is that???

void init()
{
    memset(row, false, sizeof(row));
    memset(col, false, sizeof(col));
    memset(nine, false, sizeof(nine));
}

void input()
{
    for(int i = 0 ; i < 9 ; i++)
        for(int j = 0 ; j < 9 ; j++)
        {
            scanf("%1d",&p[i][j]);
            if(p[i][j])
            {
                temp = p[i][j];
                row[i][temp] = true;
                col[j][temp] = true;
                temp1 = (i / 3)*3 + j / 3 ;
                nine[temp1][temp] = true;
            }
            
        }
}

bool dfs(int x,int y)  
{  
    if(x==9)    return true;
    bool flag=false;  
    if(p[x][y])  
    {  
        if(y==8) flag=dfs(x+1,0);  
        else flag=dfs(x,y+1);  
        return flag;  
    }  
    int k1 = (x / 3)*3 + y / 3 ;
    for(int i=1;i<=9;i++)       if(!row[x][i] && !col[y][i] && !nine[k1][i])  
    {  
        p[x][y] = i;
        row[x][i] = true; 
        col[y][i] = true; 
        nine[k1][i] = true; 
          
        if(y==8) flag=dfs(x+1,0); 
        else flag=dfs(x,y+1); 
        if(flag) return true; 

        p[x][y] = 0;
        row[x][i] = false; 
        col[y][i] = false;
        nine[k1][i] = false;

    }  
    return false;  
}

int main()
{
    int T;
    cin >> T;
    while(T--)
    {
        init();

        input();

        dfs(0,0);

        for(int i = 0 ; i < 9 ; i++)
            for(int j = 0 ; j < 9 ; j++)
            {
                cout << p[i][j];
                if( j == 8 ) cout << endl;    
            }
        }
    return 0;
}

 

转载于:https://www.cnblogs.com/ronnielee/p/9495151.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值