Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

I need help with a Sudoku project  RSS feed

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys! I Have a new task which im struggling with, so if you can help me, feel free to comment this post.
For Sudoku size 25, the same rules apply to standard Sudoku size 9: we enter numbers from 1 to 25 in the fields so that no row and no column have duplicate numbers. In addition, it must also be noted that the numbers are not repeated in any of the 25 smaller squares of 5 x 5 ( see picture below).

The Sudoku Puzzle solution is written in a text file, each line of 25 numbers in its line in the file (25 lines in the file). The numbers in the row are separated by one or more spaces. The example puzzle file is sudoku1.txt which has this shape:

 8 20 13 24 19  3 12  7 25 21 16  5  6 17  2  9 18 11 10  4 22  1 15 23 14
 9 22 21 10 17  4 13  5 24 18 12 20 19 11 15 14  1 16 25 23  6  7  8  2  3
 2 23  5  7 12  6 11  8 17 22  9 14 10  4  1 21 15 19 13  3 20 18 25 16 24
 3 25 15  1 18  2 14  9 16 20  8 23  7 22 13 12  6 17  5 24  4 21 11 19 10
14  4 11  6 16 10 15  1 23 19 25  3 21 24 18  7 20  2 22  8  5 17  9 13 12
12  5 24 17 15 13  1 16  3  9  6 11 25  8 22  4 23 14  7  2 18 19 10 20 21
 6 16 25 22 23 14 19 15 10  4 21 24 18 12 20  1  3 13 17 11  7  8  5  9  2
10 18 19 21 11 12 17 25  2  7 13  1 23 15  5 20  8 24  9  6  3 14 22  4 16
20  9  7 13  8 22 21 23  6 24  3  4 14  2 16 10 25  5 18 19 15 12 17 11  1
 1  2  3 14  4  5  8 18 20 11 10 19  9  7 17 15 16 12 21 22 24 25 23  6 13
 7 13 22  9 20 11  5 21  4  3 18  2 12 14 10 19 24  8 16 25 23  6  1 15 17
21  3 10  2  1 17 25 24  7 15  5 16 13 23  8 22  4 20  6 12  9 11 18 14 19
18 12 14 15  5 19  6 22  8 23 11 17 20 21  4 13  2  3  1  9 16 24  7 10 25
19 17  6 23 24 16 10 13  1 12  7 15  3 25  9 18  5 21 11 14  2 20  4 22  8
11  8 16  4 25 20 18  2  9 14 19 22 24  1  6 17 10 15 23  7 21  3 13 12  5
16 15 18 19 22 21  4 17 11  5 14 13  8  6 24 25 12 23  3 10  1  2 20  7  9
23 24  4 25 21 18  9 14 22  8 17 12 15 20  7 11 19  1  2 13 10 16  3  5  6
17 11  9  3  6 25 16 20 12  1  2 18 22 10 23  8 21  7 15  5 19 13 14 24  4
 5 10 20  8  2 23  7  3 13  6  1  9 16 19 21 24 22  4 14 17 25 15 12 18 11
13 14  1 12  7 24  2 19 15 10  4 25 11  5  3  6  9 18 20 16  8 23 21 17 22
24  6 12 18 13  1 23  4 19 16 22  7  5  9 14  3 17 25  8 20 11 10  2 21 15
22 19 23 20 14  8  3 12  5  2 24 21  1 18 11 16 13 10  4 15 17  9  6 25  7
25 21  2 16 10  9 20 11 14 17 15  6  4  3 12 23  7 22 24  1 13  5 19  8 18
 4  7  8  5  3 15 24  6 18 25 20 10 17 13 19  2 11  9 12 21 14 22 16  1 23
15  1 17 11  9  7 22 10 21 13 23  8  2 16 25  5 14  6 19 18 12  4 24  3 20

Write the following methods:

