5
$\begingroup$

Can anyone solve this puzzle or give me some tips? Rules: you have to use all 1, 2, 3, 4, 5, 6, 7 numbers in every 8 straight and vertical lines(4 vertical line and 4 straight line) And the summation of the 8 rooms around each pink number must be the pink number! Please help me!! See the puzzle at: http://b2n.ir/24768enter image description here

$\endgroup$
  • 1
    $\begingroup$ Nice puzzle. Not serious comment: The answer is impossible to find. There are no squares in your image! :c) $\endgroup$ – BmyGuest Dec 15 '14 at 17:49
  • $\begingroup$ @faZ I updated my answer. There is exactly 2741 solutions. :) $\endgroup$ – Victor Stafusa Dec 16 '14 at 0:39
9
$\begingroup$

[EDITED: Now, with many solutions!]

The first solution is this:

$$\begin{array}{rrrrrrr} 1 & 7 & 4 & 2 & 5 & 3 & 6 \\ 2 & 37 & 5 & 30 & 2 & 27 & 3 \\ 5 & 6 & 7 & 2 & 3 & 1 & 4 \\ 4 & 37 & 6 & 32 & 1 & 22 & 1 \\ 6 & 1 & 2 & 7 & 4 & 3 & 5 \\ 3 & 24 & 1 & 35 & 7 & 38 & 7 \\ 7 & 1 & 3 & 5 & 6 & 4 & 2 \end{array} $$

How did I find that?

BRUTE FORCE!

Here is a Java program that I'd built to find the solutions. It tries that by trying every possible combination, and when it finds one that solves the puzzle, it shows it. It takes about 50 seconds to find the first solution in my PC, but it will find many:

import java.util.HashSet;
import java.util.Set;

/**
 * @author Victor
 */
public class GridFind {
    private final int[][] m;
    private int solutions;

    public GridFind() {
        m = new int[7][7];
        m[1][1] = 37;
        m[1][3] = 37;
        m[1][5] = 24;
        m[3][1] = 30;
        m[3][3] = 32;
        m[3][5] = 35;
        m[5][1] = 27;
        m[5][3] = 22;
        m[5][5] = 38;
    }

    public boolean brute(int x, int y) {
        for (int a = 1; a <= 7; a++) {
            m[x][y] = a;
            if (!check()) continue;
            if (x == 6 && y == 6) {
                show();
                return false;
            }
            int j = x, k = y;
            if (k == 6) {
                j++;
                k = 0;
            } else if (j % 2 == 1) {
                k += 2;
            } else {
                k++;
            }
            if (brute(j, k)) return true;
        }
        m[x][y] = 0;
        return false;
    }

    private boolean check() {
        for (int v = 0; v < 7; v += 2) {
            Set<Integer> a = new HashSet<>(7);
            for (int u = 0; u < 7; u++) {
                int z = m[v][u];
                if (z == 0) continue;
                if (a.contains(z)) return false;
                a.add(z);
            }
            Set<Integer> b = new HashSet<>(7);
            for (int u = 0; u < 7; u++) {
                int z = m[u][v];
                if (z == 0) continue;
                if (b.contains(z)) return false;
                b.add(z);
            }
        }
        for (int u = 0; u <= 4; u += 2) {
            for (int v = 0; v <= 4; v += 2) {
                int a1 = m[u][v];
                int a2 = m[u][v + 1];
                int a3 = m[u][v + 2];
                int a4 = m[u + 1][v];
                int a5 = m[u + 1][v + 2];
                int a6 = m[u + 2][v];
                int a7 = m[u + 2][v + 1];
                int a8 = m[u + 2][v + 2];
                int ac = m[u + 1][v + 1];
                if (a1 == 0 || a2 == 0 || a3 == 0 || a4 == 0 || a5 == 0 || a6 == 0 || a7 == 0 || a8 == 0) continue;
                if (a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 != ac) return false;
            }
        }
        return true;
    }

