数独ゲームにおける検証ロジックについて詳細を説明します。
行単位の検証処理:
各横一行に対して、1から9までの数字がすべて含まれているか確認します。特定の数値を固定し、配列内の各行と比較していきます。最後まで一致する要素が見つからない場合(インデックスが8に達した際)、エラーを出力してプログラムを終了します。
int fixed_value;
for(int row_index = 0; row_index < 9; row_index++){
for(int target_num = 1; target_num <= 9; target_num++){
fixed_value = 0;
while(grid[row_index][fixed_value] != target_num){
if(fixed_value == 8){
printf("Invalid Input!!! Please Retry!!!");
system("pause");
exit(1);
}
fixed_value++;
}
}
}
列単位の検証処理:
行検証と同様のロジックで、縦列ごとの数字の重複チェックを行います。
void validate_columns(){
int row_pos;
for(int col_index = 0; col_index < 9; col_index++){
for(int num_check = 1; num_check <= 9; num_check++){
row_pos = 0;
while(grid[row_pos][col_index] != num_check){
if(row_pos == 8){
printf("Invalid Input!!! Please Retry!!!");
system("pause");
exit(1);
}
row_pos++;
}
}
}
}
3×3ブロック単位の検証処理:
数独では行・列だけでなく、3×3の小領域内でも1から9の重複しない数字配置が必要です。
https://sudokusolving.bmcx.com/
このアルゴリズムでは、外側のループでブロック位置を指定し、内側で各数字の存在確認を行います。内部カウンター(row_offset、col_offset)でブロック内を移動し、境界条件(2,2位置)に達しても目的の数字が見つからない場合、プログラム全体を強制終了します。
void validate_blocks(){
int row_offset, col_offset, search_target;
for(int block_row = 0; block_row < 9; block_row += 3){
for(int block_col = 0; block_col < 9; block_col += 3){
for(search_target = 1; search_target <= 9; search_target++){
row_offset = 0;
col_offset = 0;
while(grid[block_row + row_offset][block_col + col_offset] != search_target){
if(row_offset == 2 && col_offset == 2){
printf("Invalid Input!!! Please Retry!!!");
system("pause");
exit(1);
}
col_offset++;
if(col_offset == 3){
row_offset++;
col_offset = 0;
}
}
}
}
}
}
数独解答データの復号化:
空マス(0)を埋める処理において、文字列形式の入力を数値計算に対応させるため変換が必要です。解答データを抽出し、XOR演算で隠蔽されたフラグ情報を復元します。
encrypted_data = b"8291767138932581849755263447186268341129653538127"
key_sequence = [
107, 2, 102, 112, 68, 105, 126, 110, 67, 74,
120, 74, 109, 96, 86, 0, 81, 89, 80, 67,
80, 81, 109, 116, 2, 85, 80, 82, 110, 111,
121, 64, 93, 75, 30, 25, 28, 116, 3, 84,
7, 76, 82, 106, 96, 80, 88, 64, 88, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0
]
decrypted_result = []
for index in range(len(encrypted_data)):
decrypted_result.append(chr(encrypted_data[index] ^ key_sequence[index]))
print(''.join(decrypted_result))
#moectf{S0_As_I_prAy_Un1imited_B1ade_WOrks---E1m1ya_Shir