• HDU 1426 Sudoku Killer(BFS)


    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1426

    Sudoku Killer

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 2921    Accepted Submission(s): 929


    Problem Description
    自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视。
    据说,在2008北京奥运会上,会将数独列为一个单独的项目进行比赛,冠军将有可能获得的一份巨大的奖品———HDU免费七日游外加lcy亲笔签名以及同hdu acm team合影留念的机会。
    所以全球人民前仆后继,为了奖品日夜训练茶饭不思。当然也包括初学者linle,不过他太笨了又没有多少耐性,只能做做最最基本的数独题,不过他还是想得到那些奖品,你能帮帮他吗?你只要把答案告诉他就可以,不用教他是怎么做的。

    数 独游戏的规则是这样的:在一个9x9的方格中,你需要把数字1-9填写到空格当中,并且使方格的每一行和每一列中都包含1-9这九个数字。同时还要保证, 空格中用粗线划分成9个3x3的方格也同时包含1-9这九个数字。比如有这样一个题,大家可以仔细观察一下,在这里面每行、每列,以及每个3x3的方格都 包含1-9这九个数字。

    例题:


    答案:
     
    Input
    本题包含多组测试,每组之间由一个空行隔开。每组测试会给你一个 9*9 的矩阵,同一行相邻的两个元素用一个空格分开。其中1-9代表该位置的已经填好的数,问号(?)表示需要你填的数。
     
    Output
    对于每组测试,请输出它的解,同一行相邻的两个数用一个空格分开。两组解之间要一个空行。
    对于每组测试数据保证它有且只有一个解。
     
    Sample Input
    7 1 2 ? 6 ? 3 5 8 ? 6 5 2 ? 7 1 ? 4 ? ? 8 5 1 3 6 7 2 9 2 4 ? 5 6 ? 3 7 5 ? 6 ? ? ? 2 4 1 1 ? 3 7 2 ? 9 ? 5 ? ? 1 9 7 5 4 8 6 6 ? 7 8 3 ? 5 1 9 8 5 9 ? 4 ? ? 2 3
     
    Sample Output
    7 1 2 4 6 9 3 5 8 3 6 5 2 8 7 1 9 4 4 9 8 5 1 3 6 7 2 9 2 4 1 5 6 8 3 7 5 7 6 3 9 8 2 4 1 1 8 3 7 2 4 9 6 5 2 3 1 9 7 5 4 8 6 6 4 7 8 3 2 5 1 9 8 5 9 6 4 1 7 2 3
     
    Author
    linle
     
    Source
     
    Recommend
    LL
     
     
     没看清题,没注意到“每行、每列,以及每个3x3的方格都包含1-9这九个数字”这个条件,导致多次WA。。。然后又在dfs函数回溯法部分出了问题,目测是因为回溯时变量值发生改变造成的,修改了下,终于过了。。。
     
    贴下代码:
     
     By LFENG
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    using namespace std;
    int n, m, ok;
    int g[12][12], vis[90];
    struct node
    {
        
    int x;
        
    int y;
    } po[
    90];
    void getdata()
    {
        
    int i, j;
        
    char temp[5];
        
    for(i = 0; i < 9; i++)
            
    for(j = 0; j < 9; j++)
            {
                
    if(!i && !j)continue;
                scanf(
    "%s", temp);
                
    if(temp[0] == '?')
                {
                    po[m].x = i;
                    po[m].y = j;
                    m++;
                }
                
    else if(temp[0] >= '1' && temp[0] <= '9')
                    g[i][j] = temp[
    0] - 48;
            }
    }
    void show()
    {
        
    int i, j;
        
    for(i = 0; i < 9; i++)
        {
            printf(
    "%d", g[i][0]);
            
    for(j = 1; j < 9; j++)
                printf(
    " %d", g[i][j]);
            printf(
    "\n");
        }
    }
    int check(int x, int y, int i)
    {
        
    int j, k;
        
    for(j = 0; j < 9; j++)
            
    if(g[x][j] == i || g[j][y] == i)return 0;
        
    for(j = x / 3 * 3; j < x / 3 * 3 + 3; j++)
            
    for(k = y / 3 * 3; k < y / 3 * 3 + 3; k++)
            {
                
    if(g[j][k] == i)
                    
    return 0;
            }
        
    return 1;
    }
    void dfs(int k)
    {
        
    int i;
        
    if(k == m)
        {
            ok = 
    1;
            show();
            
    return ;
        }
        
    for(i = 1; !ok && i <= 9; i++)
        {
            
    if(!check(po[k].x, po[k].y, i))continue;
            g[po[k].x][po[k].y] = i;
            dfs(k + 
    1);
            
    if(ok)return ;
            g[po[k].x][po[k].y] = 
    0;
        }
    }
    int main()
    {
        
    char temp[5];
        
    int cas = 0;
        
    while(scanf("%s", temp) != EOF)
        {
            m = 
    0;
            
    if(cas++)printf("\n");
            memset(g, 
    0sizeof(g));
            
    if(temp[0] == '?')
            {
                po[m].x = 
    0;
                po[m].y = 
    0;
                m++;
            }
            
    else if(temp[0] >= '1' && temp[0] <= '9')
                g[
    0][0] = temp[0] - 48;
            getdata();
            ok = 
    0;
            dfs(
    0);
        }
        
    return 0;
    }
  • 相关阅读:
    tomcat页面跳转问题
    linux shell脚本攻略总结
    nginx中配置tomcat
    centos中文输入法支持
    esxi创建centos系统
    linux日常总结
    你不知道的编码软件排行榜
    Beyond Compare切换比较会话过滤模式的方法
    用Beyond Compare找代码bug的方法
    文件对比工具比较表格时怎么显示行号
  • 原文地址:https://www.cnblogs.com/lfeng/p/3072278.html
Copyright © 2020-2023  润新知