TheAlgorithms-C/d5/df4/group__sudoku.html
2023-01-20 21:44:50 +00:00

594 lines
37 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" 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: 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 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('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">
<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="#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. <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. <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. <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. <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. <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. <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. <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;&#160;</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"><span class="lineno"> 145</span>{</div>
<div class="line"><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"><span class="lineno"> 147</span> {</div>
<div class="line"><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"><span class="lineno"> 149</span> {</div>
<div class="line"><span class="lineno"> 150</span> <span class="keywordflow">if</span> (a-&gt;a[i * a-&gt;N + j] == 0)</div>
<div class="line"><span class="lineno"> 151</span> {</div>
<div class="line"><span class="lineno"> 152</span> *x = i;</div>
<div class="line"><span class="lineno"> 153</span> *y = j;</div>
<div class="line"><span class="lineno"> 154</span> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><span class="lineno"> 155</span> }</div>
<div class="line"><span class="lineno"> 156</span> }</div>
<div class="line"><span class="lineno"> 157</span> }</div>
<div class="line"><span class="lineno"> 158</span> </div>
<div class="line"><span class="lineno"> 159</span> <span class="comment">/* no unknown locations found */</span></div>
<div class="line"><span class="lineno"> 160</span> <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"><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;&#160;</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"><span class="lineno"> 112</span>{</div>
<div class="line"><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"><span class="lineno"> 114</span> <span class="keywordflow">if</span> (result)</div>
<div class="line"><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"><span class="lineno"> 116</span> <span class="keywordflow">if</span> (result)</div>
<div class="line"><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"><span class="lineno"> 118</span> </div>
<div class="line"><span class="lineno"> 119</span> <span class="keywordflow">return</span> result;</div>
<div class="line"><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="135"><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;&#160;</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"><span class="lineno"> 86</span>{</div>
<div class="line"><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"><span class="lineno"> 88</span><span class="comment"> remember that in C/C++, division operation always rounds towards</span></div>
<div class="line"><span class="lineno"> 89</span><span class="comment"> -infinity for signed integers and towards 0 for unsigned integers</span></div>
<div class="line"><span class="lineno"> 90</span><span class="comment"> */</span></div>
<div class="line"><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"><span class="lineno"> 92</span> <span class="comment">// printf(&quot;Checking box: (%d,%d)\n&quot;, bi, bj);</span></div>
<div class="line"><span class="lineno"> 93</span> </div>
<div class="line"><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"><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"><span class="lineno"> 96</span> <span class="keywordflow">if</span> (a-&gt;a[i * a-&gt;N + j] == v)</div>
<div class="line"><span class="lineno"> 97</span> <span class="comment">// if the value is found in the box</span></div>
<div class="line"><span class="lineno"> 98</span> <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"><span class="lineno"> 99</span> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><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;&#160;</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"><span class="lineno"> 68</span>{</div>
<div class="line"><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"><span class="lineno"> 70</span> <span class="keywordflow">if</span> (a-&gt;a[i * a-&gt;N + y] == v)</div>
<div class="line"><span class="lineno"> 71</span> <span class="comment">// if the value is found in the column</span></div>
<div class="line"><span class="lineno"> 72</span> <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"><span class="lineno"> 73</span> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><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;&#160;</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"><span class="lineno"> 49</span>{</div>
<div class="line"><span class="lineno"> 50</span> <span class="keywordtype">int</span> offset = x * a-&gt;N;</div>
<div class="line"><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"><span class="lineno"> 52</span> <span class="keywordflow">if</span> (a-&gt;a[offset + j] == v)</div>
<div class="line"><span class="lineno"> 53</span> <span class="comment">// if the value is found in the row</span></div>
<div class="line"><span class="lineno"> 54</span> <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"><span class="lineno"> 55</span> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><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;&#160;</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"><span class="lineno"> 127</span>{</div>
<div class="line"><span class="lineno"> 128</span> <span class="keywordtype">int</span> i, j;</div>
<div class="line"><span class="lineno"> 129</span> <span class="keywordflow">for</span> (i = 0; i &lt; a-&gt;N; i++)</div>
<div class="line"><span class="lineno"> 130</span> <span class="keywordflow">for</span> (j = 0; j &lt; a-&gt;N; j++)</div>
<div class="line"><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"><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"><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;&#160;</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"><span class="lineno"> 173</span>{</div>
<div class="line"><span class="lineno"> 174</span> <span class="keyword">static</span> uint32_t counter = 0;</div>
<div class="line"><span class="lineno"> 175</span> <span class="keywordtype">int</span> i, j;</div>
<div class="line"><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"><span class="lineno"> 177</span> </div>
<div class="line"><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"><span class="lineno"> 179</span> {</div>
<div class="line"><span class="lineno"> 180</span> <span class="comment">/* no more empty location found</span></div>
<div class="line"><span class="lineno"> 181</span><span class="comment"> implies all good in the matrix</span></div>
<div class="line"><span class="lineno"> 182</span><span class="comment"> */</span></div>
<div class="line"><span class="lineno"> 183</span> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><span class="lineno"> 184</span> }</div>
<div class="line"><span class="lineno"> 185</span> </div>
<div class="line"><span class="lineno"> 186</span> <span class="comment">/* try all possible values for the unknown */</span></div>
<div class="line"><span class="lineno"> 187</span> <span class="keywordflow">for</span> (uint8_t v = 1; v &lt;= a-&gt;N; v++)</div>
<div class="line"><span class="lineno"> 188</span> { <span class="comment">/* try all possible values 1 thru N */</span></div>
<div class="line"><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"><span class="lineno"> 190</span> counter++;</div>
<div class="line"><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"><span class="lineno"> 192</span> {</div>
<div class="line"><span class="lineno"> 193</span> <span class="comment">/* if assignment checks satisfy, set the value and</span></div>
<div class="line"><span class="lineno"> 194</span><span class="comment"> continue with remaining elements */</span></div>
<div class="line"><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"><span class="lineno"> 196</span> a-&gt;a[i * a-&gt;N + j] = v;</div>
<div class="line"><span class="lineno"> 197</span> strcat(prefix, <span class="stringliteral">&quot; &quot;</span>);</div>
<div class="line"><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"><span class="lineno"> 199</span> {</div>
<div class="line"><span class="lineno"> 200</span> <span class="comment">/* solution found */</span></div>
<div class="line"><span class="lineno"> 201</span> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><span class="lineno"> 202</span> }</div>
<div class="line"><span class="lineno"> 203</span> </div>
<div class="line"><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"><span class="lineno"> 205</span> prefix, i, j, a-&gt;a[i * a-&gt;N + j], counter);</div>
<div class="line"><span class="lineno"> 206</span> </div>
<div class="line"><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"><span class="lineno"> 208</span> a-&gt;a[i * a-&gt;N + j] = 0;</div>
<div class="line"><span class="lineno"> 209</span> }</div>
<div class="line"><span class="lineno"> 210</span> <span class="keywordflow">else</span></div>
<div class="line"><span class="lineno"> 211</span> {</div>
<div class="line"><span class="lineno"> 212</span> printf(<span class="stringliteral">&quot;\r&quot;</span>);</div>
<div class="line"><span class="lineno"> 213</span> }</div>
<div class="line"><span class="lineno"> 214</span> }</div>
<div class="line"><span class="lineno"> 215</span> </div>
<div class="line"><span class="lineno"> 216</span> <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"><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="354" height="135"><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.6 </li>
</ul>
</div>
</body>
</html>