Files
C-Plus-Plus/d3/d05/sudoku__solver_8cpp.html
2023-01-22 19:44:10 +00:00

501 lines
36 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.6"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C++: backtracking/sudoku_solver.cpp File Reference</title>
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../resize.js"></script>
<script type="text/javascript" src="../../navtreedata.js"></script>
<script type="text/javascript" src="../../navtree.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectalign">
<div id="projectname">Algorithms_in_C++<span id="projectnumber">&#160;1.0.0</span>
</div>
<div id="projectbrief">Set of algorithms implemented in C++.</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.6 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
/* @license-end */
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
initMenu('../../',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function(){initNavTree('d3/d05/sudoku__solver_8cpp.html','../../'); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>
<div class="header">
<div class="summary">
<a href="#namespaces">Namespaces</a> &#124;
<a href="#func-members">Functions</a> </div>
<div class="headertitle"><div class="title">sudoku_solver.cpp File Reference</div></div>
</div><!--header-->
<div class="contents">
<p><a href="https://en.wikipedia.org/wiki/Sudoku" target="_blank">Sudoku Solver</a> algorithm.
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;array&gt;</code><br />
<code>#include &lt;iostream&gt;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for sudoku_solver.cpp:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../da/dfd/sudoku__solver_8cpp__incl.svg" width="162" height="124"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="namespaces" name="namespaces"></a>
Namespaces</h2></td></tr>
<tr class="memitem:db/dc0/namespacebacktracking"><td class="memItemLeft" align="right" valign="top">namespace &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../db/dc0/namespacebacktracking.html">backtracking</a></td></tr>
<tr class="memdesc:db/dc0/namespacebacktracking"><td class="mdescLeft">&#160;</td><td class="mdescRight">for <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a> <br /></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:d8/d9f/namespacesudoku__solver"><td class="memItemLeft" align="right" valign="top">namespace &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d8/d9f/namespacesudoku__solver.html">sudoku_solver</a></td></tr>
<tr class="memdesc:d8/d9f/namespacesudoku__solver"><td class="mdescLeft">&#160;</td><td class="mdescRight">Functions for the <a href="https://en.wikipedia.org/wiki/Sudoku" target="_blank">Sudoku Solver</a> implementation. <br /></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:a07dc6acffd0500de9bdbf16b3ade94b0"><td class="memTemplParams" colspan="2">template&lt;size_t V&gt; </td></tr>
<tr class="memitem:a07dc6acffd0500de9bdbf16b3ade94b0"><td class="memTemplItemLeft" align="right" valign="top">bool&#160;</td><td class="memTemplItemRight" valign="bottom"><a class="el" href="../../d3/d05/sudoku__solver_8cpp.html#a07dc6acffd0500de9bdbf16b3ade94b0">backtracking::sudoku_solver::isPossible</a> (const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a>&lt; <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a>&lt; int, V &gt;, V &gt; &amp;mat, int i, int j, int no, int n)</td></tr>
<tr class="memdesc:a07dc6acffd0500de9bdbf16b3ade94b0"><td class="mdescLeft">&#160;</td><td class="mdescRight">Check if it's possible to place a number (<code>no</code> parameter) <br /></td></tr>
<tr class="separator:a07dc6acffd0500de9bdbf16b3ade94b0"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ab040a12d7684cd85fb3684f4211ea5ac"><td class="memTemplParams" colspan="2">template&lt;size_t V&gt; </td></tr>
<tr class="memitem:ab040a12d7684cd85fb3684f4211ea5ac"><td class="memTemplItemLeft" align="right" valign="top">void&#160;</td><td class="memTemplItemRight" valign="bottom"><a class="el" href="../../d3/d05/sudoku__solver_8cpp.html#ab040a12d7684cd85fb3684f4211ea5ac">backtracking::sudoku_solver::printMat</a> (const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a>&lt; <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a>&lt; int, V &gt;, V &gt; &amp;mat, const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a>&lt; <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a>&lt; int, V &gt;, V &gt; &amp;starting_mat, int n)</td></tr>
<tr class="memdesc:ab040a12d7684cd85fb3684f4211ea5ac"><td class="mdescLeft">&#160;</td><td class="mdescRight">Utility function to print the matrix. <br /></td></tr>
<tr class="separator:ab040a12d7684cd85fb3684f4211ea5ac"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ac911c8bca8556206ff64461b2424866b"><td class="memTemplParams" colspan="2">template&lt;size_t V&gt; </td></tr>
<tr class="memitem:ac911c8bca8556206ff64461b2424866b"><td class="memTemplItemLeft" align="right" valign="top">bool&#160;</td><td class="memTemplItemRight" valign="bottom"><a class="el" href="../../d3/d05/sudoku__solver_8cpp.html#ac911c8bca8556206ff64461b2424866b">backtracking::sudoku_solver::solveSudoku</a> (<a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a>&lt; <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a>&lt; int, V &gt;, V &gt; &amp;mat, const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a>&lt; <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a>&lt; int, V &gt;, V &gt; &amp;starting_mat, int i, int j)</td></tr>
<tr class="memdesc:ac911c8bca8556206ff64461b2424866b"><td class="mdescLeft">&#160;</td><td class="mdescRight">Main function to implement the Sudoku algorithm. <br /></td></tr>
<tr class="separator:ac911c8bca8556206ff64461b2424866b"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d3/d05/sudoku__solver_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a> ()</td></tr>
<tr class="memdesc:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="mdescLeft">&#160;</td><td class="mdescRight">Main function. <br /></td></tr>
<tr class="separator:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p><a href="https://en.wikipedia.org/wiki/Sudoku" target="_blank">Sudoku Solver</a> algorithm. </p>
<p>Sudoku (数独, sūdoku, digit-single) (/suːˈdoʊkuː/, /-ˈdɒk-/, /sə-/, originally called Number Place) is a logic-based, combinatorial number-placement puzzle. In classic sudoku, the objective is to fill a 9×9 grid with digits so that each column, each row, and each of the nine 3×3 subgrids that compose the grid (also called "boxes", "blocks", or "regions") contain all of the digits from 1 to 9. The puzzle setter provides a partially completed grid, which for a well-posed puzzle has a single solution.</p>
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/DarthCoder3200" target="_blank">DarthCoder3200</a> </dd>
<dd>
<a href="https://github.com/Panquesito7" target="_blank">David Leal</a> </dd></dl>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="a07dc6acffd0500de9bdbf16b3ade94b0" name="a07dc6acffd0500de9bdbf16b3ade94b0"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a07dc6acffd0500de9bdbf16b3ade94b0">&#9670;&#160;</a></span>isPossible()</h2>
<div class="memitem">
<div class="memproto">
<div class="memtemplate">
template&lt;size_t V&gt; </div>
<table class="memname">
<tr>
<td class="memname">bool backtracking::sudoku_solver::isPossible </td>
<td>(</td>
<td class="paramtype">const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a>&lt; <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a>&lt; int, V &gt;, V &gt; &amp;&#160;</td>
<td class="paramname"><em>mat</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>i</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>j</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>no</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>n</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Check if it's possible to place a number (<code>no</code> parameter) </p>
<dl class="tparams"><dt>Template Parameters</dt><dd>
<table class="tparams">
<tr><td class="paramname">V</td><td>number of vertices in the array </td></tr>
</table>
</dd>
</dl>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">mat</td><td>matrix where numbers are saved </td></tr>
<tr><td class="paramname">i</td><td>current index in rows </td></tr>
<tr><td class="paramname">j</td><td>current index in columns </td></tr>
<tr><td class="paramname">no</td><td>number to be added in matrix </td></tr>
<tr><td class="paramname">n</td><td>number of times loop will run </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd><code>true</code> if 'mat' is different from 'no' </dd>
<dd>
<code>false</code> if 'mat' equals to 'no' </dd></dl>
<p><code>no</code> shouldn't be present in either row i or column j</p>
<p><code>no</code> shouldn't be present in the 3*3 subgrid</p>
<div class="fragment"><div class="line"><span class="lineno"> 45</span> {<span class="comment"></span></div>
<div class="line"><span class="lineno"> 46</span><span class="comment"> /// `no` shouldn&#39;t be present in either row i or column j</span></div>
<div class="line"><span class="lineno"> 47</span><span class="comment"></span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> x = 0; x &lt; n; x++) {</div>
<div class="line"><span class="lineno"> 48</span> <span class="keywordflow">if</span> (mat[x][j] == no || mat[i][x] == no) {</div>
<div class="line"><span class="lineno"> 49</span> <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"><span class="lineno"> 50</span> }</div>
<div class="line"><span class="lineno"> 51</span> }</div>
<div class="line"><span class="lineno"> 52</span><span class="comment"></span> </div>
<div class="line"><span class="lineno"> 53</span><span class="comment"> /// `no` shouldn&#39;t be present in the 3*3 subgrid</span></div>
<div class="line"><span class="lineno"> 54</span><span class="comment"></span> <span class="keywordtype">int</span> sx = (i / 3) * 3;</div>
<div class="line"><span class="lineno"> 55</span> <span class="keywordtype">int</span> sy = (j / 3) * 3;</div>
<div class="line"><span class="lineno"> 56</span> </div>
<div class="line"><span class="lineno"> 57</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> x = sx; x &lt; sx + 3; x++) {</div>
<div class="line"><span class="lineno"> 58</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> y = sy; y &lt; sy + 3; y++) {</div>
<div class="line"><span class="lineno"> 59</span> <span class="keywordflow">if</span> (mat[x][y] == no) {</div>
<div class="line"><span class="lineno"> 60</span> <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"><span class="lineno"> 61</span> }</div>
<div class="line"><span class="lineno"> 62</span> }</div>
<div class="line"><span class="lineno"> 63</span> }</div>
<div class="line"><span class="lineno"> 64</span> </div>
<div class="line"><span class="lineno"> 65</span> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><span class="lineno"> 66</span>}</div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d3/d05/sudoku__solver_8cpp_a07dc6acffd0500de9bdbf16b3ade94b0_cgraph.svg" width="158" height="75"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="ae66f6b31b5ad750f1fe042a706a4e3d4" name="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ae66f6b31b5ad750f1fe042a706a4e3d4">&#9670;&#160;</a></span>main()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int main </td>
<td>(</td>
<td class="paramtype">void&#160;</td>
<td class="paramname"></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Main function. </p>
<dl class="section return"><dt>Returns</dt><dd>0 on exit </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 154</span> {</div>
<div class="line"><span class="lineno"> 155</span> <span class="keyword">const</span> <span class="keywordtype">int</span> V = 9;</div>
<div class="line"><span class="lineno"> 156</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array&lt;std::array&lt;int, V&gt;</a>, V&gt; mat = {</div>
<div class="line"><span class="lineno"> 157</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array&lt;int, V&gt;</a>{5, 3, 0, 0, 7, 0, 0, 0, 0},</div>
<div class="line"><span class="lineno"> 158</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array&lt;int, V&gt;</a>{6, 0, 0, 1, 9, 5, 0, 0, 0},</div>
<div class="line"><span class="lineno"> 159</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array&lt;int, V&gt;</a>{0, 9, 8, 0, 0, 0, 0, 6, 0},</div>
<div class="line"><span class="lineno"> 160</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array&lt;int, V&gt;</a>{8, 0, 0, 0, 6, 0, 0, 0, 3},</div>
<div class="line"><span class="lineno"> 161</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array&lt;int, V&gt;</a>{4, 0, 0, 8, 0, 3, 0, 0, 1},</div>
<div class="line"><span class="lineno"> 162</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array&lt;int, V&gt;</a>{7, 0, 0, 0, 2, 0, 0, 0, 6},</div>
<div class="line"><span class="lineno"> 163</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array&lt;int, V&gt;</a>{0, 6, 0, 0, 0, 0, 2, 8, 0},</div>
<div class="line"><span class="lineno"> 164</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array&lt;int, V&gt;</a>{0, 0, 0, 4, 1, 9, 0, 0, 5},</div>
<div class="line"><span class="lineno"> 165</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array&lt;int, V&gt;</a>{0, 0, 0, 0, 8, 0, 0, 7, 9}};</div>
<div class="line"><span class="lineno"> 166</span> </div>
<div class="line"><span class="lineno"> 167</span> backtracking::sudoku_solver::printMat&lt;V&gt;(mat, mat, 9);</div>
<div class="line"><span class="lineno"> 168</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;Solution &quot;</span> &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 169</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array&lt;std::array&lt;int, V&gt;</a>, V&gt; starting_mat = mat;</div>
<div class="line"><span class="lineno"> 170</span> backtracking::sudoku_solver::solveSudoku&lt;V&gt;(mat, starting_mat, 0, 0);</div>
<div class="line"><span class="lineno"> 171</span> </div>
<div class="line"><span class="lineno"> 172</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 173</span>}</div>
<div class="ttc" id="aarray_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a></div></div>
<div class="ttc" id="abasic_ostream_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a></div></div>
<div class="ttc" id="aendl_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a></div><div class="ttdeci">T endl(T... args)</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d3/d05/sudoku__solver_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg" width="182" height="36"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="ab040a12d7684cd85fb3684f4211ea5ac" name="ab040a12d7684cd85fb3684f4211ea5ac"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ab040a12d7684cd85fb3684f4211ea5ac">&#9670;&#160;</a></span>printMat()</h2>
<div class="memitem">
<div class="memproto">
<div class="memtemplate">
template&lt;size_t V&gt; </div>
<table class="memname">
<tr>
<td class="memname">void backtracking::sudoku_solver::printMat </td>
<td>(</td>
<td class="paramtype">const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a>&lt; <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a>&lt; int, V &gt;, V &gt; &amp;&#160;</td>
<td class="paramname"><em>mat</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a>&lt; <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a>&lt; int, V &gt;, V &gt; &amp;&#160;</td>
<td class="paramname"><em>starting_mat</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>n</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Utility function to print the matrix. </p>
<dl class="tparams"><dt>Template Parameters</dt><dd>
<table class="tparams">
<tr><td class="paramname">V</td><td>number of vertices in array </td></tr>
</table>
</dd>
</dl>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">mat</td><td>matrix where numbers are saved </td></tr>
<tr><td class="paramname">starting_mat</td><td>copy of mat, required by printMat for highlighting the differences </td></tr>
<tr><td class="paramname">n</td><td>number of times loop will run </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>void </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 78</span> {</div>
<div class="line"><span class="lineno"> 79</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; n; i++) {</div>
<div class="line"><span class="lineno"> 80</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j &lt; n; j++) {</div>
<div class="line"><span class="lineno"> 81</span> <span class="keywordflow">if</span> (starting_mat[i][j] != mat[i][j]) {</div>
<div class="line"><span class="lineno"> 82</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;\033[93m&quot;</span> &lt;&lt; mat[i][j] &lt;&lt; <span class="stringliteral">&quot;\033[0m&quot;</span></div>
<div class="line"><span class="lineno"> 83</span> &lt;&lt; <span class="stringliteral">&quot; &quot;</span>;</div>
<div class="line"><span class="lineno"> 84</span> } <span class="keywordflow">else</span> {</div>
<div class="line"><span class="lineno"> 85</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; mat[i][j] &lt;&lt; <span class="stringliteral">&quot; &quot;</span>;</div>
<div class="line"><span class="lineno"> 86</span> }</div>
<div class="line"><span class="lineno"> 87</span> <span class="keywordflow">if</span> ((j + 1) % 3 == 0) {</div>
<div class="line"><span class="lineno"> 88</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="charliteral">&#39;\t&#39;</span>;</div>
<div class="line"><span class="lineno"> 89</span> }</div>
<div class="line"><span class="lineno"> 90</span> }</div>
<div class="line"><span class="lineno"> 91</span> <span class="keywordflow">if</span> ((i + 1) % 3 == 0) {</div>
<div class="line"><span class="lineno"> 92</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 93</span> }</div>
<div class="line"><span class="lineno"> 94</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><span class="lineno"> 95</span> }</div>
<div class="line"><span class="lineno"> 96</span>}</div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d3/d05/sudoku__solver_8cpp_ab040a12d7684cd85fb3684f4211ea5ac_cgraph.svg" width="278" height="75"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="ac911c8bca8556206ff64461b2424866b" name="ac911c8bca8556206ff64461b2424866b"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ac911c8bca8556206ff64461b2424866b">&#9670;&#160;</a></span>solveSudoku()</h2>
<div class="memitem">
<div class="memproto">
<div class="memtemplate">
template&lt;size_t V&gt; </div>
<table class="memname">
<tr>
<td class="memname">bool backtracking::sudoku_solver::solveSudoku </td>
<td>(</td>
<td class="paramtype"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a>&lt; <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a>&lt; int, V &gt;, V &gt; &amp;&#160;</td>
<td class="paramname"><em>mat</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a>&lt; <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a>&lt; int, V &gt;, V &gt; &amp;&#160;</td>
<td class="paramname"><em>starting_mat</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>i</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>j</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Main function to implement the Sudoku algorithm. </p>
<dl class="tparams"><dt>Template Parameters</dt><dd>
<table class="tparams">
<tr><td class="paramname">V</td><td>number of vertices in array </td></tr>
</table>
</dd>
</dl>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">mat</td><td>matrix where numbers are saved </td></tr>
<tr><td class="paramname">starting_mat</td><td>copy of mat, required by printMat for highlighting the differences </td></tr>
<tr><td class="paramname">i</td><td>current index in rows </td></tr>
<tr><td class="paramname">j</td><td>current index in columns </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd><code>true</code> if 'no' was placed </dd>
<dd>
<code>false</code> if 'no' was not placed </dd></dl>
<p>Base Case</p>
<p>Solved for 9 rows already</p>
<p>Crossed the last Cell in the row</p>
<p>Blue Cell - Skip</p>
<p>White Cell Try to place every possible no</p>
<p>Place the 'no' - assuming a solution will exist</p>
<p>Couldn't find a solution loop will place the next <code>no</code>.</p>
<p><a class="el" href="../../dd/d4f/class_solution.html">Solution</a> couldn't be found for any of the numbers provided</p>
<div class="fragment"><div class="line"><span class="lineno"> 112</span> {<span class="comment"></span></div>
<div class="line"><span class="lineno"> 113</span><span class="comment"> /// Base Case</span></div>
<div class="line"><span class="lineno"> 114</span><span class="comment"></span> <span class="keywordflow">if</span> (i == 9) {<span class="comment"></span></div>
<div class="line"><span class="lineno"> 115</span><span class="comment"> /// Solved for 9 rows already</span></div>
<div class="line"><span class="lineno"> 116</span><span class="comment"></span> printMat&lt;V&gt;(mat, starting_mat, 9);</div>
<div class="line"><span class="lineno"> 117</span> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><span class="lineno"> 118</span> }</div>
<div class="line"><span class="lineno"> 119</span><span class="comment"></span> </div>
<div class="line"><span class="lineno"> 120</span><span class="comment"> /// Crossed the last Cell in the row</span></div>
<div class="line"><span class="lineno"> 121</span><span class="comment"></span> <span class="keywordflow">if</span> (j == 9) {</div>
<div class="line"><span class="lineno"> 122</span> <span class="keywordflow">return</span> solveSudoku&lt;V&gt;(mat, starting_mat, i + 1, 0);</div>
<div class="line"><span class="lineno"> 123</span> }</div>
<div class="line"><span class="lineno"> 124</span><span class="comment"></span> </div>
<div class="line"><span class="lineno"> 125</span><span class="comment"> /// Blue Cell - Skip</span></div>
<div class="line"><span class="lineno"> 126</span><span class="comment"></span> <span class="keywordflow">if</span> (mat[i][j] != 0) {</div>
<div class="line"><span class="lineno"> 127</span> <span class="keywordflow">return</span> solveSudoku&lt;V&gt;(mat, starting_mat, i, j + 1);</div>
<div class="line"><span class="lineno"> 128</span> }<span class="comment"></span></div>
<div class="line"><span class="lineno"> 129</span><span class="comment"> /// White Cell</span></div>
<div class="line"><span class="lineno"> 130</span><span class="comment"> /// Try to place every possible no</span></div>
<div class="line"><span class="lineno"> 131</span><span class="comment"></span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> no = 1; no &lt;= 9; no++) {</div>
<div class="line"><span class="lineno"> 132</span> <span class="keywordflow">if</span> (isPossible&lt;V&gt;(mat, i, j, no, 9)) {<span class="comment"></span></div>
<div class="line"><span class="lineno"> 133</span><span class="comment"> /// Place the &#39;no&#39; - assuming a solution will exist</span></div>
<div class="line"><span class="lineno"> 134</span><span class="comment"></span> mat[i][j] = no;</div>
<div class="line"><span class="lineno"> 135</span> <span class="keywordtype">bool</span> solution_found = solveSudoku&lt;V&gt;(mat, starting_mat, i, j + 1);</div>
<div class="line"><span class="lineno"> 136</span> <span class="keywordflow">if</span> (solution_found) {</div>
<div class="line"><span class="lineno"> 137</span> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><span class="lineno"> 138</span> }<span class="comment"></span></div>
<div class="line"><span class="lineno"> 139</span><span class="comment"> /// Couldn&#39;t find a solution</span></div>
<div class="line"><span class="lineno"> 140</span><span class="comment"> /// loop will place the next `no`.</span></div>
<div class="line"><span class="lineno"> 141</span><span class="comment"></span> }</div>
<div class="line"><span class="lineno"> 142</span> }<span class="comment"></span></div>
<div class="line"><span class="lineno"> 143</span><span class="comment"> /// Solution couldn&#39;t be found for any of the numbers provided</span></div>
<div class="line"><span class="lineno"> 144</span><span class="comment"></span> mat[i][j] = 0;</div>
<div class="line"><span class="lineno"> 145</span> <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"><span class="lineno"> 146</span>}</div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d3/d05/sudoku__solver_8cpp_ac911c8bca8556206ff64461b2424866b_cgraph.svg" width="158" height="75"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="../../dir_c11585dfcef32a26e29098facab6c144.html">backtracking</a></li><li class="navelem"><a class="el" href="../../d3/d05/sudoku__solver_8cpp.html">sudoku_solver.cpp</a></li>
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.6 </li>
</ul>
</div>
</body>
</html>