随机产生数独初始值
随机产生数独初始值。
#include <iostream> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <algorithm> using namespace std; void print(vector<vector<int> >d){ for(int i=0;i<d.size();i++){ for(int j=0;j<d[i].size();j++){ cout<<d[i][j]<<" "; } cout<<endl; } } void generateNum(vector<vector<int> >&d,int sum){ srand( (unsigned)time( NULL ) ); int flag=false; while(sum){ int v = rand()%9+1; int m = rand()%9; int n = rand()%9; flag=false; if(d[m][n]==-1){ for(int j=0;j<d[0].size();j++){ if(d[m][j] == v){ cout<<"-----"<<m<<j<<"--"<<d[m][j]<<endl; flag=true; break; } } for(int i=0;i<d.size();i++){ if(d[i][n] == v){ flag=true; break; } } if(flag) continue; d[m][n]=v; sum--; } } } int main(){ vector<vector<int> >d(9,vector<int>(9,-1)); print(d); generateNum(d,50); print(d); }