Sudoku

Sudoku

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 527    Accepted Submission(s): 337


Problem Description
A Sudoku puzzle, once solved, is a 9x9 grid of digits organized as a 3x3 grid of smaller 3x3 units. Each of the nine rows must contain every positive digits exactly once, as do each column and also each 3x3 unit. The puzzle is to start from a partially filled 9x9 grid and to fill in the remaining cells using only logic. The puzzle maker usually makes sure that the solution will be unique and that it can be reached using deduction only, without guessing.

This number placing game is gaining popularity in the west, and every second newspaper publishes weekly instances of the puzzle. Somewhere at the head of one such newspaper, someone decided that buying individual instances from a puzzle maker would be too expansive, and instead decided to steal puzzles from other newspapers and also to print randomly generated Sudoku-like grids.

One week later, his assistant gets stuck with the job of printing the solution to the Sudoku puzzles his boss previously published. Unfortunately, his boss doesn’t have those solutions, the randomly generated problems don’t have any solution, and he doesn’t even remember which is which. In despair, the assistant calls for your help.
 

Input
The first line of the input will contain the number of test cases. Each test case will consist of a 9 by 9 grid of characters, where each character will either be ‘?’ or a digit between 1 and 9 inclusively.
 

Output
For each test case, you must print back the grid to the standard input, replacing each question mark with an appropriate digit to solve the Sudoku. If a test case does not allow any solution, output ”impossible” instead of a completed grid. If a test case do allow a solution, you can assume that the solution will be unique, and that theoretically it could be reached without guessing.

Test cases are separated by “---” both in the input and in the output.
 

Sample Input
  
  
3 ??4??9??8 ?3??5??1? 7??4??2?? 3??8??1?? ?5?????9? ??6??1??2 ??8??3??1 ?2??4??5? 6??1??7?? --- ??4??9??8 ?3??5??1? 7??4??2?? 3??8??1?? ?5?????9? ??6??1??2 ??8??3??1 62??4??5? 6??1??7?? --- 3?1????76 7??9????? 2?5?3???? ?????64?1 ???2?1??? 1?25????? ????8?9?3 ?????9??4 51????7?8
 

Sample Output
  
  
264319578 839257416 715486239 372894165 451632897 986571342 548763921 127948653 693125784 --- impossible --- 391452876 764918532 285637149 953876421 678241395 142593687 426785913 837169254 519324768
 

还是数独,和以前一题差不多,也是数独,链接:http://blog.csdn.net/l2533636371/article/details/70228529

/*

分类:搜索————数独
来源:SUdoku Killer
思路:点在9*9大方格中的坐标与3*3小方格坐标的关系:
(X,Y)表示在9*9方格坐标,(x,y)表示在该点所在在3*3方格第一个方格的坐标;
x=X/3*3
y=Y/3*3 


*/


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std;

const int MAX=10;
char map_[MAX][MAX];
int pos[MAX*9][2];//存储是问号点的坐标,方便搜索时只需要搜索这些点填什么数字.
bool row[MAX][MAX],list[MAX][MAX];//用来标明哪行哪列出现过什么数.
int k;

bool check(int v,int num){
    int n=pos[v][0]/3*3;//搜索周围9小格的行的开始搜索位置.
    int m=pos[v][1]/3*3;//搜索周围9小格的列的开始搜索位置.
    for(int i=n;i<n+3;++i){
        for(int j=m;j<m+3;++j){
            if(map_[i][j] == num+'0')return false;
        }
    }
    return true;
}

bool DFS(int v){
    if(v == k){return true;}//代表前面已经把所有的点都搜索完了.
    for(int i=1;i<10;++i){
        if(!row[pos[v][0]][i] && !list[pos[v][1]][i] && check(v,i)){
            map_[pos[v][0]][pos[v][1]]=i+'0';
            row[pos[v][0]][i]=true;
            list[pos[v][1]][i]=true;
            if(DFS(v+1)){return true;}
            map_[pos[v][0]][pos[v][1]]='?';
            row[pos[v][0]][i]=false;
            list[pos[v][1]][i]=false;
        }
    }
    return false;
}

void output(){
    for(int i=0;i<9;++i){
        cout<<map_[i][0];
        for(int j=1;j<9;++j){
            cout<<map_[i][j];
        }
        cout<<endl;
    }
   
    return;
}

int main(){
 int num=0;
 int t;
 	scanf("%d",&t);
    while(t--){
     k=0;
     memset(row,false,sizeof row);
     memset(list,false,sizeof list);
     char ch[3];
	 for(int i=0;i<9;++i){
	 	scanf("%s",map_[i]);
          for(int j=0;j<9;++j){
              
			 if(map_[i][j]=='_')
			 break;
              if(map_[i][j] == '?')
			  {
			  pos[k][0]=i;
			  pos[k++][1]=j;
			  continue;
			  }
              
			  row[i][map_[i][j]-'0']=true;
              list[j][map_[i][j]-'0']=true;
            }
        }
        if(t>0)
    cin>>ch;
    
	 int flag=0;
	 if(DFS(0)==true)
	  flag=1;//开始搜索第1个是问号的点.
     
	 if(flag==1)
	 output();
	 else {
	 cout<<"impossible"<<endl;	
	 } 
     if(t>0)
	 cout<<"---"<<endl;    
    }
    return 0;
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值