Jump to content

Recommended Posts

import java.util.*;

public class Example {
    public static void main(String[] args) {
        int[][] solution = {
                {9, 6, 3, 1, 7, 4, 2, 5, 8},
                {1, 7, 8, 3, 2, 5, 6, 4, 9},
                {2, 5, 4, 6, 8, 9, 7, 3, 1},
                {8, 2, 1, 4, 3, 7, 5, 9, 6},
                {4, 9, 6, 8, 5, 2, 3, 1, 7},
                {7, 3, 5, 9, 6, 1, 8, 2, 4},
                {5, 8, 9, 7, 1, 3, 4, 6, 2},
                {3, 1, 7, 2, 4, 6, 9, 8, 5},
                {6, 4, 2, 5, 9, 8, 1, 7, 3}
        };
        boolean ok = true;
        int[] count = new int[9];
        for (int i = 0; i < solution.length; i++) {
            // -1 added for array index starting from 0, means 9 in above means count[8]
            for (int j = 0; j < solution[0].length; j++) {
                count[solution[i][j] - 1]++;
            }
            // ok or not checker
            for (int x = 0; x < count.length; x++) {
                System.out.println(count[x]);
                if (count[x] > 1) {
                    ok = false;
                }
            }
            // reset the array count for next loop
            for (int k = 0; k < count.length; k++) {
                count[k] = 0;
            }
        }


    }
}

 

import java.util.*;

public class Example {
    public static void main(String[] args) {
        int[][] solution = {
                {9, 6, 3, 1, 7, 4, 2, 5, 8},
                {1, 7, 8, 3, 2, 5, 6, 4, 9},
                {2, 5, 4, 6, 8, 9, 7, 3, 1},
                {8, 2, 1, 4, 3, 7, 5, 9, 6},
                {4, 9, 6, 8, 5, 2, 3, 1, 7},
                {7, 3, 5, 9, 6, 1, 8, 2, 4},
                {5, 8, 9, 7, 1, 3, 4, 6, 2},
                {3, 1, 7, 2, 4, 6, 9, 8, 5},
                {6, 4, 2, 5, 9, 8, 1, 7, 3}
        };
        boolean ok = true;
        int[] count = new int[9];
        for (int i = 0; i < solution.length; i++) {
            // -1 added for array index starting from 0, means 9 in above means count[8]
            for (int j = 0; j < solution[0].length; j++) {
                count[solution[i][j] - 1]++;
            }
            ok = checkIfOk(count);
            reset(count);

        }
        System.out.println(ok);

    }

    public static void reset(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            arr[i] = 0;
        }
    }

    public static boolean checkIfOk(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > 1) {
                return false;
            }
        }
        return true;
    }
}

Better modularized code

So you have it checking all the rows, in that the outer loop counts rows (first index) and the inner loop looks at the cells in the row.
You can use the exact same technique for checking the columns. (Hint: reverse your usage of the rows and columns.)

That won't work for checking the grids, though. For the grids, you need to look at both rows and columns at once, but only a portion of them.
So the "first" grid would be rows 0,1,2 and columns 0,1,2. "Second" would be rows 0,1,2 but columns 3,4,5. Then 0,1,2 and 6.7.8.
Then repeat but with rows 3,4,5, and then again with 6,7,8.

Give that a try and we'll see what you have.

# For grids

Now, I attempted to find a pattern.

top left subgrid

i=1-3

j=1-3

top middle subgrid

i=1-3

j=4-6

top right subgrid

i=1-3

j=7-9

middle left subgrid

i=4-6

j=1-3

middle middle subgrid

i=4-6

j=4-6

middle right subgrid

i=4-6

j=7-9

bottom left subgrid

i=7-9

j=1-3

bottom middle subgrid

i=7-9

j=4-6

bottom right subgrid

i=7-9

j=7-9

This is the pattern that I've caught. However, I am unable to exactly translate these stuffs to code.

 

Checking row/col wise was easy, just interchanged j and i.

 

import java.util.*;

