sudoku algorithm explanation formula - php

I'm implementing a sudoku solver using human way algorithm. Which have 3 constraint, different number ini row, cell and box.
I googled and I got http://www.emanueleferonato.com/2008/12/09/sudoku-creatorsolver-with-php/. But I cannot understand how this guy get floor($cell / 9) for return_row function or floor(return_row($cell) / 3) * 3 + floor(return_col($cell) / 3) for return_block.
I try to figure it out by write down the data in excel and I know there is some pattern like this :
[cell] [column]
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 0
But how did he figure it out that the formula was $cell % 9 ?
I want to know, if I don't know the answer for the formula, how can I calculate that ? How can I determine that formula ? What method should I use?
Thanks

This comes from the way the cells are counted, which we could call row-major.
You can see block and cell numbers with their respective row and column numbers on this image :
rows and columns
The first row (0) contains cells 0 to 8, the second row cells 9 to 17, and so on until row 8, which contains cells 72 to 80.
If you number rows 0 to 8 and columns 0 to 8 as well, we can see that the formula for a cell that corresponds to this numbering is then cell = 9 * row + col, which should explain the formulas for get_row and get_col.
When moving right from any cell by one column, you add 1 to the cell count, which means the formula for the cell number looks like something + col.
When moving down one row, you add to the cell number the amount of cells per row, which here is 9, so the formula also looks like 9 * row + something.
Putting those together, you get a formula that is 9 * row + col + offset : the "+ something"s dependencies is row and col are determined, but maybe they still contain a constant value.
In our case, the formula gives the numbering we want with offset=0, but if you started numbering from 1 you formula would be 9 * row + col + 1.
However you do not have to do this reasoning every time. Just now that when you have a rectangle where you count items row by row, the formula for an item's number is always row * row_size + col + number at (0,0). This is also how contiguous memory is allocated for double arrays in C, for example, a very common pattern. If you count column by column, then you have col * col_size + row + number at (0,0)
Blocks
Now blocks are numbered the same way, but there are only 3 rows and columns. You can replace one by one the elements in the get_block formula to understand it : floor(row / 3) * 3 + floor(col / 3)
Since there are 3 rows of blocks but 9 of cells, the (cell-)rows 0, 1 and 2 correspond to the first row of blocks, 3 to 5 to the second row of blocks and 6 to 8 to the final and third row of blocks. What we get out of this is that a row of blocks rb contains the rows of cells 3 * rb, 3 * rb +1 and 3 * rb + 2. The opposite operation is dividing by 3 and flooring, which gets you rb for any of the expressions above.
This works exactly the same for the columns.
Thus when replacing in the expression, we now have : block_row * 3 + block_col. This I'd exactly the same formula (with 3 instead of 9) than we had for the numbering of cells, and thus gets you the number of the block from its row and column.

Related

Multidimensional-splfixedarray for integers in PHP

My question is organizing a one-dimensional list of numbers into a two-dimensional table with a variable number of columns to the right.
The lengths of the column should be balanced so that all rows except the last are filled. The empty space in the last row should be at the right end of the row.
EXAMPLE:
List: 1,5,1,3,6,2,8,7,4,9,5,6,25,4,8,5,63
COLUMNS: 5
TABLE: 1 6 4 6 8
5 2 9 25 5
1 8 5 4 63
3 7
I have to write a program (using PHP) to read a collection of records (<100), each with a positive integer in columns 1-5. A zero value terminates the list. A second set of records should be read next, with various positive integers values for C, the number of columns, in columns 1-5. After each value of C is read, the sum of the elements in each row of the table should be computed and printed. Sums will be less than 9 digits. The two-dimensional table itself should not be printed.
Using the sample data shown above for various values of C, output similar to the following should be produced:
For example:
C ROW SUM
5 25 41 81 10
***************************
1 153
***************************
Either vertical or horizontal printing of row sums is acceptable, but a line of asterisks should separate output generated for different values of C. A zero value for C should cause the program to terminate.

How to start from a certain row and end in a certain row in mysql PHP?

