プログラムの実行結果は以下の通り。
□□■□□■□□ □□■□□■□□ ■■■■■■■■ □□■□□■□□ □□■□□■□□ ■■■■■■■■ □□■□□■□□ □□■□□■□□
これを絵にすると以下の通り。
プログラムのソースは以下の通り。
#include "puzutl.h" const int RED = 1; const int BLACK = 2; int mat[8][8]; struct struct_undefrowcol { int m_row; int m_col; }; void getcolor( int row, int col, int& cntRed, int& cntBlack, int& cntUndef, struct_undefrowcol undefRowCol[]) { if( row < 0 || row >= 8) return; if( col < 0 || col >= 8) return; int color = mat[row][col]; if( color == RED) cntRed++; else if( color == BLACK) cntBlack++; else { undefRowCol[cntUndef].m_row = row; undefRowCol[cntUndef].m_col = col; cntUndef++;} } void countColor( int row, int col, int& cntRed, int& cntBlack, int& cntUndef, struct_undefrowcol undefRowCol[]) { cntRed = cntBlack = cntUndef = 0; getcolor( row-1, col-1, cntRed, cntBlack, cntUndef, undefRowCol); getcolor( row-1, col+0, cntRed, cntBlack, cntUndef, undefRowCol); getcolor( row-1, col+1, cntRed, cntBlack, cntUndef, undefRowCol); getcolor( row , col-1, cntRed, cntBlack, cntUndef, undefRowCol); getcolor( row , col+0, cntRed, cntBlack, cntUndef, undefRowCol); getcolor( row , col+1, cntRed, cntBlack, cntUndef, undefRowCol); getcolor( row+1, col-1, cntRed, cntBlack, cntUndef, undefRowCol); getcolor( row+1, col+0, cntRed, cntBlack, cntUndef, undefRowCol); getcolor( row+1, col+1, cntRed, cntBlack, cntUndef, undefRowCol); } void decided( int row, int col) { struct_undefrowcol undefRowCol[8]; int cntRed, cntBlack, cntUndef, i; countColor( row, col, cntRed, cntBlack, cntUndef, undefRowCol); // mat[row][col]の周辺の色を調べる switch( cntRed) { case 4: // 残りは全て黒 for( i=0; i< cntUndef; i++) { mat[undefRowCol[i].m_row][undefRowCol[i].m_col] = BLACK; } for( i=0; i< cntUndef; i++) { decided( undefRowCol[i].m_row, undefRowCol[i].m_col); } break; case 3: // 残りが1なら全て赤,2以上なら確定できず case 2: case 1: case 0: if( cntUndef == 4-cntRed) { for( i=0; i< cntUndef; i++) { mat[undefRowCol[i].m_row][undefRowCol[i].m_col] = RED; } for( i=0; i< cntUndef; i++) { decided( undefRowCol[i].m_row, undefRowCol[i].m_col); } } break; default: pe( "Program ERROR\n"); break; } } int main( int argc, cstring argv[]) { cstring colorstr[] = { "?", "□", "■"}; mat[0][0] = RED; decided( 0, 0); for( int row=0; row< 8; row++) { for( int col=0; col< 8; col++) { ps( "%s", colorstr[mat[row][col]]); } ps( "\n"); } return 0; }