zoukankan      html  css  js  c++  java
  • hdu Sudoku Killer

    Sudoku Killer

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 460    Accepted Submission(s): 169
     
    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
    ACM暑期集训队练习赛(三)
     
    Recommend
    LL

    分析:dfs,每个数都会影响该行、该列、该小九宫格,一直深搜知道填满。将未填空格按候选数个数sort后反而超时,直接dfsAC。

    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    char map[10][10];
    int mm[10][10];
    
    typedef struct S {
        int x, y;
    } GOOD;
    GOOD good[100];
    char s[25];
    int row[10][10], col[10][10], blo[10][10];
    
    int cnt;
    
    int dfs(int now) {
        if (now == cnt)
            return 1;
        int block, xx, yy, t;
        xx = good[now].x;
        yy = good[now].y;
        block = (xx / 3)*3 + yy / 3;
        for (t = 1; t <= 9; ++t) {
            if (row[xx][t] == 0 && col[yy][t] == 0 && blo[block][t] == 0) {
                mm[xx][yy] = t;
                row[xx][t] = 1;
                col[yy][t] = 1;
                blo[block][t] = 1;
                if (dfs(now + 1))
                    return 1;
                row[xx][t] = 0;
                col[yy][t] = 0;
                blo[block][t] = 0;
            }
        }
        return 0;
    }
    
    int main() {
        int i, j;
        int first = 1;
        while (scanf("%s", s) != EOF) {
            if (first)
                first = 0;
            else
                printf("\n");
            memset(row, 0, sizeof (row));
            memset(col, 0, sizeof (col));
            memset(blo, 0, sizeof (blo));
            cnt = 0;
            i = 0;
            j = 0;
            if (s[0] == '?') {
                good[cnt].x = i;
                good[cnt].y = j;
                ++cnt;
            } else {
                mm[i][j] = s[0] - '0';
                row[i][mm[i][j]] = 1;
                col[j][mm[i][j]] = 1;
                blo[(i / 3)*3 + j / 3][mm[i][j]] = 1;
            }
            for (i = 0; i < 9; ++i) {
                for (j = 0; j < 9; ++j) {
                    if (i == 0 && j == 0)
                        continue;
                    scanf("%s", s);
                    if (s[0] == '?') {
                        good[cnt].x = i;
                        good[cnt].y = j;
                        ++cnt;
                    } else {
                        mm[i][j] = s[0] - '0';
                        row[i][mm[i][j]] = 1;
                        col[j][mm[i][j]] = 1;
                        blo[(i / 3)*3 + j / 3][mm[i][j]] = 1;
                    }
                }
            }
    
            dfs(0);
    
            for (i = 0; i < 9; ++i) {
                for (j = 0; j < 9; ++j) {
                    printf("%d", mm[i][j]);
                    if (j == 8)
                        printf("\n");
                    else
                        printf(" ");
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    PIL 和 pythonopencv 从内存字节码中读取图片并转为np.array格式
    【转载】 什么是元类
    【转载】 Py之cupy:cupy的简介、安装、使用方法之详细攻略
    【转载】 vscode如何在最新版本中配置c/c++语言环境中的launch.json和tasks.json?
    【转载】 Ubuntu下使用VSCode的launch.json及tasks.json编写
    Javascript高级程序设计第二版第六章面向对象程序设计(ObjectOriented Programming)简称OOP编程笔记
    Javascript高级程序设计第二版第五章引用类型笔记
    css权重简单之谈
    编辑神器VIM下安装zencoding
    显示层3s后隐藏
  • 原文地址:https://www.cnblogs.com/baidongtan/p/2666256.html
Copyright © 2011-2022 走看看