TheAlgorithms-C/d5/df4/group__sudoku.html

587 lines
41 KiB
HTML
Raw Normal View History

<!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=11"/>
<meta name="generator" content="Doxygen 1.9.2"/>
<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<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.2 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search",'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('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 id="nested-classes" 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 id="func-members" 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" name="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 id="l00145" name="l00145"></a><span class="lineno"> 145</span>{</div>
<div class="line"><a id="l00146" name="l00146"></a><span class="lineno"> 146</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; a-&gt;N; i++)</div>
<div class="line"><a id="l00147" name="l00147"></a><span class="lineno"> 147</span> {</div>
<div class="line"><a id="l00148" name="l00148"></a><span class="lineno"> 148</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j &lt; a-&gt;N; j++)</div>
<div class="line"><a id="l00149" name="l00149"></a><span class="lineno"> 149</span> {</div>
<div class="line"><a id="l00150" name="l00150"></a><span class="lineno"> 150</span> <span class="keywordflow">if</span> (a-&gt;a[i * a-&gt;N + j] == 0)</div>
<div class="line"><a id="l00151" name="l00151"></a><span class="lineno"> 151</span> {</div>
<div class="line"><a id="l00152" name="l00152"></a><span class="lineno"> 152</span> *x = i;</div>
<div class="line"><a id="l00153" name="l00153"></a><span class="lineno"> 153</span> *y = j;</div>
<div class="line"><a id="l00154" name="l00154"></a><span class="lineno"> 154</span> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><a id="l00155" name="l00155"></a><span class="lineno"> 155</span> }</div>
<div class="line"><a id="l00156" name="l00156"></a><span class="lineno"> 156</span> }</div>
<div class="line"><a id="l00157" name="l00157"></a><span class="lineno"> 157</span> }</div>
<div class="line"><a id="l00158" name="l00158"></a><span class="lineno"> 158</span> </div>
<div class="line"><a id="l00159" name="l00159"></a><span class="lineno"> 159</span> <span class="comment">/* no unknown locations found */</span></div>
<div class="line"><a id="l00160" name="l00160"></a><span class="lineno"> 160</span> <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"><a id="l00161" name="l00161"></a><span class="lineno"> 161</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="ga3a0ec150ac9d0cb6b28279d36d95d72f" name="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 id="l00112" name="l00112"></a><span class="lineno"> 112</span>{</div>
<div class="line"><a id="l00113" name="l00113"></a><span class="lineno"> 113</span> <span class="keywordtype">bool</span> result = <a class="code hl_function" href="../../d5/df4/group__sudoku.html#ga85d25d3b40017436f264a103685e4c55">OKrow</a>(a, x, y, v);</div>
<div class="line"><a id="l00114" name="l00114"></a><span class="lineno"> 114</span> <span class="keywordflow">if</span> (result)</div>
<div class="line"><a id="l00115" name="l00115"></a><span class="lineno"> 115</span> result = <a class="code hl_function" href="../../d5/df4/group__sudoku.html#ga6503128d4f5ce0a0826f72f73f9e0b2a">OKcol</a>(a, x, y, v);</div>
<div class="line"><a id="l00116" name="l00116"></a><span class="lineno"> 116</span> <span class="keywordflow">if</span> (result)</div>
<div class="line"><a id="l00117" name="l00117"></a><span class="lineno"> 117</span> result = <a class="code hl_function" href="../../d5/df4/group__sudoku.html#ga1cd43df3f4187845ce186042fe53e6f1">OKbox</a>(a, x, y, v);</div>
<div class="line"><a id="l00118" name="l00118"></a><span class="lineno"> 118</span> </div>
<div class="line"><a id="l00119" name="l00119"></a><span class="lineno"> 119</span> <span class="keywordflow">return</span> result;</div>
<div class="line"><a id="l00120" name="l00120"></a><span class="lineno"> 120</span>}</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_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_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><!-- 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" name="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 id="l00086" name="l00086"></a><span class="lineno"> 86</span>{</div>
<div class="line"><a id="l00087" name="l00087"></a><span class="lineno"> 87</span> <span class="comment">/* get start indices of the box that the current (x,y) lies in</span></div>
<div class="line"><a id="l00088" name="l00088"></a><span class="lineno"> 88</span><span class="comment"> remember that in C/C++, division operation always rounds towards</span></div>
<div class="line"><a id="l00089" name="l00089"></a><span class="lineno"> 89</span><span class="comment"> -infinity for signed integers and towards 0 for unsigned integers</span></div>
<div class="line"><a id="l00090" name="l00090"></a><span class="lineno"> 90</span><span class="comment"> */</span></div>
<div class="line"><a id="l00091" name="l00091"></a><span class="lineno"> 91</span> <span class="keywordtype">int</span> bi = x - x % a-&gt;N2, bj = y - y % a-&gt;N2;</div>
<div class="line"><a id="l00092" name="l00092"></a><span class="lineno"> 92</span> <span class="comment">// printf(&quot;Checking box: (%d,%d)\n&quot;, bi, bj);</span></div>
<div class="line"><a id="l00093" name="l00093"></a><span class="lineno"> 93</span> </div>
<div class="line"><a id="l00094" name="l00094"></a><span class="lineno"> 94</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = bi; i &lt; (bi + a-&gt;N2); i++)</div>
<div class="line"><a id="l00095" name="l00095"></a><span class="lineno"> 95</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = bj; j &lt; (bj + a-&gt;N2); j++)</div>
<div class="line"><a id="l00096" name="l00096"></a><span class="lineno"> 96</span> <span class="keywordflow">if</span> (a-&gt;a[i * a-&gt;N + j] == v)</div>
<div class="line"><a id="l00097" name="l00097"></a><span class="lineno"> 97</span> <span class="comment">// if the value is found in the box</span></div>
<div class="line"><a id="l00098" name="l00098"></a><span class="lineno"> 98</span> <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"><a id="l00099" name="l00099"></a><span class="lineno"> 99</span> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><a id="l00100" name="l00100"></a><span class="lineno"> 100</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="ga6503128d4f5ce0a0826f72f73f9e0b2a" name="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 id="l00068" name="l00068"></a><span class="lineno"> 68</span>{</div>
<div class="line"><a id="l00069" name="l00069"></a><span class="lineno"> 69</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; a-&gt;N; i++)</div>
<div class="line"><a id="l00070" name="l00070"></a><span class="lineno"> 70</span> <span class="keywordflow">if</span> (a-&gt;a[i * a-&gt;N + y] == v)</div>
<div class="line"><a id="l00071" name="l00071"></a><span class="lineno"> 71</span> <span class="comment">// if the value is found in the column</span></div>
<div class="line"><a id="l00072" name="l00072"></a><span class="lineno"> 72</span> <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"><a id="l00073" name="l00073"></a><span class="lineno"> 73</span> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><a id="l00074" name="l00074"></a><span class="lineno"> 74</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="ga85d25d3b40017436f264a103685e4c55" name="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 id="l00049" name="l00049"></a><span class="lineno"> 49</span>{</div>
<div class="line"><a id="l00050" name="l00050"></a><span class="lineno"> 50</span> <span class="keywordtype">int</span> offset = x * a-&gt;N;</div>
<div class="line"><a id="l00051" name="l00051"></a><span class="lineno"> 51</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j &lt; a-&gt;N; j++)</div>
<div class="line"><a id="l00052" name="l00052"></a><span class="lineno"> 52</span> <span class="keywordflow">if</span> (a-&gt;a[offset + j] == v)</div>
<div class="line"><a id="l00053" name="l00053"></a><span class="lineno"> 53</span> <span class="comment">// if the value is found in the row</span></div>
<div class="line"><a id="l00054" name="l00054"></a><span class="lineno"> 54</span> <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"><a id="l00055" name="l00055"></a><span class="lineno"> 55</span> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><a id="l00056" name="l00056"></a><span class="lineno"> 56</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="ga702ff4f95dde780c7d04fcdd1021b6c1" name="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 id="l00127" name="l00127"></a><span class="lineno"> 127</span>{</div>
<div class="line"><a id="l00128" name="l00128"></a><span class="lineno"> 128</span> <span class="keywordtype">int</span> i, j;</div>
<div class="line"><a id="l00129" name="l00129"></a><span class="lineno"> 129</span> <span class="keywordflow">for</span> (i = 0; i &lt; a-&gt;N; i++)</div>
<div class="line"><a id="l00130" name="l00130"></a><span class="lineno"> 130</span> <span class="keywordflow">for</span> (j = 0; j &lt; a-&gt;N; j++)</div>
<div class="line"><a id="l00131" name="l00131"></a><span class="lineno"> 131</span> 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 id="l00132" name="l00132"></a><span class="lineno"> 132</span> (j == a-&gt;N - 1 ? <span class="charliteral">&#39;\n&#39;</span> : <span class="charliteral">&#39; &#39;</span>));</div>
<div class="line"><a id="l00133" name="l00133"></a><span class="lineno"> 133</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="gadfe0ed5085b4775d8fa00b434cc0fdfc" name="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 id="l00173" name="l00173"></a><span class="lineno"> 173</span>{</div>
<div class="line"><a id="l00174" name="l00174"></a><span class="lineno"> 174</span> <span class="keyword">static</span> uint32_t counter = 0;</div>
<div class="line"><a id="l00175" name="l00175"></a><span class="lineno"> 175</span> <span class="keywordtype">int</span> i, j;</div>
<div class="line"><a id="l00176" name="l00176"></a><span class="lineno"> 176</span> <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 id="l00177" name="l00177"></a><span class="lineno"> 177</span> </div>
<div class="line"><a id="l00178" name="l00178"></a><span class="lineno"> 178</span> <span class="keywordflow">if</span> (!<a class="code hl_function" href="../../d5/df4/group__sudoku.html#ga62e94fc39f116e2c81daed8f5437431b">get_next_unknown</a>(a, &amp;i, &amp;j))</div>
<div class="line"><a id="l00179" name="l00179"></a><span class="lineno"> 179</span> {</div>
<div class="line"><a id="l00180" name="l00180"></a><span class="lineno"> 180</span> <span class="comment">/* no more empty location found</span></div>
<div class="line"><a id="l00181" name="l00181"></a><span class="lineno"> 181</span><span class="comment"> implies all good in the matrix</span></div>
<div class="line"><a id="l00182" name="l00182"></a><span class="lineno"> 182</span><span class="comment"> */</span></div>
<div class="line"><a id="l00183" name="l00183"></a><span class="lineno"> 183</span> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><a id="l00184" name="l00184"></a><span class="lineno"> 184</span> }</div>
<div class="line"><a id="l00185" name="l00185"></a><span class="lineno"> 185</span> </div>
<div class="line"><a id="l00186" name="l00186"></a><span class="lineno"> 186</span> <span class="comment">/* try all possible values for the unknown */</span></div>
<div class="line"><a id="l00187" name="l00187"></a><span class="lineno"> 187</span> <span class="keywordflow">for</span> (uint8_t v = 1; v &lt;= a-&gt;N; v++)</div>
<div class="line"><a id="l00188" name="l00188"></a><span class="lineno"> 188</span> { <span class="comment">/* try all possible values 1 thru N */</span></div>
<div class="line"><a id="l00189" name="l00189"></a><span class="lineno"> 189</span> 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 id="l00190" name="l00190"></a><span class="lineno"> 190</span> counter++;</div>
<div class="line"><a id="l00191" name="l00191"></a><span class="lineno"> 191</span> <span class="keywordflow">if</span> (<a class="code hl_function" href="../../d5/df4/group__sudoku.html#ga3a0ec150ac9d0cb6b28279d36d95d72f">OK</a>(a, i, j, v))</div>
<div class="line"><a id="l00192" name="l00192"></a><span class="lineno"> 192</span> {</div>
<div class="line"><a id="l00193" name="l00193"></a><span class="lineno"> 193</span> <span class="comment">/* if assignment checks satisfy, set the value and</span></div>
<div class="line"><a id="l00194" name="l00194"></a><span class="lineno"> 194</span><span class="comment"> continue with remaining elements */</span></div>
<div class="line"><a id="l00195" name="l00195"></a><span class="lineno"> 195</span> printf(<span class="stringliteral">&quot;passed (counter=%&quot;</span> SCNu32 <span class="stringliteral">&quot;)\n&quot;</span>, counter);</div>
<div class="line"><a id="l00196" name="l00196"></a><span class="lineno"> 196</span> a-&gt;a[i * a-&gt;N + j] = v;</div>
<div class="line"><a id="l00197" name="l00197"></a><span class="lineno"> 197</span> strcat(prefix, <span class="stringliteral">&quot; &quot;</span>);</div>
<div class="line"><a id="l00198" name="l00198"></a><span class="lineno"> 198</span> <span class="keywordflow">if</span> (<a class="code hl_function" href="../../d5/df4/group__sudoku.html#gadfe0ed5085b4775d8fa00b434cc0fdfc">solve</a>(a))</div>
<div class="line"><a id="l00199" name="l00199"></a><span class="lineno"> 199</span> {</div>
<div class="line"><a id="l00200" name="l00200"></a><span class="lineno"> 200</span> <span class="comment">/* solution found */</span></div>
<div class="line"><a id="l00201" name="l00201"></a><span class="lineno"> 201</span> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><a id="l00202" name="l00202"></a><span class="lineno"> 202</span> }</div>
<div class="line"><a id="l00203" name="l00203"></a><span class="lineno"> 203</span> </div>
<div class="line"><a id="l00204" name="l00204"></a><span class="lineno"> 204</span> 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 id="l00205" name="l00205"></a><span class="lineno"> 205</span> prefix, i, j, a-&gt;a[i * a-&gt;N + j], counter);</div>
<div class="line"><a id="l00206" name="l00206"></a><span class="lineno"> 206</span> </div>
<div class="line"><a id="l00207" name="l00207"></a><span class="lineno"> 207</span> prefix[strlen(prefix) - 2] = <span class="charliteral">&#39;\0&#39;</span>; <span class="comment">// truncate the prefix</span></div>
<div class="line"><a id="l00208" name="l00208"></a><span class="lineno"> 208</span> a-&gt;a[i * a-&gt;N + j] = 0;</div>
<div class="line"><a id="l00209" name="l00209"></a><span class="lineno"> 209</span> }</div>
<div class="line"><a id="l00210" name="l00210"></a><span class="lineno"> 210</span> <span class="keywordflow">else</span></div>
<div class="line"><a id="l00211" name="l00211"></a><span class="lineno"> 211</span> {</div>
<div class="line"><a id="l00212" name="l00212"></a><span class="lineno"> 212</span> printf(<span class="stringliteral">&quot;\r&quot;</span>);</div>
<div class="line"><a id="l00213" name="l00213"></a><span class="lineno"> 213</span> }</div>
<div class="line"><a id="l00214" name="l00214"></a><span class="lineno"> 214</span> }</div>
<div class="line"><a id="l00215" name="l00215"></a><span class="lineno"> 215</span> </div>
<div class="line"><a id="l00216" name="l00216"></a><span class="lineno"> 216</span> <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"><a id="l00217" name="l00217"></a><span class="lineno"> 217</span>}</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>
<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><!-- 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="356" 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 -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<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.2 </li>
</ul>
</div>
</body>
</html>