• POJ2676 Sudoku [数独]


    好题,也非常有用,犯了几个错误

    1.在枚举赋值的时候,思维有个错误:当当前的赋值不能填完这个数独,应该是继续下一个循环,而不是return false 终止枚举

    2.Generic Programing写错了,,,本来那个memset想写成Generic Programing的,,,然后,永远仅仅有第一组结果对

    不说了,泪哈,,,

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    int map[10][10];
    char tmp[10][10];
    bool row[10][10];
    bool col[10][10];
    bool grid[10][10];
    bool DFS(int x,int y)
    {
    	int here=3*((x-1)/3)+(y-1)/3+1;
    	if(x==10)
    		return true;
    	if(map[x][y])
    	{
    		bool flag;
    		return y==9?flag=DFS(x+1,1):flag=DFS(x,y+1);
    	}
    	else
    	{
    		for(int num=1;num<=9;num++)
    		{
    			if(!row[x][num]&&!col[y][num]&&!grid[here][num])
    			{
    				map[x][y]=num;
    				row[x][num]=true;
    				col[y][num]=true;
    				grid[here][num]=true;
    				bool flag;
    				y==9?flag=DFS(x+1,1):flag=DFS(x,y+1);
    				if(flag)
    					return true;
    				else
    				{
    					map[x][y]=0;
    					row[x][num]=false;
    					col[y][num]=false;
    					grid[here][num]=false;
    				}
    			}
    		}
    	}
    	return false;
    }
    int main()
    {
       //freopen("/home/rainto96/in.txt","r",stdin);
    	int test;
    	cin>>test;
    	while(test--)
    	{
    		memset(map,0,sizeof(map));
    		memset(tmp,0,sizeof(tmp));
    		memset(grid,0,sizeof(grid));
    		memset(col,0,sizeof(col));
    		memset(row,0,sizeof(row));
    		for(int i=1;i<=9;i++)
    		{
    			for(int j=1;j<=9;j++)
    			{
    				cin>>tmp[i][j];
    				int here=3*((i-1)/3)+(j-1)/3+1;
    				map[i][j]=tmp[i][j]-'0';
    				if(map[i][j])
    				{
    					row[i][map[i][j]]=true;
    					col[j][map[i][j]]=true;
    					grid[here][map[i][j]]=true;
    				}
    			}
    		}
    		DFS(1,1);
    		for(int i=1;i<=9;i++)
    		{
    			for(int j=1;j<=9;j++)
    			{
    				cout<<map[i][j];
    			}
    			cout<<'
    ';
    		}
    	}
    	return 0;
    }
    


  • 相关阅读:
    android之app widget(二)
    使用作业异步调用存储过程的示例
    简单两步走 中兴V880获取权限方法
    3D化网页工具:Tilt
    在各种处理中应用排序规则的示例
    GreenSock推出了新一代动画引擎平台GSAP v12
    Derived_Tables_And_Correlated_Subqueries
    六大浏览器帐号管理功能大PK
    Entity Framework 5 Sample Provider简介
    中兴U880刷recovery与刷机详细教程
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/4030747.html
Copyright © 2020-2023  润新知