poj 2676 Sudoku
2015-07-18 18:12
246 查看
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; int mp[10][10],row[10][10],col[10][10],grid[10][10]; int get(int x,int y) { if(x>=1&&x<=3) { if(y>=1&&y<=3) return 1; else if(y>=4&&y<=6) return 4; else return 7; } else if(4<=x&&x<=6) { if(y>=1&&y<=3) return 2; else if(y>=4&&y<=6) return 5; else return 8; } else { if(y>=1&&y<=3) return 3; else if(y>=4&&y<=6) return 6; else return 9; } } int dfs(int x,int y) { if(x==10) return 1; int flag=0,i; if(mp[x][y]) { if(y==9) flag=dfs(x+1,1); else flag=dfs(x,y+1); if(flag) return 1; else return 0; } else { for(i=1; i<=9; i++) { int k=get(x,y); if(!row[x][i]&&!col[y][i]&&!grid[k][i]) { row[x][i]=1; col[y][i]=1; grid[k][i]=1; mp[x][y]=i; if(y==9) flag=dfs(x+1,1); else flag=dfs(x,y+1); if(flag==0) { row[x][i]=0; col[y][i]=0; grid[k][i]=0; mp[x][y]=0; } else return 1; } } } return 0; } int main() { int i,j,k,_; char s; scanf("%d",&_); while(_--) { memset(row,0,sizeof(row)); memset(col,0,sizeof(col)); memset(grid,0,sizeof(grid)); for(i=1; i<=9; i++) for(j=1; j<=9; j++) { cin>>s; mp[i][j]=s-'0'; if(mp[i][j]) { k=get(i,j); row[i][mp[i][j]]=1; col[j][mp[i][j]]=1; grid[k][mp[i][j]]=1; } } dfs(1,1); for(i=1; i<=9; i++) { for(j=1; j<=9; j++) { printf("%d",mp[i][j]); } printf("\n"); } } return 0; }
相关文章推荐
- 【v2.x OGE课程 15】 布局相关
- [leetcode] 201.Bitwise AND of Numbers Range
- 在2440上实现ramdisk文件系统
- Linux 文件系统分区基础
- 背包九讲
- hdu 1711 Number Sequence(kmp)
- 关于C/S和B/S结构的比较
- hdu 1711 Number Sequence(kmp)
- 近期需要学习的OI知识点
- centos6.5用memcached 来作PHP 的session.save_handler
- 解题报告:人人都有极客精神
- Ubuntu12.04安装Freescale LTIB环境
- Ubuntu12.04安装Freescale LTIB环境
- Ubuntu12.04安装Freescale LTIB环境
- Ubuntu12.04安装Freescale LTIB环境
- Ubuntu12.04安装Freescale LTIB环境
- Ubuntu12.04安装Freescale LTIB环境
- Ubuntu12.04安装Freescale LTIB环境
- Ubuntu12.04安装Freescale LTIB环境
- Ubuntu12.04安装Freescale LTIB环境