强力幻方 [英] Brute force magic squares

查看:164
本文介绍了强力幻方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我有一个3×3网格中填充有两位数字00 - 99。一些号码被作为输入的其余部分是未知的。什么是如何解决与蛮力这样的问题在C中的一些建议?

Basically I have a 3 x 3 grid that is filled with two digit numbers 00 - 99. Some of the numbers are given as input the rest are unknown. What are some suggestions on how to solve such a problem with brute force in C?

编辑:对不起,我忘了问题的一部分。每行和列和对角线上必须添加到相​​同的数量。我不希望任何code只是一些想法,开始使用算法

Sorry I forgot part of the problem. Every row and column and diagonal must add up to the same number. I don't want any code just some ideas to get started with algorithm

推荐答案

有一个简单的递归解决您的问题,这是一种强力称为回溯的例子(谷歌的)。

There is a simple recursive solution to your problem, which is an example of a type of brute force called backtracking (google that).

递归功能(比如,fill_next)发现未知值的下一个单元格。如果不存在这样的小区,它检查正方形,看它是否符合要求(的总和是正确的),并且如果如此打印的平方作为溶液;它然后返回。如果有一个未知值的单元格,它循环,依次为每个单元格的值的0试图99则自称递归地填写下一个未知的细胞。

The recursive function (say, fill_next) finds the next cell with unknown value. If there is no such cell, it checks the square to see if it fits the requirements (the sums are correct) and if so prints the square as a solution; it then returns. If there is a cell with an unknown value, it loops, trying each of the values 0 to 99 in turn for that cell then calling itself recursively to fill in the next unknown cell.

如何获得与未知值下一个单元格:你可以简单地传递给find_next下一个单元格开始寻找数;你会通过调用fill_next(0)开始,整个事情了。细胞数为0到8,因为你有9个细胞。如果要存储的方形二维数组中,只使用NUM%3和num / 3作为指标。

How to get to the next cell with unknown value: you can simply pass to find_next the number of the next cell to start looking at; you would start the whole thing off by calling fill_next(0). The cell number is 0 through 8 since you have 9 cells. If you are storing the square in a 2D array, just use num%3 and num/3 as the indices.

这会容易得多,只需给你$ C $的几行C时需来形容,但你说你不希望出现这种情况。

This would be much easier to describe by just giving you the few lines of code it takes, but you said you don't want that.

这篇关于强力幻方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