zoukankan      html  css  js  c++  java
  • Sudoku 数独游戏 果断dfs(),就是每个3*3的方格判断时要算一下,左上角方格的位置

    Problem 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. 
     
    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
    ***************************************************************************************************************************
    3*3方格的计算方法左上角{(i/3)*3,(j/3)*3}
    ***************************************************************************************************************************
     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<cstdio>
     6 using namespace std;
     7 int num,su[1001][2],map[11][11],cas;
     8 int i,j,k;
     9 bool judge(int x,int y,int k)
    10  {
    11     int it,jt;
    12     //同行同列时的判断
    13     for(it=0;it<9;it++)
    14     {
    15         if(map[it][y]==k)return false;
    16         if(map[x][it]==k)return false;
    17     }
    18     it=(x/3)*3;//小方格左上角的数
    19     jt=(y/3)*3;
    20     for(int st=0;st<3;st++)
    21      for(int yt=0;yt<3;yt++)
    22        if(map[it+st][yt+jt]==k)
    23         return false;
    24     return true;
    25  }
    26 int dfs(int cap)
    27 {
    28     int ie;
    29     if(cap<0) return 1;//满足的真实条件
    30     for(ie=1;ie<=9;ie++)
    31     {
    32         int xs=su[cap][0];
    33         int ys=su[cap][1];
    34         if(judge(xs,ys,ie))
    35         {
    36             map[xs][ys]=ie;
    37             if(dfs(cap-1))return 1;//搜到底,满足条件返回
    38             map[xs][ys]=0;//还原
    39         }
    40     }
    41     return 0;
    42 }
    43 int main()
    44 {
    45     scanf("%d",&cas);
    46     getchar();
    47     while(cas--)
    48     {
    49         char ch;
    50         num=0;
    51         for(i=0;i<9;i++,getchar())
    52         {
    53             for(j=0;j<9;j++)
    54             {
    55                 scanf("%c",&ch);
    56                 map[i][j]=ch-'0';
    57                 if(map[i][j]==0)//记录要填入数的的位置
    58                 {
    59                     su[num][0]=i;
    60                     su[num++][1]=j;
    61                 }
    62             }
    63         }
    64         dfs(num-1);
    65         //输出结果
    66         for(i=0;i<9;i++)
    67         {
    68          for(j=0;j<9;j++)
    69            printf("%d",map[i][j]);
    70          printf("
    ");
    71         }
    72         printf("
    ");
    73 
    74     }
    75     return 0;
    76 
    77 }
    View Code
     
  • 相关阅读:
    取最小未用的账号
    Oracle 同义词
    RMAN备份报 RMAN-06059 错误
    Oracle11g memory_target
    基础记录
    uboot启动流程
    android有关生命周期探讨
    can协议
    java虚拟机
    内存管理机制
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3372893.html
Copyright © 2011-2022 走看看