public class Example {
    public static void main(String[] args) {
        int[][] solution = {
                {9, 6, 3, 1, 7, 4, 2, 5, 8},
                {1, 7, 8, 3, 2, 5, 6, 4, 9},
                {2, 5, 4, 6, 8, 9, 7, 3, 1},
                {8, 2, 1, 4, 3, 7, 5, 9, 6},
                {4, 9, 6, 8, 5, 2, 3, 1, 7},
                {7, 3, 5, 9, 6, 1, 8, 2, 4},
                {5, 8, 9, 7, 1, 3, 4, 6, 2},
                {3, 1, 7, 2, 4, 6, 9, 8, 5},
                {6, 4, 2, 5, 9, 8, 1, 7, 3}
        };
        boolean ok = true;
        int[] count = new int[9];
        // row-wise loop
        for (int i = 0; i < solution.length; i++) {
            // -1 added for array index starting from 0, means 9 in above means count[8]
            for (int j = 0; j < solution[0].length; j++) {
                count[solution[i][j] - 1]++;
            }
            ok = checkIfOk(count);
            reset(count);

        }
        // column-wise loop
        for (int i = 0; i < solution.length; i++) {
            // -1 added for array index starting from 0, means 9 in above means count[8]
            for (int j = 0; j < solution[0].length; j++) {
                count[solution[j][i] - 1]++;
            }
            ok = checkIfOk(count);
            reset(count);

        }
        System.out.println(ok);

    }

    public static void reset(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            arr[i] = 0;
        }
    }

    public static boolean checkIfOk(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > 1) {
                return false;
            }
        }
        return true;
    }
}

 

If the individual cells are numbered 0 - 80 in 9 rows of 9

image.png.424e6234fc14667d31b9a6ab5b89bfa6.png

Then the index (0-8) of the large 3x3 square is given by

    function gridSquare($n)
    {
        return floor(floor($n/9) / 3) * 3 + floor($n % 9 / 3);
    }

where $n i the nunber of the individual cell (0-80)

@Barandsir. What do I do with that? I'm getting really puzzled by this puzzle.

import java.util.*;

public class Example {
    public static void main(String[] args) {
        int[][] solution = {
                {9, 6, 3, 1, 7, 4, 2, 5, 8},
                {1, 7, 8, 3, 2, 5, 6, 4, 9},
                {2, 5, 4, 6, 8, 9, 7, 3, 1},
                {8, 2, 1, 4, 3, 7, 5, 9, 6},
                {4, 9, 6, 8, 5, 2, 3, 1, 7},
                {7, 3, 5, 9, 6, 1, 8, 2, 4},
                {5, 8, 9, 7, 1, 3, 4, 6, 2},
                {3, 1, 7, 2, 4, 6, 9, 8, 5},
                {6, 4, 2, 5, 9, 8, 1, 7, 3}
        };
        boolean ok = true;
        int[] count = new int[9];
        // row-wise loop
        for (int i = 0; i < solution.length; i++) {
            // -1 added for array index starting from 0, means 9 in above means count[8]
            for (int j = 0; j < solution[0].length; j++) {
                count[solution[i][j] - 1]++;
            }
            ok = checkIfOk(count);
            System.out.println(ok);
            reset(count);

        }
        // column-wise loop
        for (int i = 0; i < solution.length; i++) {
            // -1 added for array index starting from 0, means 9 in above means count[8]
            for (int j = 0; j < solution[0].length; j++) {
                count[solution[j][i] - 1]++;
            }
            ok = checkIfOk(count);
            System.out.println(ok);
            reset(count);

        }

        // grid check 3x3
        // 1st grid,2nd grid, 3rd grid
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                count[solution[i][j] - 1]++;
            }
            ok = checkIfOk(count);
            System.out.println(ok);
            reset(count);
            for (int j = 3; j < 6; j++) {
                count[solution[i][j] - 1]++;
            }
            ok = checkIfOk(count);
            System.out.println(ok);
            reset(count);
            for (int j = 6; j < 9; j++) {
                count[solution[i][j] - 1]++;
            }
            ok = checkIfOk(count);
            System.out.println(ok);
            reset(count);
        }
        // 4th,5th,6th grid
        // so on

        //7th,8th,9th grid
        // so on
        System.out.println(ok);

    }

    public static void reset(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            arr[i] = 0;
        }
    }

    public static boolean checkIfOk(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > 1) {
                return false;
            }
        }
        return true;
    }
}

I've came so far and there is no going back. I need to solve this.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.