Need help with 2D arrays in Go

agolangf · · 421 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I need to make sudoku solver using go-routines but every go routine needs to be a single row or column (so 18 go routines total). The problem I ecountered is that I don&#39;t know how to extract column from a 2D array...is it even possible in Go. Here is a test code for printing out row but I have no clue how to print out entire column:</p> <pre><code>package main import ( &#34;fmt&#34; ) var puzzle = [9][9]int{{5, 3, 0, 0, 7, 0, 0, 0, 0}, {6, 0, 0, 1, 9, 5, 0, 0, 0}, {0, 9, 8, 0, 0, 0, 0, 6, 0}, {8, 0, 0, 0, 6, 0, 0, 0, 3}, {4, 0, 0, 8, 0, 3, 0, 0, 1}, {7, 0, 0, 0, 2, 0, 0, 0, 6}, {0, 6, 0, 0, 0, 0, 2, 8, 0}, {0, 0, 0, 4, 1, 9, 0, 0, 5}, {0, 0, 0, 0, 8, 0, 0, 7, 9}} func main() { rows(puzzle) fmt.Println(&#34;=======================&#34;) columns(puzzle) } func rows(board [9][9]int) { for i:=0; i&lt;=9; i++ { fmt.Printf(&#34;%v\n&#34;, board[:i]) } } func columns(board [9][9]int) { for i := 0; i&lt;=9; i++ { for j := 0; j &lt; 9; j++ { fmt.Printf(&#34;%v\n&#34;, board[j][i]) } } } </code></pre> <p>Is there a way that I can print out columns just like rows, only to take second array and print it out, or I need to make a new array and append values in it or is there an even better way to do it. Thanks.</p> <hr/>**评论:**<br/><br/>silviucm: <pre><p>Consider transposing the matrix. This way, your columns become rows, and you can easily print. Examples here: <a href="https://rosettacode.org/wiki/Matrix_transposition#Go" rel="nofollow">https://rosettacode.org/wiki/Matrix_transposition#Go</a></p></pre>Mal3On: <pre><p>Thank you so much, will try that. Thank you all &lt;3</p></pre>deusmetallum: <pre><p>You&#39;d have to iterate over everything to get the columns.</p> <p>Essentially, the first column would be [a[0], b[0], c[0],...,i[0]], col 2 would be [a[1], b[1],...,i[1]], etc.</p></pre>Mal3On: <pre><p><em>function rows</em> prints out [[5 3 0 0 7 0 0 0 0]] after second iteration, but <em>function columns</em> prints out after second iteration 6, after third 0 and so on (first element of each row bassicaly). If I put go routine there instead of a print function I would get 81 go routines (9*9) and I don&#39;t want that. So there is no other way of making columns but to iterate over everything? I am pretty new to Go so I don&#39;t have the best knowledge of it. Non the less, thank you for helping.</p></pre>Morgahl: <pre><p>To write a solver you&#39;ll also need to spin up a goroutine for each 3x3 box as well as that is also a constraint that must be resolved.</p> <p>My semi monthly coding kata usually ends up being a sudoku solver of some variation. It&#39;s a fun way to explore different ways to do the same things.</p></pre>Mal3On: <pre><p>I will be doing that as well, but at the moment I am stuck how to make go-routine for each column (this print statement will be replaced with go-routines), because if I have to iterate over everything, that will make go-routine for each square, not column or row</p></pre>epiris: <pre><p>Make sure to pass those arrays by reference too or you will be copying 9x9 every iteration. If you don&#39;t end up using a struct matrix as suggested already.</p></pre>throwawayguy123xd: <pre><p><a href="https://play.golang.org/p/bZBPp-os2o" rel="nofollow">https://play.golang.org/p/bZBPp-os2o</a></p> <p>small intro to 2-d arrays</p></pre>Mal3On: <pre><p>Great, this will help. Thank you!</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

421 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传