Chinaunix首页 | 论坛 | 博客
  • 博客访问: 452852
  • 博文数量: 98
  • 博客积分: 6011
  • 博客等级: 准将
  • 技术积分: 1030
  • 用 户 组: 普通用户
  • 注册时间: 2006-11-23 13:19
文章分类

全部博文(98)

文章存档

2011年(2)

2009年(2)

2008年(31)

2007年(35)

2006年(28)

我的朋友

分类:

2007-10-26 13:05:10

Sudoku
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 2060 Accepted: 849 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
题目的意思是说,用1-9的数字去填充上面的9*9表格,要求每一行的九个格子中1-9各出现一次,每一列的九个格子中1-9各出现一次,还有如上图用粗线隔开的3*3的小块的九个格子中1-9个数字也各出现一次,有可能不止一种解,只要输出一种合适的解即可..可用递归搜索枚举所有的解,在搜索过程中可以剪掉一些不符合要求的解,可节省大量的时间,本题耗时700多MS.
代码如下:
#include
using namespace std;
int x[82],f;int row[82],col[82],use[10][10],use2[10][10],z[82],use3[10][10],block[82];
void fun(int p)
{
 int i,r,c,b;
 for(x[z[p]]=1;x[z[p]]<=9&&f==0;x[z[p]]++)
 { r=row[z[p]];c=col[z[p]];b=block[z[p]];
  if(use[r][x[z[p]]]) continue;
   if(use2[c][x[z[p]]]) continue;
   if(use3[b][x[z[p]]]) continue;
   use[r][x[z[p]]]=1;
   use2[c][x[z[p]]]=1;
   use3[b][x[z[p]]]=1;
     if(p  else
  {
   f=1;
   for(i=1;i<=81;i++)
   {  cout<  }
  use[r][x[z[p]]]=0;
   use2[c][x[z[p]]]=0;
   use3[b][x[z[p]]]=0;
 }
}
int main()
{
 int t,i,j,k,s;
 char a[10];
 row[1]=1;col[1]=1;block[1]=1;
 for(i=2;i<=81;i++)
 {
  if(col[i-1]<9) {col[i]=col[i-1]+1;row[i]=row[i-1];}
  else
  { col[i]=1;row[i]=row[i-1]+1;}
  if(row[i]<=3)
  {
   if(col[i]<=3) block[i]=1;else if(col[i]<=6) block[i]=2;
   else block[i]=3;
  }
  else if(row[i]<=6)
  {
   if(col[i]<=3) block[i]=4;else if(col[i]<=6) block[i]=5;
   else block[i]=6;
  }
  else
  {
   if(col[i]<=3) block[i]=7;else if(col[i]<=6) block[i]=8;
   else block[i]=9;
  }
 }
 cin>>t;
 while(t--)
 {
  k=0;s=0;
  memset(use,0,sizeof(use));
  memset(use2,0,sizeof(use2));
  memset(use3,0,sizeof(use3));
  for(i=1;i<=9;i++)
  {
   cin>>a;
   for(j=0;j<9;j++)
   { 
    x[++k]=a[j]-'0';
    if(x[k]==0)
    {s++;z[s]=k;}
    else
    {
     use[i][x[k]]=1;
     use2[j+1][x[k]]=1;
     use3[block[(i-1)*9+(j+1)]][x[k]]=1;
    }
   }
  }
  z[0]=s;
  f=0;
  fun(1);
 }
 return 0;
}
/*
Sample Input

1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107
Sample Output

143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127
*/
 
阅读(2284) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~