ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

POJ 2676 Sudoku

2020-10-19 09:32:39  阅读:261  来源: 互联网

标签:11 Sudoku cells 2676 digits POJ each empty


POJ 2676 Sudoku

POJ传送门

Description

Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty cells with decimal digits from 1 to 9, one digit per cell, in such way that in each row, in each column and in each marked 3x3 subsquare, all the digits from 1 to 9 to appear. Write a program to solve a given Sudoku-task.
img

Input

The input data will start with the number of the test cases. For each test case, 9 lines follow, corresponding to the rows of the table. On each line a string of exactly 9 decimal digits is given, corresponding to the cells in this line. If a cell is empty it is represented by 0.

Output

For each test case your program should print the solution in the same format as the input data. The empty cells have to be filled according to the rules. If solutions is not unique, then the program may print any one of them.

Sample Input

1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107

Sample Output

143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127

题解:

暴搜填数独。稍稍有一点进阶的暴搜。思路也很简单:尝试往里填数,然后暴力判断即可。

这个暴力判断可以用位运算优化一下.但是本题我并没有这么干,用了个bool数组。

需要提一嘴的是这个第几个九宫格的处理。式子已经打在代码里了,大家手画模拟一下就能看出来这个东西的正确性。

代码:

#include<cstdio>
#include<cstring>
using namespace std;
int t,cnt;
int mp[11][11];
char mapi[11][11];
bool flag;
bool row[11][11],col[11][11],grid[11][11];
void print()
{
    for(int i=1;i<=9;i++)
    {
        for(int j=1;j<=9;j++)
            printf("%d",mp[i][j]);
        puts("");
    }
}
void dfs(int x,int y)
{
    if(flag)
        return;
    if(mp[x][y])
    {
        if(x==9&&y==9)
        {
            for(int i=1;i<=9;i++)
            {
                for(int j=1;j<=9;j++)
                    printf("%d",mp[i][j]);
                puts("");
            }
            flag=1;
            return;
        }
        if(y==9)
            dfs(x+1,1);
        else
            dfs(x,y+1);
    }
    if(!mp[x][y])
    {
        int k=3*((x-1)/3)+(y-1)/3+1;
        for(int i=1;i<=9;i++)
        {
            if(!row[x][i]&&!col[y][i]&&!grid[k][i])
            {
                row[x][i]=1;
                col[y][i]=1;
                grid[k][i]=1;
                mp[x][y]=i;
                if(x==9&&y==9)
                {
                    for(int i=1;i<=9;i++)
                    {
                        for(int j=1;j<=9;j++)
                            printf("%d",mp[i][j]);
                        puts("");
                    }
                    flag=1;
                    puts("");
                    return;
                }
                if(y==9)
                    dfs(x+1,1);
                else
                    dfs(x,y+1);
                row[x][i]=0;
                col[y][i]=0;
                grid[k][i]=0;
                mp[x][y]=0;
            }
        }
    }
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        memset(col,0,sizeof(col));
        memset(row,0,sizeof(row));
        memset(grid,0,sizeof(grid));
        flag=0;
        for(int i=1;i<=9;i++)
            scanf("%s",mapi[i]+1);
        for(int i=1;i<=9;i++)
            for(int j=1;j<=9;j++)
            {
                mp[i][j]=mapi[i][j]-'0';
                if(mp[i][j])
                {
                    int k=3*((i-1)/3)+(j-1)/3+1;
                    row[i][mp[i][j]]=1;
                    col[j][mp[i][j]]=1;
                    grid[k][mp[i][j]]=1;
                }
            }
        dfs(1,1);
    }
    return 0;
}

标签:11,Sudoku,cells,2676,digits,POJ,each,empty
来源: https://www.cnblogs.com/fusiwei/p/13838478.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有