zoukankan      html  css  js  c++  java
  • POJ2676 Sudoku

     
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 18193   Accepted: 8803   Special Judge

    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

    Source

     
    依旧是数独题,解法见http://www.cnblogs.com/SilverNebula/p/5857587.html [靶形数独]
      1 /*by SilverN*/
      2 #include<algorithm>
      3 #include<iostream>
      4 #include<cstring>
      5 #include<cstdio>
      6 using namespace std;
      7 int mp[15][15];
      8 int block[15],h[15],r[15];
      9 int in[15][15];
     10 int sx[100],sy[100],cnt=0;
     11 inline int blcnt(int x){
     12     int res=9;
     13     while(x){
     14         res-=(x&1);
     15         x>>=1;
     16     }
     17     return res;
     18 }
     19 void init(){
     20     for(int i=1;i<=9;i++)
     21      for(int j=1;j<=9;j++)
     22         in[i][j]=(i+2)/3*3+(j+2)/3-3;
     23 }
     24 void Print(){
     25     for(int i=1;i<=9;i++){    
     26          for(int j=1;j<=9;j++)
     27              printf("%d",mp[i][j]);
     28          printf("
    ");
     29     }
     30 }
     31 bool dfs(){
     32     int i,j;
     33     int mini=60,pos=0;
     34     int tmp,now;
     35     int ctmp;
     36     for(i=1;i<=cnt;i++){
     37         if(!mp[sx[i]][sy[i]]){
     38             tmp=h[sx[i]]|r[sy[i]]|block[in[sx[i]][sy[i]]];
     39             now=blcnt(tmp);
     40             if(!now)return 0;
     41             if(now<mini){
     42                 ctmp=tmp;//记录最小值对应的tmp
     43                 //先前由于没有记录ctmp,而在后面计算时用tmp算,出现了bug,费了一个小时查出来错 
     44                 mini=now;
     45                 pos=i;
     46             }
     47         }
     48     }
     49     if(mini==60){
     50         Print();
     51         return 1;
     52     }
     53     i=sx[pos];
     54     j=sy[pos];
     55              for(int k=1;k<=9;k++){
     56                  if(!(ctmp&(1<<k))){
     57                      mp[i][j]=k;
     58                      h[i]^=(1<<k);
     59                      r[j]^=(1<<k);
     60                      block[in[i][j]]^=(1<<k);
     61                      if(dfs())return 1;
     62                      //回溯 
     63                      block[in[i][j]]^=(1<<k);
     64                      h[i]^=(1<<k);
     65                      r[j]^=(1<<k);
     66                      mp[i][j]=0;
     67                  }
     68              }
     69     return 0;
     70 }
     71 
     72 int main(){
     73     int i,j;
     74     int T;
     75     scanf("%d",&T);
     76     init();
     77     while(T--){
     78         memset(h,0,sizeof h);
     79         memset(r,0,sizeof r);
     80         memset(block,0,sizeof block);
     81         cnt=0;
     82 
     83         char ch[20];
     84         for(i=1;i<=9;i++){
     85             scanf("%s",ch);
     86             for(j=1;j<=9;j++){
     87                 mp[i][j]=ch[j-1]-'0';
     88             }
     89         }    
     90         for(i=1;i<=9;i++)//
     91          for(j=1;j<=9;j++){//
     92             if(mp[i][j]){
     93                  r[j]^=1<<mp[i][j];
     94                  h[i]^=1<<mp[i][j];
     95                  block[in[i][j]]^=1<<mp[i][j];
     96             }
     97             else{
     98                 cnt++;
     99                 sx[cnt]=i;
    100                 sy[cnt]=j;
    101             }
    102         }
    103         dfs();
    104     }
    105     return 0;
    106 }
  • 相关阅读:
    Python之位移操作符所带来的困惑
    SR采用PubSubHubbub协议实时接收GReaderSharedItems更新
    如何找到正在热传的微博客图片?
    手持设备:懒人的互联网音乐智能同步/播放器
    七十二般变化解得了三灾?
    如何测量Google Reader用户的分享活跃度
    基于Google Reader发展起来的个性化推荐系统之三大问题
    Python检测Windows剩余磁盘空间
    Python 内部类,内部类调用外部类属性,方法
    禁止IE页面自动跳转到EDGE浏览器的方法
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/5857685.html
Copyright © 2011-2022 走看看