朝日新聞2005年10月7日パズル横丁解答

プログラムの実行結果は以下の通り。

3 9 1
 6
2 4 7
8   5

これを図示すると以下のようになる。

プログラムのソースは以下の通り。

#include "puzutl.h"

YesNo used[10];

int main( int argc, cstring argv[])
{
  for( int a=1; a< 10; a++) {
    used[a] = YES;
    for( int b=1; b< 10; b++) {
      if( used[b]) continue;
      used[b] = YES;
      for( int c=1; c< 10; c++) {
        if( used[c]) continue;
        used[c] = YES;
        for( int d=1; d< 10; d++) {
          if( used[d]) continue;
          used[d] = YES;
          for( int e=1; e< 10; e++) {
            if( used[e]) continue;
            used[e] = YES;
            for( int f=1; f< 10; f++) {
              if( used[f]) continue;
              used[f] = YES;
              for( int g=1; g< 10; g++) {
                if( used[g]) continue;
                used[g] = YES;
                for( int h=1; h< 10; h++) {
                  if( used[h]) continue;
                  used[h] = YES;
                  for( int i=1; i< 10; i++) {
                    if( used[i]) continue;
                    used[i] = YES;
                    int x[] = { a+b+c,
                                d+e+f,
                                g+h,
                                a+d+g,
                                b+e,
                                c+f+h,
                                a+i+e,
                                c+e+g};
                    int n = sizeof(x)/sizeof(x[0]);
                    YesNo eq = YES;
                    for( int k=1; k< n; k++) {
                      if( x[0] != x[k]) { eq = NO; break; }
                    }
                    if( eq) {
                      ps( "%d %d %d\n", a, b, c);
                      ps( " %d\n", i);
                      ps( "%d %d %d\n", d, e, f);
                      ps( "%d   %d\n", g, h);
                    }
                    used[i] = NO;
                  }
                  used[h] = NO;
                }
                used[g] = NO;
              }
              used[f] = NO;
            }
            used[e] = NO;
          }
          used[d] = NO;
        }
        used[c] = NO;
      }
      used[b] = NO;
    }
    used[a] = NO;
  }
  return    0;
}