poj 2676 Sudoku

Posted njczy2010

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 2676 Sudoku相关的知识,希望对你有一定的参考价值。

传送门

Sudoku
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 16894   Accepted: 8229   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

 
 
思路:

DFS不为0的点1-9枚举,为0的跳过

本来没什么好说的,一个教训:循环的判别条件不要出现任何计算

 

15234481 njczy2010 2676 Accepted 700K 1547MS G++ 2083B 2016-03-06 09:55:21

 

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <iostream>
  4 #include <algorithm>
  5 #include <stack>
  6 #include <cctype>
  7 #include <vector>
  8 #include <cmath>
  9 #include <map>
 10 #include <queue>
 11 
 12 #define ll long long
 13 
 14 using namespace std;
 15 
 16 int T;
 17 int ans[100][100];
 18 char s[100][100];
 19 int flag;
 20 
 21 struct PP
 22 {
 23     int x;
 24     int y;
 25 };
 26 
 27 int ok(int x,int y,int v)
 28 {
 29     int i,j;
 30     i = x;
 31     for(j = 1;j <= 9;j++){
 32         if(ans[i][j] == v) return 0;
 33     }
 34     j = y;
 35     for(i = 1;i <= 9;i++){
 36         if(ans[i][j] == v) return 0;
 37     }
 38     int rr = (x-1)/3*3;
 39     int cc = (y-1)/3*3;
 40     for(i = rr + 1 ;i <= rr + 3 ;i++  ){
 41         for(j =  cc +1 ;j <= cc + 3 ;j++  ){
 42             if(ans[i][j] == v) return 0;
 43         }
 44     }
 45     return 1;
 46 }
 47 
 48 void dfs(int x,int y)
 49 {
 50     int i;
 51     if(flag == 1){
 52         return;
 53     }
 54     if(x == 10){
 55         flag = 1;
 56         return;
 57     }
 58     int ff;
 59     if(ans[x][y] == 0){
 60         ff = 0;
 61         for(i = 1;i <= 9 ;i++){
 62             if(ok(x,y,i) == 1){
 63                 ans[x][y] = i;
 64                 ff = 1;
 65                 if(y == 9){
 66                     dfs(x+1,1);
 67                 }
 68                 else{
 69                     dfs(x,y+1);
 70                 }
 71                 if(flag == 1) return;
 72                 ans[x][y] = 0;
 73             }
 74         }
 75         if(ff == 0) return;
 76     }
 77     else{
 78         if(y == 9){
 79             dfs(x+1,1);
 80             if(flag == 1) return;
 81         }
 82         else{
 83             dfs(x,y+1);
 84             if(flag == 1) return;
 85         }
 86     }
 87 }
 88 
 89 int main()
 90 {
 91     //freopen("in.txt","r",stdin);
 92     scanf("%d",&T);
 93     for(int ccnt=1;ccnt<=T;ccnt++){
 94     //while(scanf("%d%s%s",&n,a,b)!=EOF){
 95         flag = 0;
 96         int i,j;
 97         for(i = 1;i <= 9;i++){
 98             scanf("%s",s[i] + 1);
 99         }
100         for(i = 1;i <= 9;i++){
101             for(j = 1;j <= 9;j++){
102                 ans[i][j] = s[i][j] - 0;
103             }
104         }
105         dfs(1,1);
106         for(i = 1;i <= 9;i++){
107             for(j = 1;j <= 9;j++){
108                 printf("%d",ans[i][j]);
109             }
110             printf("\n");
111         }
112     }
113     return 0;
114 }

 

以上是关于poj 2676 Sudoku的主要内容,如果未能解决你的问题,请参考以下文章

POJ2676 Sudoku - DFS

poj2676 Sudoku(DFS)

POJ2676 Sudoku

poj2676 Sudoku

poj-2676 Sudoku

POJ2676 Sudoku