TheAlgorithms-C/d5/df4/group__sudoku.html
2020-09-03 12:54:15 +00:00

588 lines
40 KiB
HTML

<!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">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.20"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C: Sudoku solver</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 style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">Algorithms_in_C
&#160;<span id="projectnumber">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.8.20 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
/* @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:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(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:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('d5/df4/group__sudoku.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">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="header">
<div class="summary">
<a href="#nested-classes">Data Structures</a> &#124;
<a href="#func-members">Functions</a> </div>
<div class="headertitle">
<div class="title">Sudoku solver</div> </div>
</div><!--header-->
<div class="contents">
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="nested-classes"></a>
Data Structures</h2></td></tr>
<tr class="memitem:"><td class="memItemLeft" align="right" valign="top">struct &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../dc/d18/structsudoku.html">sudoku</a></td></tr>
<tr class="memdesc:"><td class="mdescLeft">&#160;</td><td class="mdescRight">Structure to hold the matrix and dimensions. <a href="../../dc/d18/structsudoku.html#details">More...</a><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 name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:ga85d25d3b40017436f264a103685e4c55"><td class="memItemLeft" align="right" valign="top">bool&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d5/df4/group__sudoku.html#ga85d25d3b40017436f264a103685e4c55">OKrow</a> (const struct <a class="el" href="../../dc/d18/structsudoku.html">sudoku</a> *a, int x, int y, int v)</td></tr>
<tr class="memdesc:ga85d25d3b40017436f264a103685e4c55"><td class="mdescLeft">&#160;</td><td class="mdescRight">Check if <code>x</code>^th row is valid. <a href="../../d5/df4/group__sudoku.html#ga85d25d3b40017436f264a103685e4c55">More...</a><br /></td></tr>
<tr class="separator:ga85d25d3b40017436f264a103685e4c55"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga6503128d4f5ce0a0826f72f73f9e0b2a"><td class="memItemLeft" align="right" valign="top">bool&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d5/df4/group__sudoku.html#ga6503128d4f5ce0a0826f72f73f9e0b2a">OKcol</a> (const struct <a class="el" href="../../dc/d18/structsudoku.html">sudoku</a> *a, int x, int y, int v)</td></tr>
<tr class="memdesc:ga6503128d4f5ce0a0826f72f73f9e0b2a"><td class="mdescLeft">&#160;</td><td class="mdescRight">Check if <code>y</code>^th column is valid. <a href="../../d5/df4/group__sudoku.html#ga6503128d4f5ce0a0826f72f73f9e0b2a">More...</a><br /></td></tr>
<tr class="separator:ga6503128d4f5ce0a0826f72f73f9e0b2a"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga1cd43df3f4187845ce186042fe53e6f1"><td class="memItemLeft" align="right" valign="top">bool&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d5/df4/group__sudoku.html#ga1cd43df3f4187845ce186042fe53e6f1">OKbox</a> (const struct <a class="el" href="../../dc/d18/structsudoku.html">sudoku</a> *a, int x, int y, int v)</td></tr>
<tr class="memdesc:ga1cd43df3f4187845ce186042fe53e6f1"><td class="mdescLeft">&#160;</td><td class="mdescRight">Check if a 3x3 box is valid. <a href="../../d5/df4/group__sudoku.html#ga1cd43df3f4187845ce186042fe53e6f1">More...</a><br /></td></tr>
<tr class="separator:ga1cd43df3f4187845ce186042fe53e6f1"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga3a0ec150ac9d0cb6b28279d36d95d72f"><td class="memItemLeft" align="right" valign="top">bool&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d5/df4/group__sudoku.html#ga3a0ec150ac9d0cb6b28279d36d95d72f">OK</a> (const struct <a class="el" href="../../dc/d18/structsudoku.html">sudoku</a> *a, int x, int y, int v)</td></tr>
<tr class="memdesc:ga3a0ec150ac9d0cb6b28279d36d95d72f"><td class="mdescLeft">&#160;</td><td class="mdescRight">Check if element <code>v</code> is valid to place at (x,y) location. <a href="../../d5/df4/group__sudoku.html#ga3a0ec150ac9d0cb6b28279d36d95d72f">More...</a><br /></td></tr>
<tr class="separator:ga3a0ec150ac9d0cb6b28279d36d95d72f"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga702ff4f95dde780c7d04fcdd1021b6c1"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d5/df4/group__sudoku.html#ga702ff4f95dde780c7d04fcdd1021b6c1">print</a> (const struct <a class="el" href="../../dc/d18/structsudoku.html">sudoku</a> *a)</td></tr>
<tr class="memdesc:ga702ff4f95dde780c7d04fcdd1021b6c1"><td class="mdescLeft">&#160;</td><td class="mdescRight">Print the matrix to stdout. <a href="../../d5/df4/group__sudoku.html#ga702ff4f95dde780c7d04fcdd1021b6c1">More...</a><br /></td></tr>
<tr class="separator:ga702ff4f95dde780c7d04fcdd1021b6c1"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga62e94fc39f116e2c81daed8f5437431b"><td class="memItemLeft" align="right" valign="top">bool&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d5/df4/group__sudoku.html#ga62e94fc39f116e2c81daed8f5437431b">get_next_unknown</a> (const struct <a class="el" href="../../dc/d18/structsudoku.html">sudoku</a> *a, int *x, int *y)</td></tr>
<tr class="memdesc:ga62e94fc39f116e2c81daed8f5437431b"><td class="mdescLeft">&#160;</td><td class="mdescRight">Find and get the location for next empty cell. <a href="../../d5/df4/group__sudoku.html#ga62e94fc39f116e2c81daed8f5437431b">More...</a><br /></td></tr>
<tr class="separator:ga62e94fc39f116e2c81daed8f5437431b"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:gadfe0ed5085b4775d8fa00b434cc0fdfc"><td class="memItemLeft" align="right" valign="top">bool&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d5/df4/group__sudoku.html#gadfe0ed5085b4775d8fa00b434cc0fdfc">solve</a> (struct <a class="el" href="../../dc/d18/structsudoku.html">sudoku</a> *a)</td></tr>
<tr class="memdesc:gadfe0ed5085b4775d8fa00b434cc0fdfc"><td class="mdescLeft">&#160;</td><td class="mdescRight">Function to solve a partially filled sudoku matrix. <a href="../../d5/df4/group__sudoku.html#gadfe0ed5085b4775d8fa00b434cc0fdfc">More...</a><br /></td></tr>
<tr class="separator:gadfe0ed5085b4775d8fa00b434cc0fdfc"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<h2 class="groupheader">Function Documentation</h2>
<a id="ga62e94fc39f116e2c81daed8f5437431b"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga62e94fc39f116e2c81daed8f5437431b">&#9670;&nbsp;</a></span>get_next_unknown()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">bool get_next_unknown </td>
<td>(</td>
<td class="paramtype">const struct <a class="el" href="../../dc/d18/structsudoku.html">sudoku</a> *&#160;</td>
<td class="paramname"><em>a</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int *&#160;</td>
<td class="paramname"><em>x</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int *&#160;</td>
<td class="paramname"><em>y</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Find and get the location for next empty cell. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">a</td><td>pointer to sudoku instance </td></tr>
<tr><td class="paramdir">[out]</td><td class="paramname">x</td><td>pointer to row index of next unknown </td></tr>
<tr><td class="paramdir">[out]</td><td class="paramname">y</td><td>pointer to column index of next unknown </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd><code>true</code> if an empty location was found </dd>
<dd>
<code>false</code> if no more empty locations found </dd></dl>
<div class="fragment"><div class="line"><a name="l00145"></a><span class="lineno"> 145</span>&#160;{</div>
<div class="line"><a name="l00146"></a><span class="lineno"> 146</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; a-&gt;N; i++)</div>
<div class="line"><a name="l00147"></a><span class="lineno"> 147</span>&#160; {</div>
<div class="line"><a name="l00148"></a><span class="lineno"> 148</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j &lt; a-&gt;N; j++)</div>
<div class="line"><a name="l00149"></a><span class="lineno"> 149</span>&#160; {</div>
<div class="line"><a name="l00150"></a><span class="lineno"> 150</span>&#160; <span class="keywordflow">if</span> (a-&gt;a[i * a-&gt;N + j] == 0)</div>
<div class="line"><a name="l00151"></a><span class="lineno"> 151</span>&#160; {</div>
<div class="line"><a name="l00152"></a><span class="lineno"> 152</span>&#160; *x = i;</div>
<div class="line"><a name="l00153"></a><span class="lineno"> 153</span>&#160; *y = j;</div>
<div class="line"><a name="l00154"></a><span class="lineno"> 154</span>&#160; <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><a name="l00155"></a><span class="lineno"> 155</span>&#160; }</div>
<div class="line"><a name="l00156"></a><span class="lineno"> 156</span>&#160; }</div>
<div class="line"><a name="l00157"></a><span class="lineno"> 157</span>&#160; }</div>
<div class="line"><a name="l00158"></a><span class="lineno"> 158</span>&#160; </div>
<div class="line"><a name="l00159"></a><span class="lineno"> 159</span>&#160; <span class="comment">/* no unknown locations found */</span></div>
<div class="line"><a name="l00160"></a><span class="lineno"> 160</span>&#160; <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"><a name="l00161"></a><span class="lineno"> 161</span>&#160;}</div>
</div><!-- fragment -->
</div>
</div>
<a id="ga3a0ec150ac9d0cb6b28279d36d95d72f"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga3a0ec150ac9d0cb6b28279d36d95d72f">&#9670;&nbsp;</a></span>OK()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">bool OK </td>
<td>(</td>
<td class="paramtype">const struct <a class="el" href="../../dc/d18/structsudoku.html">sudoku</a> *&#160;</td>
<td class="paramname"><em>a</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>x</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>y</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>v</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Check if element <code>v</code> is valid to place at (x,y) location. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">a</td><td><a class="el" href="../../dc/d18/structsudoku.html" title="Structure to hold the matrix and dimensions.">sudoku</a> to check </td></tr>
<tr><td class="paramname">x</td><td>row to place value </td></tr>
<tr><td class="paramname">y</td><td>column to place value </td></tr>
<tr><td class="paramname">v</td><td>value to check if it is valid </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd><code>true</code> if valid </dd>
<dd>
<code>false</code> if in-valid </dd></dl>
<div class="fragment"><div class="line"><a name="l00112"></a><span class="lineno"> 112</span>&#160;{</div>
<div class="line"><a name="l00113"></a><span class="lineno"> 113</span>&#160; <span class="keywordtype">bool</span> result = <a class="code" href="../../d5/df4/group__sudoku.html#ga85d25d3b40017436f264a103685e4c55">OKrow</a>(a, x, y, v);</div>
<div class="line"><a name="l00114"></a><span class="lineno"> 114</span>&#160; <span class="keywordflow">if</span> (result)</div>
<div class="line"><a name="l00115"></a><span class="lineno"> 115</span>&#160; result = <a class="code" href="../../d5/df4/group__sudoku.html#ga6503128d4f5ce0a0826f72f73f9e0b2a">OKcol</a>(a, x, y, v);</div>
<div class="line"><a name="l00116"></a><span class="lineno"> 116</span>&#160; <span class="keywordflow">if</span> (result)</div>
<div class="line"><a name="l00117"></a><span class="lineno"> 117</span>&#160; result = <a class="code" href="../../d5/df4/group__sudoku.html#ga1cd43df3f4187845ce186042fe53e6f1">OKbox</a>(a, x, y, v);</div>
<div class="line"><a name="l00118"></a><span class="lineno"> 118</span>&#160; </div>
<div class="line"><a name="l00119"></a><span class="lineno"> 119</span>&#160; <span class="keywordflow">return</span> result;</div>
<div class="line"><a name="l00120"></a><span class="lineno"> 120</span>&#160;}</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="../../d5/df4/group__sudoku_ga3a0ec150ac9d0cb6b28279d36d95d72f_cgraph.svg" width="163" height="139"><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="ga1cd43df3f4187845ce186042fe53e6f1"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga1cd43df3f4187845ce186042fe53e6f1">&#9670;&nbsp;</a></span>OKbox()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">bool OKbox </td>
<td>(</td>
<td class="paramtype">const struct <a class="el" href="../../dc/d18/structsudoku.html">sudoku</a> *&#160;</td>
<td class="paramname"><em>a</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>x</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>y</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>v</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Check if a 3x3 box is valid. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">a</td><td>matrix to check </td></tr>
<tr><td class="paramname">x</td><td>row index of the element to check </td></tr>
<tr><td class="paramname">y</td><td>column index of the element to check </td></tr>
<tr><td class="paramname">v</td><td>value to check if it repeats </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd><code>true</code> if valid </dd>
<dd>
<code>false</code> if in-valid </dd></dl>
<div class="fragment"><div class="line"><a name="l00086"></a><span class="lineno"> 86</span>&#160;{</div>
<div class="line"><a name="l00087"></a><span class="lineno"> 87</span>&#160; <span class="comment">/* get start indices of the box that the current (x,y) lies in</span></div>
<div class="line"><a name="l00088"></a><span class="lineno"> 88</span>&#160;<span class="comment"> remember that in C/C++, division operation always rounds towards</span></div>
<div class="line"><a name="l00089"></a><span class="lineno"> 89</span>&#160;<span class="comment"> -infinity for signed integers and towards 0 for unsigned integers</span></div>
<div class="line"><a name="l00090"></a><span class="lineno"> 90</span>&#160;<span class="comment"> */</span></div>
<div class="line"><a name="l00091"></a><span class="lineno"> 91</span>&#160; <span class="keywordtype">int</span> bi = x - x % a-&gt;N2, bj = y - y % a-&gt;N2;</div>
<div class="line"><a name="l00092"></a><span class="lineno"> 92</span>&#160; <span class="comment">// printf(&quot;Checking box: (%d,%d)\n&quot;, bi, bj);</span></div>
<div class="line"><a name="l00093"></a><span class="lineno"> 93</span>&#160; </div>
<div class="line"><a name="l00094"></a><span class="lineno"> 94</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = bi; i &lt; (bi + a-&gt;N2); i++)</div>
<div class="line"><a name="l00095"></a><span class="lineno"> 95</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = bj; j &lt; (bj + a-&gt;N2); j++)</div>
<div class="line"><a name="l00096"></a><span class="lineno"> 96</span>&#160; <span class="keywordflow">if</span> (a-&gt;a[i * a-&gt;N + j] == v)</div>
<div class="line"><a name="l00097"></a><span class="lineno"> 97</span>&#160; <span class="comment">// if the value is found in the box</span></div>
<div class="line"><a name="l00098"></a><span class="lineno"> 98</span>&#160; <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"><a name="l00099"></a><span class="lineno"> 99</span>&#160; <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><a name="l00100"></a><span class="lineno"> 100</span>&#160;}</div>
</div><!-- fragment -->
</div>
</div>
<a id="ga6503128d4f5ce0a0826f72f73f9e0b2a"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga6503128d4f5ce0a0826f72f73f9e0b2a">&#9670;&nbsp;</a></span>OKcol()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">bool OKcol </td>
<td>(</td>
<td class="paramtype">const struct <a class="el" href="../../dc/d18/structsudoku.html">sudoku</a> *&#160;</td>
<td class="paramname"><em>a</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>x</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>y</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>v</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Check if <code>y</code>^th column is valid. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">a</td><td><a class="el" href="../../dc/d18/structsudoku.html" title="Structure to hold the matrix and dimensions.">sudoku</a> to check </td></tr>
<tr><td class="paramname">x</td><td>ignored row </td></tr>
<tr><td class="paramname">y</td><td>column to check </td></tr>
<tr><td class="paramname">v</td><td>value to check if it repeats </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd><code>true</code> if valid </dd>
<dd>
<code>false</code> if in-valid </dd></dl>
<div class="fragment"><div class="line"><a name="l00068"></a><span class="lineno"> 68</span>&#160;{</div>
<div class="line"><a name="l00069"></a><span class="lineno"> 69</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; a-&gt;N; i++)</div>
<div class="line"><a name="l00070"></a><span class="lineno"> 70</span>&#160; <span class="keywordflow">if</span> (a-&gt;a[i * a-&gt;N + y] == v)</div>
<div class="line"><a name="l00071"></a><span class="lineno"> 71</span>&#160; <span class="comment">// if the value is found in the column</span></div>
<div class="line"><a name="l00072"></a><span class="lineno"> 72</span>&#160; <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"><a name="l00073"></a><span class="lineno"> 73</span>&#160; <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><a name="l00074"></a><span class="lineno"> 74</span>&#160;}</div>
</div><!-- fragment -->
</div>
</div>
<a id="ga85d25d3b40017436f264a103685e4c55"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga85d25d3b40017436f264a103685e4c55">&#9670;&nbsp;</a></span>OKrow()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">bool OKrow </td>
<td>(</td>
<td class="paramtype">const struct <a class="el" href="../../dc/d18/structsudoku.html">sudoku</a> *&#160;</td>
<td class="paramname"><em>a</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>x</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>y</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int&#160;</td>
<td class="paramname"><em>v</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Check if <code>x</code>^th row is valid. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">a</td><td><a class="el" href="../../dc/d18/structsudoku.html" title="Structure to hold the matrix and dimensions.">sudoku</a> to check </td></tr>
<tr><td class="paramname">x</td><td>row to check </td></tr>
<tr><td class="paramname">y</td><td>ignored column </td></tr>
<tr><td class="paramname">v</td><td>value to check if it repeats </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd><code>true</code> if valid </dd>
<dd>
<code>false</code> if in-valid </dd></dl>
<div class="fragment"><div class="line"><a name="l00049"></a><span class="lineno"> 49</span>&#160;{</div>
<div class="line"><a name="l00050"></a><span class="lineno"> 50</span>&#160; <span class="keywordtype">int</span> offset = x * a-&gt;N;</div>
<div class="line"><a name="l00051"></a><span class="lineno"> 51</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j &lt; a-&gt;N; j++)</div>
<div class="line"><a name="l00052"></a><span class="lineno"> 52</span>&#160; <span class="keywordflow">if</span> (a-&gt;a[offset + j] == v)</div>
<div class="line"><a name="l00053"></a><span class="lineno"> 53</span>&#160; <span class="comment">// if the value is found in the row</span></div>
<div class="line"><a name="l00054"></a><span class="lineno"> 54</span>&#160; <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"><a name="l00055"></a><span class="lineno"> 55</span>&#160; <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><a name="l00056"></a><span class="lineno"> 56</span>&#160;}</div>
</div><!-- fragment -->
</div>
</div>
<a id="ga702ff4f95dde780c7d04fcdd1021b6c1"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga702ff4f95dde780c7d04fcdd1021b6c1">&#9670;&nbsp;</a></span>print()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void print </td>
<td>(</td>
<td class="paramtype">const struct <a class="el" href="../../dc/d18/structsudoku.html">sudoku</a> *&#160;</td>
<td class="paramname"><em>a</em></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Print the matrix to stdout. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">a</td><td>array to print </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><a name="l00127"></a><span class="lineno"> 127</span>&#160;{</div>
<div class="line"><a name="l00128"></a><span class="lineno"> 128</span>&#160; <span class="keywordtype">int</span> i, j;</div>
<div class="line"><a name="l00129"></a><span class="lineno"> 129</span>&#160; <span class="keywordflow">for</span> (i = 0; i &lt; a-&gt;N; i++)</div>
<div class="line"><a name="l00130"></a><span class="lineno"> 130</span>&#160; <span class="keywordflow">for</span> (j = 0; j &lt; a-&gt;N; j++)</div>
<div class="line"><a name="l00131"></a><span class="lineno"> 131</span>&#160; printf(<span class="stringliteral">&quot;%&quot;</span> SCNu8 <span class="stringliteral">&quot;%c&quot;</span>, a-&gt;a[i * a-&gt;N + j],</div>
<div class="line"><a name="l00132"></a><span class="lineno"> 132</span>&#160; (j == a-&gt;N - 1 ? <span class="charliteral">&#39;\n&#39;</span> : <span class="charliteral">&#39; &#39;</span>));</div>
<div class="line"><a name="l00133"></a><span class="lineno"> 133</span>&#160;}</div>
</div><!-- fragment -->
</div>
</div>
<a id="gadfe0ed5085b4775d8fa00b434cc0fdfc"></a>
<h2 class="memtitle"><span class="permalink"><a href="#gadfe0ed5085b4775d8fa00b434cc0fdfc">&#9670;&nbsp;</a></span>solve()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">bool solve </td>
<td>(</td>
<td class="paramtype">struct <a class="el" href="../../dc/d18/structsudoku.html">sudoku</a> *&#160;</td>
<td class="paramname"><em>a</em></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Function to solve a partially filled sudoku matrix. </p>
<p>For each unknown value (0), the function fills a possible value and calls the function again to check forvalid solution.</p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in,out]</td><td class="paramname">a</td><td>sudoku matrix to solve </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd><code>true</code> if solution found </dd>
<dd>
<code>false</code> if no solution found </dd></dl>
<div class="fragment"><div class="line"><a name="l00173"></a><span class="lineno"> 173</span>&#160;{</div>
<div class="line"><a name="l00174"></a><span class="lineno"> 174</span>&#160; <span class="keyword">static</span> uint32_t counter = 0;</div>
<div class="line"><a name="l00175"></a><span class="lineno"> 175</span>&#160; <span class="keywordtype">int</span> i, j;</div>
<div class="line"><a name="l00176"></a><span class="lineno"> 176</span>&#160; <span class="keyword">static</span> <span class="keywordtype">char</span> prefix[100] = <span class="stringliteral">&quot;&quot;</span>; <span class="comment">// enough memory</span></div>
<div class="line"><a name="l00177"></a><span class="lineno"> 177</span>&#160; </div>
<div class="line"><a name="l00178"></a><span class="lineno"> 178</span>&#160; <span class="keywordflow">if</span> (!<a class="code" href="../../d5/df4/group__sudoku.html#ga62e94fc39f116e2c81daed8f5437431b">get_next_unknown</a>(a, &amp;i, &amp;j))</div>
<div class="line"><a name="l00179"></a><span class="lineno"> 179</span>&#160; {</div>
<div class="line"><a name="l00180"></a><span class="lineno"> 180</span>&#160; <span class="comment">/* no more empty location found</span></div>
<div class="line"><a name="l00181"></a><span class="lineno"> 181</span>&#160;<span class="comment"> implies all good in the matrix</span></div>
<div class="line"><a name="l00182"></a><span class="lineno"> 182</span>&#160;<span class="comment"> */</span></div>
<div class="line"><a name="l00183"></a><span class="lineno"> 183</span>&#160; <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><a name="l00184"></a><span class="lineno"> 184</span>&#160; }</div>
<div class="line"><a name="l00185"></a><span class="lineno"> 185</span>&#160; </div>
<div class="line"><a name="l00186"></a><span class="lineno"> 186</span>&#160; <span class="comment">/* try all possible values for the unknown */</span></div>
<div class="line"><a name="l00187"></a><span class="lineno"> 187</span>&#160; <span class="keywordflow">for</span> (uint8_t v = 1; v &lt;= a-&gt;N; v++)</div>
<div class="line"><a name="l00188"></a><span class="lineno"> 188</span>&#160; { <span class="comment">/* try all possible values 1 thru N */</span></div>
<div class="line"><a name="l00189"></a><span class="lineno"> 189</span>&#160; printf(<span class="stringliteral">&quot;%sTry (%d,%d) = %&quot;</span> SCNu8 <span class="stringliteral">&quot;... &quot;</span>, prefix, i, j, v);</div>
<div class="line"><a name="l00190"></a><span class="lineno"> 190</span>&#160; counter++;</div>
<div class="line"><a name="l00191"></a><span class="lineno"> 191</span>&#160; <span class="keywordflow">if</span> (<a class="code" href="../../d5/df4/group__sudoku.html#ga3a0ec150ac9d0cb6b28279d36d95d72f">OK</a>(a, i, j, v))</div>
<div class="line"><a name="l00192"></a><span class="lineno"> 192</span>&#160; {</div>
<div class="line"><a name="l00193"></a><span class="lineno"> 193</span>&#160; <span class="comment">/* if assignment checks satisfy, set the value and</span></div>
<div class="line"><a name="l00194"></a><span class="lineno"> 194</span>&#160;<span class="comment"> continue with remaining elements */</span></div>
<div class="line"><a name="l00195"></a><span class="lineno"> 195</span>&#160; printf(<span class="stringliteral">&quot;passed (counter=%&quot;</span> SCNu32 <span class="stringliteral">&quot;)\n&quot;</span>, counter);</div>
<div class="line"><a name="l00196"></a><span class="lineno"> 196</span>&#160; a-&gt;a[i * a-&gt;N + j] = v;</div>
<div class="line"><a name="l00197"></a><span class="lineno"> 197</span>&#160; strcat(prefix, <span class="stringliteral">&quot; &quot;</span>);</div>
<div class="line"><a name="l00198"></a><span class="lineno"> 198</span>&#160; <span class="keywordflow">if</span> (<a class="code" href="../../d5/df4/group__sudoku.html#gadfe0ed5085b4775d8fa00b434cc0fdfc">solve</a>(a))</div>
<div class="line"><a name="l00199"></a><span class="lineno"> 199</span>&#160; {</div>
<div class="line"><a name="l00200"></a><span class="lineno"> 200</span>&#160; <span class="comment">/* solution found */</span></div>
<div class="line"><a name="l00201"></a><span class="lineno"> 201</span>&#160; <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><a name="l00202"></a><span class="lineno"> 202</span>&#160; }</div>
<div class="line"><a name="l00203"></a><span class="lineno"> 203</span>&#160; </div>
<div class="line"><a name="l00204"></a><span class="lineno"> 204</span>&#160; printf(<span class="stringliteral">&quot;%sBacktrack (%d,%d) &lt;- %&quot;</span> SCNu8 <span class="stringliteral">&quot; (counter=%&quot;</span> SCNu32 <span class="stringliteral">&quot;)\n&quot;</span>,</div>
<div class="line"><a name="l00205"></a><span class="lineno"> 205</span>&#160; prefix, i, j, a-&gt;a[i * a-&gt;N + j], counter);</div>
<div class="line"><a name="l00206"></a><span class="lineno"> 206</span>&#160; </div>
<div class="line"><a name="l00207"></a><span class="lineno"> 207</span>&#160; prefix[strlen(prefix) - 2] = <span class="charliteral">&#39;\0&#39;</span>; <span class="comment">// truncate the prefix</span></div>
<div class="line"><a name="l00208"></a><span class="lineno"> 208</span>&#160; a-&gt;a[i * a-&gt;N + j] = 0;</div>
<div class="line"><a name="l00209"></a><span class="lineno"> 209</span>&#160; }</div>
<div class="line"><a name="l00210"></a><span class="lineno"> 210</span>&#160; <span class="keywordflow">else</span></div>
<div class="line"><a name="l00211"></a><span class="lineno"> 211</span>&#160; {</div>
<div class="line"><a name="l00212"></a><span class="lineno"> 212</span>&#160; printf(<span class="stringliteral">&quot;\r&quot;</span>);</div>
<div class="line"><a name="l00213"></a><span class="lineno"> 213</span>&#160; }</div>
<div class="line"><a name="l00214"></a><span class="lineno"> 214</span>&#160; }</div>
<div class="line"><a name="l00215"></a><span class="lineno"> 215</span>&#160; </div>
<div class="line"><a name="l00216"></a><span class="lineno"> 216</span>&#160; <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"><a name="l00217"></a><span class="lineno"> 217</span>&#160;}</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="../../d5/df4/group__sudoku_gadfe0ed5085b4775d8fa00b434cc0fdfc_cgraph.svg" width="354" height="139"><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 -->
<div class="ttc" id="agroup__sudoku_html_ga62e94fc39f116e2c81daed8f5437431b"><div class="ttname"><a href="../../d5/df4/group__sudoku.html#ga62e94fc39f116e2c81daed8f5437431b">get_next_unknown</a></div><div class="ttdeci">bool get_next_unknown(const struct sudoku *a, int *x, int *y)</div><div class="ttdoc">Find and get the location for next empty cell.</div><div class="ttdef"><b>Definition:</b> sudoku_solver.c:144</div></div>
<div class="ttc" id="agroup__sudoku_html_gadfe0ed5085b4775d8fa00b434cc0fdfc"><div class="ttname"><a href="../../d5/df4/group__sudoku.html#gadfe0ed5085b4775d8fa00b434cc0fdfc">solve</a></div><div class="ttdeci">bool solve(struct sudoku *a)</div><div class="ttdoc">Function to solve a partially filled sudoku matrix.</div><div class="ttdef"><b>Definition:</b> sudoku_solver.c:172</div></div>
<div class="ttc" id="agroup__sudoku_html_ga85d25d3b40017436f264a103685e4c55"><div class="ttname"><a href="../../d5/df4/group__sudoku.html#ga85d25d3b40017436f264a103685e4c55">OKrow</a></div><div class="ttdeci">bool OKrow(const struct sudoku *a, int x, int y, int v)</div><div class="ttdoc">Check if x^th row is valid.</div><div class="ttdef"><b>Definition:</b> sudoku_solver.c:48</div></div>
<div class="ttc" id="agroup__sudoku_html_ga6503128d4f5ce0a0826f72f73f9e0b2a"><div class="ttname"><a href="../../d5/df4/group__sudoku.html#ga6503128d4f5ce0a0826f72f73f9e0b2a">OKcol</a></div><div class="ttdeci">bool OKcol(const struct sudoku *a, int x, int y, int v)</div><div class="ttdoc">Check if y^th column is valid.</div><div class="ttdef"><b>Definition:</b> sudoku_solver.c:67</div></div>
<div class="ttc" id="agroup__sudoku_html_ga1cd43df3f4187845ce186042fe53e6f1"><div class="ttname"><a href="../../d5/df4/group__sudoku.html#ga1cd43df3f4187845ce186042fe53e6f1">OKbox</a></div><div class="ttdeci">bool OKbox(const struct sudoku *a, int x, int y, int v)</div><div class="ttdoc">Check if a 3x3 box is valid.</div><div class="ttdef"><b>Definition:</b> sudoku_solver.c:85</div></div>
<div class="ttc" id="agroup__sudoku_html_ga3a0ec150ac9d0cb6b28279d36d95d72f"><div class="ttname"><a href="../../d5/df4/group__sudoku.html#ga3a0ec150ac9d0cb6b28279d36d95d72f">OK</a></div><div class="ttdeci">bool OK(const struct sudoku *a, int x, int y, int v)</div><div class="ttdoc">Check if element v is valid to place at (x,y) location.</div><div class="ttdef"><b>Definition:</b> sudoku_solver.c:111</div></div>
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="footer">Generated by <a href="http://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.8.20 </li>
</ul>
</div>
</body>
</html>