设为首页 加入收藏

TOP

POJ2676 Sudoku [数独]
2015-07-24 05:50:32 来源: 作者: 【 】 浏览:4
Tags:POJ2676 Sudoku 数独

好题,也很实用,犯了几个错误

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

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

不说了,泪哈,,,

#include 
  
   
#include 
   
     #include 
    
      #include 
     
       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<
      
       

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Longest Consecutive Sequence 下一篇poj 3740 Easy Finding(Dancing L..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: