Scippy

    SCIP

    Solving Constraint Integer Programs

    sudoku_utils.h
    Go to the documentation of this file.
    1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    2/* */
    3/* This file is part of the program and library */
    4/* SCIP --- Solving Constraint Integer Programs */
    5/* */
    6/* Copyright (c) 2002-2025 Zuse Institute Berlin (ZIB) */
    7/* */
    8/* Licensed under the Apache License, Version 2.0 (the "License"); */
    9/* you may not use this file except in compliance with the License. */
    10/* You may obtain a copy of the License at */
    11/* */
    12/* http://www.apache.org/licenses/LICENSE-2.0 */
    13/* */
    14/* Unless required by applicable law or agreed to in writing, software */
    15/* distributed under the License is distributed on an "AS IS" BASIS, */
    16/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
    17/* See the License for the specific language governing permissions and */
    18/* limitations under the License. */
    19/* */
    20/* You should have received a copy of the Apache-2.0 license */
    21/* along with SCIP; see the file LICENSE. If not visit scipopt.org. */
    22/* */
    23/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    24
    25/**@file sudoku_utils.h
    26 * @brief A set of utilities that are used to read the puzzle and display the puzzle
    27 * @author Naga V C Gudapati
    28*/
    29
    30#include <iostream>
    31#include <fstream>
    32#include <vector>
    33#include <string>
    34
    35namespace sudoku
    36{
    37 /** reads in the sudoku puzzle from filepath
    38 *
    39 * Reads the string of sudoku puzzle into a 9x9 grid represented by a vector
    40 * of a vector of ints. The actual number is stored as itself and the blanks are stored as -1.
    41 *
    42 */
    43 inline std::vector<std::vector<int>> getSudokuPuzzle( const std::string &filepath )
    44 {
    45 /* setting up a 9x9 grid forstoring the sudoku puzzle. */
    46 std::vector<std::vector<int>> puzzle(9, std::vector<int>(9));
    47
    48 /* Reading the puzzle into a stringstream */
    49 std::ifstream infile(filepath);
    50
    51 std::string puzzledata = "";
    52
    53 if( infile.is_open() )
    54 {
    55 (void) std::getline(infile, puzzledata);
    56 if( puzzledata.length() != 81 ) /* The puzzle should have 81 characters */
    57 {
    58 std::cerr << "Please check the puzzle file forinconsistencies"
    59 << "\n";
    60 exit(1);
    61 }
    62 }
    63
    64 size_t idx = 0; /* This variable will be used to access the numbers in the puzzle string */
    65
    66 for( size_t i = 0; i < 9; ++i )
    67 {
    68 for( size_t j = 0; j < 9; ++j )
    69 {
    70 /* We will only convert the numeric string to an integer if it is not '.' or '0'. */
    71 if( (puzzledata.substr(idx, 1) != ".") && (puzzledata.substr(idx, 1) != "0") )
    72 {
    73 puzzle[i][j] = std::stoi( puzzledata.substr(idx, 1) );
    74 }
    75 else
    76 {
    77 /* If we are currently reading a '.' or '0' make it -1. */
    78 puzzle[i][j] = -1;
    79 }
    80 idx++;
    81 }
    82 }
    83
    84 return puzzle;
    85 }
    86
    87 /** prints the sudoku puzzle to console */
    88 inline void printSudoku( const std::vector<std::vector<int>> &sudokupuzzle )
    89 {
    90 std::cout << "+----------+-----------+-----------+" << "\n";
    91 for( size_t i = 0; i < 9; ++i )
    92 {
    93 std::cout << "|";
    94 for( size_t j = 0; j < 9; ++j )
    95 {
    96 if( sudokupuzzle[i][j] > 0 )
    97 {
    98
    99 if( j == 2 || j == 5 || j == 8 )
    100 {
    101 std::cout << sudokupuzzle[i][j] << " | ";
    102 }
    103 else
    104 {
    105 std::cout << sudokupuzzle[i][j] << " ";
    106 }
    107 }
    108 else
    109 {
    110 if( j == 2 || j == 5 || j == 8 )
    111 {
    112 std::cout << "*" << " | ";
    113 }
    114 else
    115 {
    116 std::cout << "*" << " ";
    117 }
    118 }
    119 }
    120 std::cout << "\n";
    121
    122 if( i == 2 || i == 5 || i == 8 )
    123 {
    124 std::cout << "+----------+-----------+-----------+" << "\n";
    125 }
    126 }
    127 }
    128} /* namespace sudoku */
    std::vector< std::vector< int > > getSudokuPuzzle(const std::string &filepath)
    Definition: sudoku_utils.h:43
    void printSudoku(const std::vector< std::vector< int > > &sudokupuzzle)
    Definition: sudoku_utils.h:88