朝日新聞2006年3月31日パズル横丁解答

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

Find
    2   3   4   5   6   7  
 2  ×  ○  ○  ○  ○  × 
 3  ○  ×  ○  ○  ×  ○ 
 4  ○  ○  ×  ×  ○  ○ 
 5  ○  ○  ×  ×  ○  ○ 
 6  ○  ×  ○  ○  ×  ○ 
 7  ×  ○  ○  ○  ○  × 

図示すると以下の通り。

判りやすく図示すると以下の通り。

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

#include "puzutl.h"

int val[6] = { 2, 3, 4, 5, 6, 7};
int mat[6][6];

void dump()
{
  int row, col;
  ps( "   ");
  for( col=0; col< 6; col++) {
    ps( "%2d  ", val[col]);
  }
  ps( "\n");
  for( row=0; row< 6; row++) {
    ps( "%2d ", val[row]);
    for( col=0; col< 6; col++) {
      if( mat[row][col]) {
        ps( " ○ ");
      }
      else {
        ps( " × ");
      }
    }
    ps( "\n");
  }
}

void check18()
{
  int sum = 0;
  for( int row=0; row< 6; row++) {
    sum = 0;
    for( int col=0; col< 6; col++) {
      if( mat[row][col]) sum += val[col];
    }
    if( sum != 18) return;
  }
  ps( "Find\n");
  dump();
}

int main( int argc, cstring argv[])
{
  mat[0][0] = mat[1][1] = mat[2][2] = mat[3][3] = mat[4][4] = mat[5][5] = 0;
  for( int i=0; i<= 0x7FFF; i++) {
    if( i&0x0001) mat[0][1] = mat[1][0] = 1; else mat[0][1] = mat[1][0] = 0;
    if( i&0x0002) mat[0][2] = mat[2][0] = 1; else mat[0][2] = mat[2][0] = 0;
    if( i&0x0004) mat[0][3] = mat[3][0] = 1; else mat[0][3] = mat[3][0] = 0;
    if( i&0x0008) mat[0][4] = mat[4][0] = 1; else mat[0][4] = mat[4][0] = 0;
    if( i&0x0010) mat[0][5] = mat[5][0] = 1; else mat[0][5] = mat[5][0] = 0;
    if( i&0x0020) mat[1][2] = mat[2][1] = 1; else mat[1][2] = mat[2][1] = 0;
    if( i&0x0040) mat[1][3] = mat[3][1] = 1; else mat[1][3] = mat[3][1] = 0;
    if( i&0x0080) mat[1][4] = mat[4][1] = 1; else mat[1][4] = mat[4][1] = 0;
    if( i&0x0100) mat[1][5] = mat[5][1] = 1; else mat[1][5] = mat[5][1] = 0;
    if( i&0x0200) mat[2][3] = mat[3][2] = 1; else mat[2][3] = mat[3][2] = 0;
    if( i&0x0400) mat[2][4] = mat[4][2] = 1; else mat[2][4] = mat[4][2] = 0;
    if( i&0x0800) mat[2][5] = mat[5][2] = 1; else mat[2][5] = mat[5][2] = 0;
    if( i&0x1000) mat[3][4] = mat[4][3] = 1; else mat[3][4] = mat[4][3] = 0;
    if( i&0x2000) mat[3][5] = mat[5][3] = 1; else mat[3][5] = mat[5][3] = 0;
    if( i&0x4000) mat[4][5] = mat[5][4] = 1; else mat[4][5] = mat[5][4] = 0;
    check18();
  }
  return    0;
}