Puzzle Programming - Unable to optimize?

I write programs for solving various puzzles, but I constantly develop unreasonably complex search algorithms that I cannot optimize.

For example, in one puzzle you are given a 3x3 grid with numbers from 1 to 9:

123
456
789

You are allowed to cycle numbers in any row or column in any direction. Below is an example of switching the top line of numbers to the right . The numbers will loop if they are on the edge of the grid.

123 -> 312
456    456
789    789

You have to move the numbers this way until you create a magic square , in which the sum of the numbers in each column, row and diagonal is 15.

I wrote a DFS brute force algorithm to check all possible sequences of moves, although the number of available moves on each revolution increases exponentially (approximately 12 ^ [current revolution]), which makes it useless.

It seems that BFS will be optimal for finding the right moves, but it will require me to store hundreds, not thousands of copies of the grid, to retreat!


I run into such problems all the time. Both BFS and DFS algorithms use too much memory and time, respectively. I need help optimizing such algorithms so that they work faster and more efficiently. Maybe recognize patterns and relationships of numbers, or let the logic of the algorithm work toward the goal? (I do not know what this will entail).

EDIT:

. , , . !

+5
5

memoization ( , ). memoization, ( , ). fibonacci, fibonacci + memoization .

DFS BFS , Branch and Bound. . , , ( : , ).

, .

DFS. , . . DFS ( , ). ? (EDIT: . , , )

, , , . . ...

. , . , 15, , 9, 8 7 . 9 6. 6 8 1 7 2. 6 / 5 4, 15 - ( / 9, 8 7). , , , , , . .

, , , . ( , ), ( , ).

:

  • .
  • .

, , . . , , Levenshtein.

+9

ccoakley stubbscroll .

stubbscroll , 9!= 362880 :
( ) .

0 1 2 3  -->  0
0 1 3 2  -->  1
0 2 1 3  -->  2
...
1 0 2 3  -->  6
1 0 3 2  -->  7
...
3 1 2 0  --> 21
3 2 0 1  --> 22
3 2 1 0  --> 23

,

n = a_1 * 1! + a_2 * 2! + a_3 * 3! + a_4 * 4! + ...

0 <= a_k <= k. s, 0 s! -1, s-1 n, (a_1,a_2,...,a_(s-1)). n :

 for i = 1 to s-1
    the i-th symbol becomes the (a_(s-i)+1)-th smallest unused symbol
 the last symbol is the left over one

, . , 4231 {1,2,3,4,5,6,7,8}. 4231

4231 = 1 + 2*2115 :  a_1 = 1
2115 = 0 + 3* 705 :  a_2 = 0
 705 = 1 + 4* 176 :  a_3 = 1
 176 = 1 + 5*  35 :  a_4 = 1
  35 = 5 + 6*   5 :  a_5 = 5
   5 = 5 + 7*   0 :  a_6 = 5

( a_7) 0. a_i (a_7, a_6,... a_1),

 coefficients      symbols       choice
0,5,5,1,1,0,1  1,2,3,4,5,6,7,8     1
 5,5,1,1,0,1    2,3,4,5,6,7,8      7
  5,1,1,0,1      2,3,4,5,6,8       8
   1,1,0,1        2,3,4,5,6        3
    1,0,1          2,4,5,6         4
     0,1            2,5,6          2
      1              5,6           6
      -               5            5

: 17834265.

246351:

symbols     count     perm    index(head)
1,2,3,4,5,6   6   2,4,6,3,5,1    1         a_5 = 1
 1,3,4,5,6    5    4,6,3,5,1     2         a_4 = 2
  1,3,5,6     4     6,3,5,1      3         a_3 = 3
   1,3,5      3      3,5,1       1         a_2 = 1
    1,5       2       5,1        1         a_1 = 1

index - `1 * 5! + 2 * 4! + 3 * 3! + 1 * 2! + 1 * 1!= 187.

, . (O (s ^ 2)), ( ?). , , .

, .

  • , , .

, , , . ( ) 9!/2 = 181440 . . , , a_k .

, . , . , , - . .

, 5 . 8 , 8!/2 = 20160 . ( .)

, , , , . , , , .

, , .

+4

, 3x3, , , 3x3 ( ). , , , .

, 4 .

, DFS BFS , , . , , .

+3

A *. , . , .

+1

. , , , , , .

, , .

" NP", , .

+1

All Articles