    public void show() {
        solutions++;
        System.out.println("Found the solution #" + solutions);
        for (int a = 0; a < 7; a++) {
            for (int b = 0; b < 7; b++) {
                int x = m[b][a];
                if (x < 10) System.out.print(" ");
                System.out.print(x + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        GridFind p = new GridFind();
        p.brute(0, 0);
    }
}

I am sorry that this site does not have syntax coloring.

I mentioned that the algorithm finds many solutions. The first one that it finds is up there in the beginning of this post. So what are the others?

Second solution:

$$\begin{array}{rrrrrrr} 1 & 7 & 4 & 2 & 5 & 3 & 6 \\ 2 & 37 & 5 & 30 & 3 & 27 & 3 \\ 5 & 6 & 7 & 3 & 1 & 2 & 4 \\ 4 & 37 & 6 & 32 & 2 & 22 & 1 \\ 6 & 1 & 2 & 7 & 4 & 3 & 5 \\ 3 & 24 & 1 & 35 & 7 & 38 & 7 \\ 7 & 1 & 3 & 5 & 6 & 4 & 2 \\ \end{array} $$

Third solution:

$$\begin{array}{rrrrrrr} 1 & 5 & 7 & 3 & 4 & 2 & 6 \\ 2 & 37 & 4 & 30 & 2 & 27 & 7 \\ 6 & 7 & 5 & 4 & 1 & 2 & 3 \\ 3 & 37 & 6 & 32 & 3 & 22 & 1 \\ 7 & 1 & 2 & 6 & 5 & 3 & 4 \\ 5 & 24 & 1 & 35 & 6 & 38 & 5 \\ 4 & 1 & 3 & 5 & 7 & 6 & 2 \\ \end{array} $$

40th solution:

$$\begin{array}{rrrrrrr} 1 & 7 & 5 & 2 & 6 & 3 & 4 \\ 4 & 37 & 3 & 30 & 3 & 27 & 5 \\ 7 & 6 & 4 & 5 & 2 & 1 & 3 \\ 6 & 37 & 7 & 32 & 1 & 22 & 1 \\ 2 & 4 & 1 & 7 & 5 & 3 & 6 \\ 3 & 24 & 2 & 35 & 4 & 38 & 7 \\ 5 & 1 & 6 & 3 & 7 & 4 & 2 \\ \end{array} $$

126th solution:

$$\begin{array}{rrrrrrr} 2 & 5 & 6 & 1 & 4 & 3 & 7 \\ 1 & 37 & 5 & 30 & 2 & 27 & 3 \\ 5 & 6 & 7 & 2 & 3 & 1 & 4 \\ 6 & 37 & 4 & 32 & 1 & 22 & 1 \\ 4 & 3 & 2 & 7 & 6 & 1 & 5 \\ 3 & 24 & 1 & 35 & 7 & 38 & 6 \\ 7 & 1 & 3 & 4 & 5 & 6 & 2 \\ \end{array} $$

415th solution:

$$\begin{array}{rrrrrrr} 3 & 6 & 7 & 2 & 1 & 4 & 5 \\ 1 & 37 & 2 & 30 & 7 & 27 & 4 \\ 7 & 6 & 5 & 4 & 2 & 1 & 3 \\ 6 & 37 & 3 & 32 & 4 & 22 & 1 \\ 5 & 1 & 4 & 7 & 3 & 2 & 6 \\ 2 & 24 & 1 & 35 & 6 & 38 & 7 \\ 4 & 1 & 6 & 3 & 5 & 7 & 2 \\ \end{array} $$

The 2741st (and last) solution:

$$\begin{array}{rrrrrrr} 7 & 1 & 3 & 4 & 6 & 2 & 5 \\ 6 & 37 & 2 & 30 & 4 & 27 & 3 \\ 5 & 7 & 6 & 3 & 2 & 1 & 4 \\ 4 & 37 & 7 & 32 & 1 & 22 & 1 \\ 3 & 4 & 1 & 7 & 5 & 2 & 6 \\ 2 & 24 & 4 & 35 & 3 & 38 & 7 \\ 1 & 4 & 5 & 3 & 7 & 6 & 2 \\ \end{array} $$

The program took, on my PC, 7 hours, 15 minutes and 16 seconds to find all the 2741 solutions.

$\endgroup$
  • 2
    $\begingroup$ Good old Brute Force! :) $\endgroup$ – A E Dec 15 '14 at 2:43
  • 3
    $\begingroup$ Truly the best force. $\endgroup$ – No. 7892142 Dec 15 '14 at 9:50
  • 1
    $\begingroup$ If you let it keep running, does it find any more solutions? :-) $\endgroup$ – Hellion Dec 15 '14 at 16:43
  • $\begingroup$ @Hellion I changed the program. Yes, see the edit. :) $\endgroup$ – Victor Stafusa Dec 15 '14 at 17:34
  • $\begingroup$ @Hellion My program just finished. There is 2741 solutions. $\endgroup$ – Victor Stafusa Dec 16 '14 at 0:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.