Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:AC~欢迎交流In Reply To:AC~欢迎交流 Posted by:yueyibo at 2016-11-19 16:17:16 > http://blog.csdn.net/luckcircle/article/details/53191366 #include<cstdlib> #include<cstring> #include<cstdio> #include<vector> #include<iostream> using namespace std; int rowNum[9][10]; int colNum[9][10]; int blockNum[9][10]; int a[9][9]; struct Position{ int r; int c; Position(int r,int c):r(r),c(c){} }; vector<Position> pos; int getBlock(int r,int c){ int rr=r/3; int cc=c/3; return(rr*3+cc); } void setAllFlags(int i,int j,int num,int f){ rowNum[i][num]=f; colNum[j][num]=f; blockNum[getBlock(i,j)][num]=f; } bool isOk(int r,int c,int num){ return !rowNum[r][num]&&!colNum[c][num]&&!blockNum[getBlock(r,c)][num]; } bool DFS(int n){ if(n<0)return true; int r=pos[n].r; int c=pos[n].c; for(int num=1;num<=9;num++){ if(isOk(r,c,num)){ a[r][c]=num; setAllFlags(r,c,num,1); if(DFS(n-1)){ return true; } setAllFlags(r,c,num,0); } } return false; } int main(){ int t; scanf("%d",&t); while(t--){ memset(rowNum,0,sizeof(rowNum)); memset(colNum,0,sizeof(colNum)); memset(blockNum,0,sizeof(blockNum)); pos.clear(); char c; for(int i=0;i<9;i++){ for(int j=0;j<9;j++){ scanf("%c",&c); a[i][j]=c-'0'; if(a[i][j]==0){ pos.push_back(Position(i,j)); }else{ setAllFlags(i,j,a[i][j],1); } } } if(DFS(pos.size()-1)){ for(int i=0;i<9;i++){ for(int j=0;j<9;j++){ printf("%c",char(a[i][j]+'0')); } } printf("\n"); } } return 0; } 输入一个数就崩了,找了两天也没找出错在哪,希望您帮忙看看代码,多谢啦 Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator