next up previous contents
Next: 付録B パターンの内積値計算・変更プログラム Up: 無題 Previous: 参考文献

付録A パターン作成プログラム

 

#include <stdio.h>

double drand48();

#define W 0 /* 白 */
#define B 1 /* 黒 */
#define PN 4 /* パターン数 */
#define N1 10 /* 縦または横の数 */
#define N2 N1 /* 同上 */

int P[N1][N2]; /* パターン配列 */
FILE *f;

main(argc,argv)
     int argc;
     char *argv[];
{
  int i,ran_n;
  char fname[50];

  /* コマンドラインのパラメータの使用方法 */
  if(argc != 2){
    printf("pat_s <乱数初期値>\n");
    exit(-1);
  }

  ran_n = atoi(argv[1]);/* 乱数初期値 */
  sprintf(fname,"pat_%lu.dat",ran_n);
  f=fopen(fname,"w");
  srand48(ran_n);
  for(i=0; i<PN; i++)
    ratio(); /* パターン作成 */
  fclose(f);
}

/* パターン作成 */
ratio()
{
  int rat,i,j,sum,n;

  sum=0; /* 平均 0.5 管理変数 */
  
  /* 一列当たりの黒白の数を決定 */
  for(i=0; i<N1; i++){
    rat=(int)(drand48()*11);
    if(rat > (n = N1*N2/2-sum))
      rat = n;
    if(rat < (n = N1*(i-4)-sum))
      rat = n;
    pattern(rat,i); /* 一列のパターンを決定 */
    sum += rat;
  }
  henkou(); /* 縦横を一部順序変更 */

  /* 全体の順序を変更しファイルへ出力 (左右上下)*/
  if(drand48() < 0.5)
    if(drand48() < 0.5)
      for(i=0; i<N1; i++){
        for(j=0; j<N2; j++)
          fprintf(f,"%d ",P[i][j]);
        fprintf(f,"\n");
      }
    else
      for(j=0; j<N2; j++){
        for(i=0; i<N1; i++)
          fprintf(f,"%d ",P[i][j]);
        fprintf(f,"\n");
      }
  else
    if(drand48() < 0.5)
      for(i=N1-1; i>=0; i--){
        for(j=N2-1; j>=0; j--)
          fprintf(f,"%d ",P[i][j]);
        fprintf(f,"\n");
      }
    else
      for(j=N2-1; j>=0; j--){
        for(i=N1-1; i>=0; i--)
          fprintf(f,"%d ",P[i][j]);
        fprintf(f,"\n");
      }
  fprintf(f,"\n");
}

/* 一列のパターンを決定 */
pattern(rat,i)
     int rat,i;
{
  int j,sum;

  sum = 0; /* 黒が rat と等しくなるように作成 */
  for(j=0; j<N2; j++)
    {
      if(drand48() < (double)rat/10)
        P[i][j] = B;
      else
        P[i][j] = W;
      if(sum == rat)
        P[i][j] = W;
      if(j-sum == N2-rat)
        P[i][j] = B;
      sum += P[i][j];
    }
}

/* 縦横を一部順序変更 */
henkou()
{
  int a,b,i,j;

  /* 縦 (または横) */
  for(i=0; i<N1; i++){
    if(drand48() < 0.5)
      for(j=0; j< N2/2; j++){
        a=P[i][j];
        P[i][j]=P[i][N2-1-j];
        P[i][N2-1-j]=a;
      }
  }
  /* 横(または縦) */
  for(j=0; j<N2; j++){
    if(drand48() < 0.5)
      for(i=0; i<N1/2; i++){
        b=P[i][j];
        P[i][j]=P[N1-1-i][j];
        P[N1-1-i][j]=b;
      }
  }
}



Deguchi Toshinori
1996年09月05日 (木) 11時50分24秒 JST