加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

利用C++实现随机生成数独程序(原创)

(2011-07-29 20:44:00)
标签:

杂谈

分类: c

转载请注明出处

#include "stdafx.h"
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
#include<stdio.h>

class shudu
{
 int i,j;
 int b[9][9],answer[9][9];
 int a[9];
 public:
  shudu()
  {
   i = 0;j = 0;
   for(i=0;i<9;i++)
    a[i] = i+1;
  }
void shiftarray(int a[9]);
void creatsudo();
};
void shudu::shiftarray(int a[9])
{
 int tmp = a[0];
 for(i=1;i<9;i++)
  a[i-1] = a[i];
 a[8] = tmp;
}
void print(int array[9][9])
{
 cout<<"++++++print the array+++++++"<<endl;
 int i,j;
  for(i=0;i<9;i++)
  {for(j=0;j<9;j++)
    cout<<array[i][j]<<" ";
  cout<<endl;
  }
  cout<<"++++++end print++++++\n";
  
}
void swap(int &a,int &b)
{
 int tmp;
 tmp = a;
 a = b;
 b = tmp;
}
void shudu::creatsudo()
{
 cout<<"Now I will create a math array"<<endl;
 srand(time(0));
 
 int m,c[9]={0};
 int tmpi,tmpj;
 for(j=0;j<9;j++)
 {
 for(i=0;i<9;i++)
  b[j][i] =a[i];
 shiftarray(a);
 }//init the array that satisfy the array sudo asked

 for(i=0;i<4;i++)
 { tmpi = rand()%9;
   tmpj = rand()%9;
   for(j=0;j<9;j++)
    swap(b[j][tmpi],b[j][tmpj]);
 }
 //chang col
 for(i=0;i<4;i++)
 { tmpi = rand()%9;
   tmpj = rand()%9;
   for(j=0;j<9;j++)
   {
    swap(b[tmpi][j],b[tmpj][j]);
  
   }
 }
//chang row
//print the array that generate
 for(i=0;i<9;i++)
  for(j=0;j<9;j++)
   answer[i][j] = b[i][j];
  for(i=0;i<9;i++)
  for(j=0;j<4;j++)
  {
   tmpi = rand()%9;
    b[i][tmpi] = 0;
   //swap two number
  }

  for(i=0;i<9;i++)
  for(j=0;j<9;j++)
 {
  if(b[i][j]==0)
   cout<<"# ";
   else cout<<b[i][j]<<" ";
   //swap two number
  if(j==8)
   cout<<"\n";
 }
  cout<<" if you want to know the answer ,you can type #"<<endl;
  char p;
  cin>>p;
  if(p =='#')
   print(answer);
  else return ;
}
int main()

{

shudu a;

a.creatsudo();

}


C语言生成数独的程序以解无聊
made by 沈岩

0

阅读 收藏 喜欢 打印举报/Report
前一篇:fedora使用文档
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有