How to start from a certain row and end in a certain row in mysql ?
Example :
row 1 - Mike
row 2 - ayman
row 3 - kayla ---> i want to start from here
row 4 - polat
row 5 - ninety
row 6 - Okamou
row 7 - Sakura
row 8 - kall ---> i want to end here
row 9 - kopa
row 10 - Traas
what i'm using is:
Select name, number, id from usernames_table LIMIT 3,8
This is just an example, In my real application i want to start from row number 2000 and end in row 6000 which is not the end of the table.
I used LIMIT starthere, endhere but it gives me some extra rows i don't know why?
Please help ?
You are using the limit incorrectly. This:
Select name, number, id from usernames_table LIMIT 3,8
says to start at row 4 and end after 8 rows have been selected.
Try:
Select name, number, id from usernames_table LIMIT 2,6
This says start after row 2, and return 6 rows (so 3-8).
Further information: http://dev.mysql.com/doc/refman/5.7/en/select.html
... or for your real application:
Select name, number, id from usernames_table LIMIT 1999,4000
which says start at 2000 and return 4000 rows (so end at 6000, assuming rows weren't deleted..).

Lowest free value in mysql column

I already searched but I always find LEAST and GREATEST as hints. I want to have the next ascending number in a row that's not used. Like the following:
entries
1
2
3
5
6
7
If every of the numbers is for one row in my table I want the number 4 as a result and in the following example:
1
2
3
4
5
6
I want the number 7 as a result. Is there any possiblity to accomplish this in an SQL statement?
Best,
Robin
This query assumes that the number 1 is in your table
select min(number) + 1 from entries e1
where not exists (
select 1 from entries e2
where e2.number = e1.number + 1
)
If you want all missing numbers (where gaps are no larger than 1) instead of the smallest one, then remove min()
It think the solution is to do a self-join with the next value, and extract the first lowest result. Example:
Table: values, with column value
SELECT v1.value
FROM values v1
LEFT JOIN values v2 ON v1.value = (v2.value + 1)
WHERE v2.value IS NULL
ORDER BY v1.value ASC
LIMIT 1

Detecting columns in a document (table) via php - Algorithm

Given a table like the one below, what would be the best way to detect the two columns separately?
So what I would need the total colspans for the first column.
What is important to remember is that the nr of columns can change.
In the case of this example, the second column starts at "10 euro" (second row). The first section is equal to 2 colspans. The other section is 5 colspans.
Any (abstract) ideas on how to do this?
You must consider the gaps in between the table cells and mark their positions, like this::
0 1 2 3 4 7
0 2 3 4 5 6 7
0 1 2 4 5 7
...
0 2 7
Once you have built an array with above information, you iterate over them and mark the common gap locations:
0 2 7
Since 0 and 7 are both at the edges of your table, you can strip those off. Then you're left with position 2 as the common gap between your rows.
Done :)

PHP: What's the mathematically efficient way to find a 'subgrid' of a larger grid?

i'm trying to find an efficient way to find a subportion of a large grid. Currently I'm looping through rows to define a FROM-TO selection of ids in that row, but this doesn't feel right...
Let's say I have a grid of 200x200 fields (x between 1 and 200, y between 1 and 200). Every field also has it's unique ID, starting at X1,Y1 (fieldid = 1) to X200,Y200 (fieldid = 40000).
I have a subportion of the grid that I need to select from the database (based on XY-ranges or unique id's, where uniqueids are much faster so preferred). The subportion is defined by the upperleft field (again based on XY value or a unique id) and then 16 fields wide and 9 fields high.
So, how can I efficiently select a subgrid of 144 fields (16x9) of a large grid of 40000 fields (200x200) based on the unique-id or XY-value of the upperleft field in the 16x9 subgrid?
Not too sure if I understand the problem you are trying to overcome, some context would probably help... anyway, here goes...
x1 -> x2 = 16 spaces
y1 -> y2 = 9 spaces
x1*y1 = start ID
x2*y2 = end ID
you will have nine ranges (where 'a' is the valid range):
x1*y1 < a < (x1*y1)+16
x1*(y1+1) < a < (x1*(y1+1))+16
x1*(y1+2) < a < (x1*(y1+2))+16
....
x1*(y1+8) < a < (x1*(y1+8))+16
I have found that the geometry extension in MySQL is pretty well optimized. So the most efficient way would be to add a column of type POINT to your table holding the position in the matrix.
ALTER TABLE YourTable ADD COLUMN pos POINT NOT NULL;
Then you can select using the extension's functions (Example for selecting 16x9 area starting at coordinates 10/10):
SET #polygon = GeomFromText('Polygon((10 10, 26 10, 26 19, 10 19, 10 10))');
SELECT * FROM YourTable WHERE MBRContains(#polygon, pos);

Categories