码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2676

时间:2014-11-16 10:30:39      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:des   blog   http   io   ar   os   sp   for   strong   

Sudoku
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14105   Accepted: 6964   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. 
bubuko.com,布布扣

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
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
 
using namespace std;

const int n = 9;
int board[9][9];
char ch[10];

bool ok(int x, int y)
{
       
        for (int i = x / 3 * 3; i < x / 3 * 3 + 3; ++i)
        {
                for (int j = y / 3 * 3; j < y / 3 * 3 + 3; ++j)
                {
                        if (x == i && y == j)
                                continue;
                        if (board[x][y] == board[i][j])        // the number has been used
                                return false;
                }
        }
        int temp = board[x][y];
   
        for (int j = 0; j < n; ++j)
        {
                if (j == y)
                        continue;
                if (board[x][j] == temp)
                        return false;
        }
        
        for (int i = 0; i < n; ++i)
        {
                if (i == x)
                        continue;
                if (board[i][y] == temp)
                        return false;
        }
        return true;        
}

int dfs(int location)
{
        if (location == -1)
                return 1;
        
        if (board[location / n][location % n] != 0)        
                return dfs(location - 1);
        else
        {
                for (int i = 1; i <= n; ++i)        
                {
                        board[location / n][location % n] = i;
                        if (ok(location / n, location % n))
                        {
                                if (dfs(location - 1))
                                        return 1;
                        }
                        board[location / n][location % n] = 0;
                }
        }
        return 0;
}

int main()
{
        int nCases;
        scanf("%d", &nCases);
        while (nCases--)
        {
                for (int i = 0; i < n; ++i)
                {
                        scanf("%s", ch);
                        for (int j = 0; j < n; ++j)
                        {
                                board[i][j] = ch[j] - ‘0‘;
                        }
                }

                dfs(80);
                
                for (int i = 0; i < n; ++i)
                {
                        for (int j = 0; j < n; ++j)
                        {
                                printf("%d", board[i][j]);
                        }
                        printf("\n");
                }
        }
        
        return 0;
}

  

poj 2676

标签:des   blog   http   io   ar   os   sp   for   strong   

原文地址:http://www.cnblogs.com/a972290869/p/4101043.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!