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;
    }
    
  • 相关阅读:
    [转]在自己的项目中调用别人的库的方法(static lib库,dynamic lib库以及dll动态库)
    前端、后台、客户端以及服务器
    C# 使用j
    C# Linq技术中SelectMany() 获取list对象的属性 汇总成为一个新的集合
    C# 集合的扩展方法-查询表达式GroupBy()的使用 转
    C# ASP.NET MVC中Filter过滤器的使用 通过注册到Global.asax注册为全局 过滤所有action
    C# ASP.NET MVC中有四种Filter 过滤器类型 通过标签 Attribute加到action上面
    C# 《Asp.Net Web Api 》-----路由机制 转
    C# ASP.NET MVC5路由系统机制详细讲解(转)
    MVC 自定义数据校验规则 Validation
  • 原文地址:https://www.cnblogs.com/herumw/p/9464814.html
Copyright © 2011-2022 走看看