# Finding all possible solutions of a japanese sums puzzle

I'm trying to think of an algorithm that gives out all possible solutions to a japanese sums puzzle. It's not an a college assignment or anything, I'm giving puzzles to my cousin for fun, but want to be ready with all solutions in an easy way, and I got stuck at this interesting hurdle.

So Imagine a 3x3 grid. You're given the total sum of all rows and all columns. All numbers in the grid can be any number between 1 to 9, and numbers could repeat. For example, suppose you are supposed to find all possible grids which satisfy the following constraint:

(SR1,SR2,SR3,SC1,SC2,SC3) = (4,5,4,4,5,4) Let's define an array = (0,0,0,0,0,0), which denotes the current sum

I came up with a solution to this but I'm unable to figure out how would you program it. So one way to solve this, would be to assign 1 to every element at the beginning, and next time you add a number to any element, you update the above array. So right now the array would be (3,3,3,3,3,3).

So you need to go from (3,3,3,3,3,3) to (4,5,4,4,5,4), and there are multiple ways to arrive at the latter array from the former array, and hence multiple final grids. I'm only able to think of brute force ways of coming up with all solutions. Is there an elegant solution/algorithm to this?

• This questions seems more apt for something like PPCG or SO, as it's more an algorithmic question rather than a puzzle – Wen1now Dec 14 '17 at 7:24
• Can I move the question? – qwerty_uiop Dec 14 '17 at 7:33
• If you're looking for practicality, then brute force really is the way to go. It's only a 3x3 grid containing digits 1-9, and you'll be able to easily detect invalid configurations and prune really early into each configuration. Brute force is definitely the most feasible answer for a problem of this scope. – votbear Dec 14 '17 at 7:33
• @qwerty_uiop meta.stackexchange.com/questions/64068/… <- read that, I think it has the protocols for cross-site stuff – Wen1now Dec 14 '17 at 7:35
• @VotBear But I want to generalize it such that the algorithm is scalable for any grid dimension – qwerty_uiop Dec 14 '17 at 7:35
{"ft_lang_label":"__label__en","ft_lang_prob":0.9514623,"math_prob":0.90285975,"size":1251,"snap":"2020-10-2020-16","text_gpt3_token_len":341,"char_repetition_ratio":0.114675224,"word_repetition_ratio":0.0,"special_character_ratio":0.2733813,"punctuation_ratio":0.17152104,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9673122,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-02-27T01:14:42Z\",\"WARC-Record-ID\":\"<urn:uuid:42a91459-d393-4565-a68c-fade6eced4de>\",\"Content-Length\":\"139799\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d9568502-a296-400c-96d5-9dccb2500e87>\",\"WARC-Concurrent-To\":\"<urn:uuid:14f78a18-f414-4f85-8916-e855a36dae9d>\",\"WARC-IP-Address\":\"151.101.193.69\",\"WARC-Target-URI\":\"https://puzzling.stackexchange.com/questions/57935/finding-all-possible-solutions-of-a-japanese-sums-puzzle\",\"WARC-Payload-Digest\":\"sha1:V47ATIAFVHK2P4CQTI7BTRTOCQSXHJQU\",\"WARC-Block-Digest\":\"sha1:5V35DABKSOUOHVCK3PNKFXBNHCS3BM4R\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-10/CC-MAIN-2020-10_segments_1581875146643.49_warc_CC-MAIN-20200227002351-20200227032351-00374.warc.gz\"}"}