大头壳

大头大头 下雨不愁 人家有伞 我有大头
posts - 1, comments - 6, trackbacks - 0, articles - 22
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

2676@poj

Posted on 2008-03-28 10:27 王大头 阅读(141) 评论(0)  编辑 收藏 引用
Time Limit: 2000MS  Memory Limit: 65536K
Total Submissions: 2074  Accepted: 858  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.

2676_1 

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
Southeastern Europe 2005


数独游戏,对啥算法没啥概念,似乎是DFS吧,反正

就是递归加上剪枝就得了。

提交了几次TLE,又有几次WA,TLE好像是因为从开

头往结尾这个方向算会比较耗时,改成了从后往前算

就好了;WA是因为居然漏掉了一个等号……



#include <stdio.h> 

char t[
9][9][2]; 

int allow(int r
, int c, int n)
{
        int i
, j, k; 

        
for(i = 0; i < 9; i++)
                
if(t[i][c][0== n)
                        
return 0

        
for(j = 0; j < 9; j++)
                
if(t[r][j][0== n)
                        
return 0

        
for(i = r / 3 * 3; i < r / 3 * 3 + 3; i++)
                
for(j = c / 3 * 3; j < c / 3 * 3 + 3; j++)
                        
if(t[i][j][0== n)
                                
return 0

        
return 1;


int guess(int r
, int c)
{
        int i
, j, k, n, f; 

        k 
= r * 9 + c;
        i 
= (k - 1/ 9;
        j 
= (k - 1% 9

        
if(k == 0) {
                
if(t[r][c][1== 1)
                        
return 0

                
for(n = 9; n > 0; n--)
                        
if(allow(r, c, n)) {
                                t[r][c][
0= n;
                                
return 0;
                        } 

                
return -1;
        } 

        
if(t[r][c][1== 1) {
                
if(guess(i, j) == -1)
                        
return -1;
                
else
                        
return 0

        } 

        
for(n = 9, f = 0; n > 0; n--)
                
if(allow(r, c, n))
                        f 
= 1

        
if(f == 0)
                
return -1

        
for(n = 9, f = 0; n > 0; n--)
                
if(allow(r, c, n)) {
                        t[r][c][
0= n; 

                        
if(guess(i, j) == -1) {
                                t[r][c][
0= 0;
                                
continue;
                        }
                        
else
                                f 
= 1;
                } 

        
if(f == 0)
                
return -1;


int main()
{
        int n
, r, c; 

        scanf(
"%d", &n); 

        getchar(); 

        
while(n--) {
                
for(r = 0; r < 9; r++)
                        
for(c = 0; c < 9; t[r][c][0= t[r][c][1= 0, c++); 

                
for(r = 0; r < 9; r++)
                        
for(c = 0; c < 10; c++)
                                
if(c < 9) {
                                        t[r][c][
0= getchar() - 48;
                                        
if(t[r][c][0])
                                                t[r][c][
1= 1;
                                }
                                
else
                                        getchar(); 

                guess(
8, 8); 

                
for(r = 0; r < 9printf("\n"), r++)
                        
for(c = 0; c < 9printf("%d", t[r][c][0]), c++);
        }