• 数独


     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 
     5 using namespace std;
     6 
     7 char g[90];
     8 
     9 bool isPlace(int n)
    10 {
    11     for (int i = 0; i < 9; i++) {
    12         int nn = n / 9 * 9 + i;
    13         if (g[nn] == g[n] && nn != n) return false;
    14         nn = n % 9 + i * 9;
    15         if (g[nn] == g[n] && nn != n) return false;
    16         nn = (n / 27 * 3 + i / 3) * 9 + n % 9 / 3 * 3 + i % 3;
    17         if (g[nn] == g[n] && nn != n) return false;
    18     }
    19     return true;
    20 }
    21 
    22 void dfs(int n)
    23 {
    24     if (n == 81) {
    25         puts(g);
    26         return;
    27     }
    28     if (g[n] == '.') {
    29         for (int i = 1; i <= 9; i++) {
    30             g[n] = i + '0';
    31             if (isPlace(n)) {
    32                 dfs(n + 1);
    33             }
    34         }
    35         g[n] = '.';
    36     }
    37     else
    38         dfs(n + 1);
    39 }
    40 
    41 int main()
    42 {
    43     while (scanf("%s", g) == 1) {
    44         if (strcmp(g, "end") == 0) break;
    45         dfs(0);
    46     }
    47     return 0;
    48 }

    输入:
    .2738..1..1...6735.......293.5692.8...........6.1745.364.......9518...7..8..6534. ......52..8.4......3...9...5.1...6..2..7........3.....6...1..........7.4.......3. end

    输出:
    527389416819426735436751829375692184194538267268174593643217958951843672782965341
    416837529982465371735129468571298643293746185864351297647913852359682714128574936



    Time Limit:1000MS。虽然超时了,但是我觉得能解出来就已经非常棒了(因为代码非常漂亮)!超时原因在于回溯的时候出栈的用时,我会改进的。

  • 相关阅读:
    Oracle FGA审计记录的清理步骤
    UVa11488-Hyper Prefix Sets(trie树)
    配置Log4j(非常具体)
    poj1190生日蛋糕
    zju1610Count the Colors
    【例9.3】求最长不下降序列
    P1364 医院设置
    P1629 邮递员送信
    P1476 休息中的小呆
    P1330 封锁阳光大学
  • 原文地址:https://www.cnblogs.com/jacen789/p/5043979.html
Copyright © 2020-2023  润新知