The method readSolutions()reads the solution of the puzzle from the file and stores the data in the table and returns this table with numbers. The name of the solution file is specified as the method parameter.
The method checkSudoku()as a parameter receives a table in which the Sudoku solution is written, and verifies that the solution is correct (returns true). In case of an incorrect solution, the method returns false.
sudoku.png
[Thumbnail for sudoku.png]
 
Rancher
Posts: 3127
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Pedrag,

what part is giving you some problems, and what are these problems?
 
Sheriff
Posts: 5813
150
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, Predrag Djindjic.

Please be sure to write a descriptive subject line when you start a thread.  I've edited it for you.

What have you done so far?  If you've written any code, please post it here and be sure to UseCodeTags (that's a link).

If you need help getting started, try writing down what you would need to do as a human (not a computer).  Admittedly, this is a little hard with with "read a file from the disc" but how about this for the first task:

* make a connection to the disc file
* read from that connection

Then you need to consider these "programmy" questions

* need a data structure to hold a table. What would that be in Java?
* need a method block.  What does it take as a parameter(s) and what does it return.

Then do this for the second method.  If you need more help than that, post what you've done so far and someone will help you.
 
Piet Souris
Rancher
Posts: 3127
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excuses, should be Predrag.
 
Predrag Djindjic
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've done the first part, which is making a table out of given data.Now i have table in this shape:
{8,3,4,4}
{3,2,4,1}
{2,3,5,6}
And now the second part is to check if the given data is true(by sudoku standard rules).The sudoku is 25x25 and i have to check all rows,columns and little 5x5 blocks for mistakes.I need help with that 5x5 little blocks in 25x25 sudoku.Check the picture above if you dont get what im saying.
Thanks for quick replies!
 
Knute Snortum
Sheriff
Posts: 5813
150
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know the size of the Sudoku block?  How would you determine a "subblock" from an arbitrary size?

