zoukankan      html  css  js  c++  java
  • poj2676 Sudoku

    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
    这道题可以用深搜,题意是补完数独中的空格数,0代表空格。用了三个剪枝,同行,同列还有同一个大格子的数字不能重复。
    
    
    #include<stdio.h>
    #include<string.h>
    char s[12][12];
    int row[12][12],col[12][12],dage[12][12],map[12][12],flag;
    void dfs(int x,int y)
    {
    	int i,j,k;
    	if(x==10){
    		flag=1;return;
    	}
    	if(map[x][y]!=0){
    		if(y==9){
    			dfs(x+1,1);
    		}
    		else dfs(x,y+1);
    		if(flag)return;
    	}
    	else if(map[x][y]==0){
    	    k=(x-1)/3*3+(y-1)/3+1;
    		for(i=1;i<=9;i++){
    			if(row[x][i]==0 && col[y][i]==0 && dage[k][i]==0){
    				map[x][y]=i;
    				row[x][i]=1;col[y][i]=1;dage[k][i]=1;
    				if(y==9){
    					dfs(x+1,1);
    				}
    				else dfs(x,y+1);
    			    if(flag==1)return;
    			    map[x][y]=0;row[x][i]=0;col[y][i]=0;dage[k][i]=0;
    			}
    		}
    	}
    }
    
    
    int main()
    {
    	int n,m,i,j,T,k,flag1;
    	scanf("%d",&T);
    	while(T--)
    	{
    		memset(row,0,sizeof(row));memset(col,0,sizeof(col));memset(dage,0,sizeof(dage));memset(map,0,sizeof(map));
    		flag1=1;
    		for(i=1;i<=9;i++){
    			scanf("%s",s[i]+1);
    			for(j=1;j<=9;j++){
    				map[i][j]=s[i][j]-'0';
    				k=(i-1)/3*3+(j-1)/3+1;
    				if(map[i][j]!=0){
    					if(row[i][map[i][j]]!=0 || col[j][map[i][j]]!=0 || dage[k][map[i][j]]!=0){
    						flag1=0;break;
    					}
    					row[i][map[i][j]]=1;
    					col[j][map[i][j]]=1;
    					dage[k][map[i][j]]=1;
    				}
    			}
    			if(flag1==0)break;
    		}
    		if(flag1==0){
    			for(i=1;i<=9;i++){
    				printf("%s
    ",s[i]+1);
    			}
    		}
    		flag=0;
    		dfs(1,1);
    		if(flag==0){
    			for(i=1;i<=9;i++){
    				printf("%s
    ",s[i]+1);
    			}
    		}
    		else{
    			for(i=1;i<=9;i++){
    				for(j=1;j<=9;j++){
    					printf("%d",map[i][j]);
    				}
    				printf("
    ");
    			}
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    vue.js---利用vue cli脚手架工具+webpack创建项目遇到的坑
    List<KeyValuePair<TKey,TValue>> 与 Dictionary<TKey,TValue> 不同
    StackExchange.Redis 官方文档(一) Basics
    离线安装chrome插件
    Cocoapods的安装与使用
    多线程
    作业二:个人编程项目——编写一个能自动生成小学四则运算题目的程序
    初学者如何在博客园发布博客
    作业一:建立博客、自我介绍、速读教材、学习进度总结
    计科131同学们的博客
  • 原文地址:https://www.cnblogs.com/herumw/p/9464814.html
Copyright © 2011-2022 走看看