If it's fixed as 25 x 25, than you already know how to divide the larger block into sub blocks.  Than the question is, Do I write the same code 25 times?  That's both tedious and violates the DRY principle (don't repeat yourself).  Could you create a method that could help you?
 
Predrag Djindjic
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you can see in the picture above, the sudoku is 25x25 fixed and the subblocks are 5x5 as in all the Sudoku tasks ever if you ever played it.Now i have to make a method to check if any number is repeating itself in it's block and im struggling with that.I found some solutions online but they are not helpful because all of them require the exact number i need to chect that for in parametac, but I have to check that for every number in given Sudoku list.If you can help me in any way,write it please.Thank you!
 
Piet Souris
Rancher
Posts: 3127
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several ways. For instance, have an int[26] array. When you have a row, column or block, go through each element of it. If you find a number i, then increase array[i] by one. In the end, array[1]..array[25]  should all contain a 1. Or determine minimum and maximum of a row/column/block, it should be 1 and 25, and the sum of the elements should be 325.
 
Piet Souris
Rancher
Posts: 3127
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote: (...) . Or determine minimum and maximum of a row/column/block, it should be 1 and 25, and the sum of the elements should be 325.


Oops, that is incorrect. The sequence 1, 1, 4, 4, 5, 6, ..., 25 would be tested as valid.
 
Sheriff
Posts: 13391
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The logic you'll use for checking a row or column or 5x5 block is the same. So you might want to consider how to check any collection of 25 numbers for completeness first. With that logic written, you can then easily write something like this:

Each of those would be private methods that iterate over all rows, columns, and 5x5 blocks respectively, using the common logic for determining if a collection of 25 numbers is a correct solution.
 
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:There are several ways.


@OP
Or have a way to get all numbers either from row or from column or from box and put them in a Set. If the Set's size is what you expect, then...?
 
Piet Souris
Rancher
Posts: 3127
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Liutauras
why must you then also check whether the minimum is 1 and the max is 25?
 
Liutauras Vilda
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:@Liutauras
why must you then also check whether the minimum is 1 and the max is 25?


I don't know. You mustn't, if one can assume that such sudoku setup contains numbers between 1 and 25 (including).

instructions wrote:...we enter numbers from 1 to 25...

 
Predrag Djindjic
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys no offense, but almost everything you post is not helpful at all.I am asking you for help for an algorithm to solve subblocks in Sudoku, and i mention before that I already wrote rows and column check methods, and the thing you type is: you need a boolean method which returns boolean of all three checks.I literally typed that i did that prior to your reply.I dont want to be rude and i'm new on forum but if you want to comment and help me please read everything and don't just type the first thing that comes to your mind, try to reply something that can actually help me.
P.S. I did it, someone just had to link me some Sudoku solving algorithms on StackOverflow.
Have a great day
 
Predrag Djindjic
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code if anyone needs it:
 
Liutauras Vilda
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't go much to details, but it seems incorrect solution. Have you tested it?

Such input below it seems would return true. Meaning a correct solution. Except it isn't.
 
Liutauras Vilda
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Predrag Djindjic wrote:Guys no offense, but almost everything you post is not helpful at all.I am asking you for help for an algorithm to solve subblocks in Sudoku


What do you mean to solve? As far as I understood, you are given a solved (either correctly or incorrectly) sudoku, and your task is to verify whether the solution is correct.
 
Liutauras Vilda
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing, let's assume your solution is correct. Now you have essentially 3 same methods. Check line, column and block. If you think more carefully, you'll figure out, that these (line/column/block) contain 25 numbers each. So really find the way (presumably a method) to return either row/column/box (25 elements in total) and pass it on to the method to check whether it contains legit numbers (meaning no duplicates, you can check also ranges if you want to, as Piet suggested earlier).
 
Predrag Djindjic
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's correct and yeah it verifies if it's true, as i wrote in the original post. As i said just please read everything i wrote before you comment on my post because it leads me to some totally different things.
 
Liutauras Vilda
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Predrag Djindjic wrote:It's correct and yeah it verifies if it's true, as i wrote in the original post. As i said just please read everything i wrote before you comment on my post because it leads me to some totally different things.


I read once again what you wrote, don't assume people don't read. And once again I came to the same conclusion - your solution is incorrect. But I don't see the point in arguing. Good luck with assignment.
 
Predrag Djindjic
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not an argument.I just said read everything I wrote (and I wrote ant the end of an original post that my assignment is to check if it is valid), and then you said specify the problem you have.I really appreciate you commenting but again, if you just write "I think this is not correct" it really doesn't help.
Also in main method i give the argument(dadoteka) which contains the info im analyzing.
 
Liutauras Vilda
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Predrag Djindjic wrote:...if you just write "I think this is not correct" it really doesn't help.


Liutauras Vilda wrote:...it seems incorrect solution...

Such input below it seems would return true. Meaning a correct solution. Except it isn't.


I have also provided an input, on which your solution would fail, whether it would a line or column or block. All three methods contain same issue. What issue specifically is for you to identify as I personally think that should be enough as for a hint.

Predrag, a friendly advice. Try to avoid collecting solutions from pieces found on internet or similar. Idea is to: [1] sit down and understand an assignment, [2] presumably sketch something on a paper to verify your thinking, [3] think through an exercise once again, [4] attempt to solve it yourself.

Doing so every other assignment would become easier and easier to solve as you'd figure out how to tackle problems. The given example input on which your input would fail, and the response received from you suggests, that you didn't get what I was saying.

Ok, please explain, how do you check whether the line's solution is legit? Could you describe in english how you solving it?
 
Predrag Djindjic
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So basically i first had to make a method that creates int[][] with sudoku rows.Then i had to make 3 methonds that check if the given data for sudoku is valid.I made 1 method that checks rows(nested for loop that iterates through whole data and checks if there is equal numbers in every row), the 2nd method is the same just for the columns and the third checks if the number is the unique one in the 5x5 square.
 
Liutauras Vilda
Marshal
Posts: 6634
443
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, can you explain how you check whether singular row is solved correctly? Step by step please.
 
This tiny ad is wafer thin:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!