Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Rule for checking the correct solution for a sudoku problem are :-
Grid size is 9x9, divided into 9 regions of 3x3
Each row must contain all digits from 1-9
Each column must contain all digits from 1-9
Each 3x3 square must contain all digits from 1-9
Most of the solutions check for the below 3 constraints :-
Each row must contain all digits from 1-9
Each column must contain all digits from 1-9
Each 3x3 square must contain all digits from 1-9
My question is "If i check for the first two constraints i.e.
Each row must contain all digits from 1-9
Each column must contain all digits from 1-9
Does it not enough to meet the 3rd constraint also?"
In other words does anybody know any sudoku solution where digits 1-9 are uniquely placed in rows and columns but are not unique in 3X3 square?
1 2 3 | 4 5 6 | 7 8 9
9 1 2 | 3 4 5 | 6 7 8
8 9 1 | 2 3 4 | 5 6 7
- - - + - - - + - - -
7 8 9 | 1 2 3 | 4 5 6
6 7 8 | 9 1 2 | 3 4 5
5 6 7 | 8 9 1 | 2 3 4
- - - + - - - + - - -
4 5 6 | 7 8 9 | 1 2 3
3 4 5 | 6 7 8 | 9 1 2
2 3 4 | 5 6 7 | 8 9 1
Yes you must check the third constraint. See the following solution that passes the first two constraints, but not the third.
[[1,2,3,4,5,6,7,8,9],
[2,3,4,5,6,7,8,9,1],
[3,4,5,6,7,8,9,1,2],
[4,5,6,7,8,9,1,2,3],
[5,6,7,8,9,1,2,3,4],
[6,7,8,9,1,2,3,4,5],
[7,8,9,1,2,3,4,5,6],
[8,9,1,2,3,4,5,6,7],
[9,1,2,3,4,5,6,7,8]]
Related
Given a grid of dimensions A*B with values between 1-9, find a sequence of B numbers that maximizes the minimum number of values matched when compared with A rows.
Describe the certain steps you would take to maximize the minimum score.
Example:
Grid Dimension
A = 5 , B = 10
Grid Values
9 3 9 2 9 9 4 5 7 6
6 3 4 2 8 5 7 5 9 2
4 9 5 8 3 7 3 2 7 6
7 5 8 9 9 4 7 3 3 7
2 6 8 3 2 4 5 4 2 2
Possible Answer
6 3 8 2 9 4 7 5 7 4
Score Calculation
This answer scores
5 when compared with Row 1
5 when compared with Row 2
1 when compared with Row 3
4 when compared with Row 4
2 when compared with Row 5
And thus the minimal score for this answer is 1.
I would go for a local hill-climbing approach that you can complement with a randomization to avoid local minima. Something like:
1. Generate a random starting solution S
2. Compute its score score(S, row) for each row. We'll call min_score(S) the minimum score among all rows for S.
3. Attempt to improve the solution with:
For each digit i (1..B) in S:
If i belongs to a row such that score(S, row) > (min_score(S) + 1) then:
Change i to be the digit of a row with min_score(S). If there was only one row with min_score(S), then min_score(S) has improved by 1
Update the scores of all the rows.
If min_score(S) hasn't improved for more than N iterations of 3, go back to 1 and start with a new random solution.
I'm trying to generate a matrix, like for a cross-sum game, where in a matrix of random numbers, for a given a sum (or a product, depending on a chosen operation) for each row and column, there's exactly 1 way to "deactivate" (meaning, to exclude the number from the final sum or product) correct numbers so that each row and column end up summing the active numbers to the correct sum.
To illustrate this, let's say I have a 3x3 matrix, and chosen sums (numbers next to * represent the sum):
*12* *5* *3*
4* 1 2 3 *4
9* 4 5 6 *9
7* 7 8 9 *7
In order to solve this, I would need to deactivate numbers 2, 6, 9 and 8.
One way to generate a matrix with needed sums it to just generate the numbers, and then choose which ones to exclude at random. However, the drawback is that for bigger matrices, like 7x7, 8x8, there's a good possibility that there will be more than 1 solution.
Another solution I'm thinking of is to exclude the numbers that can add up to another for each row / column. For example if a required sum is 5, then 4 2 1 3 would be invalid because (4 + 1 and 3 + 2), but this seems rather complicated and inefficient.
If anyone has any pointers, I'd greatly appreciate it. This seems like it's a solved problem, but I have no idea what to look for.
Checking random grids with a solver
For a matrix of limited size, up to 10×10 or so, a simple solver can quickly find the solutions. If there is only one, even the quick'n'dirty solver I wrote in javaScript usually finds in it less than a second.
I used a simple recursive row-by-row solver which works like this:
For each column, iterate over every possible selection of numbers and check whether excluding them gives the column the correct sum. Then check whether any numbers are part of all or none of the valid selections; these will have to be included and avoided in all selections.
For each row, iterate over every possible selection of numbers and check whether excluding them gives the row the correct sum, and whether they contain all of the to-be-included and none of the to-be-avoided numbers identified in the previous step. Store all valid selections per row.
After these preparations, the recursive part of the algorithm is then called:
Receive a matrix with numbers, a list of sums per row, and a list of sums per column.
For the top row, check whether any of the numbers cannot be excluded (because the numbers below it add up to less that the sum for that column).
Iterate over all valid selections of numbers in the top row (as identified in the preparation phase). For each selection, check whether removing it gives the row its correct sum. If it does, recurse with a copy of the matrix with the top row removed, a list of sums per row with the first item removed, and a list of sums per column with the non-excluded numbers in the top row subtracted.
Starting from a pattern like this, where the X's indicate which cell will be excluded:
- - - X - - - X - -
- - - - X - X - - -
X - - - - X - - - -
- X - - - - - - - X
- - X - - - - - X -
- X - - - - - X - -
X - - - - - - - X -
- - - - X - - - - X
- - - X - X - - - -
- - X - - - X - - -
I let the matrix be filled with random numbers from 1 to 9, and then ran the solver on it, and about one in ten attempts results in a grid like this, which has exactly one solution:
4 1 3 8 1 3 4 1 1 8 25
9 9 7 8 1 1 3 2 1 7 44
9 8 8 1 5 5 9 2 2 6 41
4 6 8 1 9 2 1 7 1 5 33
9 4 2 4 4 5 8 6 3 8 48
8 5 6 9 6 6 6 4 1 8 50
4 3 2 4 8 7 6 7 9 1 38
6 7 8 1 9 9 9 4 6 7 50
7 7 1 7 9 6 2 7 1 2 36
3 3 8 8 9 2 4 9 6 8 48
50 42 43 36 51 35 45 44 19 48
When using only numbers from 1 to 9, grids with only one solution are easy to find for smaller grids (more than half of 8×8 grids have only one solution), but become hard to find for grid sizes over 10×10. Most larger grids have many solutions, like this one which has 16:
4 1 5 7 2 2 5 6 5 8 32
5 1 1 6 4 6 5 2 2 9 32
9 2 3 8 7 7 4 8 3 6 41
4 8 1 8 4 3 1 9 7 2 37
4 6 9 8 8 5 8 6 6 5 50
1 5 5 5 1 3 5 7 7 1 28
5 5 1 7 2 9 2 6 3 8 40
9 8 9 2 8 3 1 9 6 8 47
5 1 3 7 1 2 6 1 8 9 34
1 5 1 2 1 1 1 6 4 3 23
33 29 28 46 26 32 32 47 42 49
The number of solutions also depends on the number of excluded numbers per row and column. The results shown above are specifically for the pattern with two excluded numbers per row and column. The more excluded numbers, the greater the average number of solutions (I assume with a peak at 50% excluded numbers).
You can of course use a random pattern of cells to be excluded, or choose the numbers by hand, or have random numbers chosen with a certain distribution, or give the matrix any other property that you think will enhance its usefulness as a puzzle. Multiple solutions don't seem to be a big problem for smaller grids, but it is of course best to check for them; I first ran the solver on a grid I had made by hand, and it turned out to have three solutions.
Choosing the excluded values
Because the value of the excluded numbers can be chosen freely, this is the obvious way to improve the chance of a matrix having only one solution. If you choose numbers that don't occur anywhere else in the row and column, or only once, then the percentage of 10×10 grids that have only one solution rises from 10% to 50%.
(This simple method obviously gives a clue about which numbers should be excluded – it's not the numbers that occur several times in a row or column – so it's probably better to use how many times each number occurs in the whole grid, not just in its own row and column.)
You could of course choose excluded values that add up to a number that can't be made with any other combination of values in the row or column, and that would guarantee only one solution. The problem with this is of course that such a grid doesn't really work as a puzzle; there is only ever one way to exclude values and get the correct sum for every row and column. A variant would be to choose excluded values so that the sum of the row or column can be made in exactly two, or three, or ... ways. This would also give you a way to choose the difficulty level of the puzzle.
Sudoku – avoiding duplicate values
The fact that larger grids have a higher chance of having more than one solution is of course linked to using only values for 1 to 9. Grids of 10×10 and greater are guaranteed to have duplicate values in every row and column.
To check whether grids with no duplicate values per row or column are more likely to lead to only one solution, the obvious test data is the Sudoku.
When using random patterns of 1 to 3 cells per row and column to be excluded, around 90% of cross-sum matrix games based on Sudokus have only one solution, compared to around 60% when using random values.
(It could of course be interesting to create puzzles which work both as a Sudoku and as a cross-sum matrix puzzle. For every Sudoku it should be easy to find a visually pleasing pattern of excluded cells that has only one solution.)
Examples
For those who like a challenge (or want to test a solver), here's a cross-sum Sudoku and an 11×11, 12×12 and 13×13 cross-sum matrix puzzle that have just one solution:
. 3 . 4 . . . . . 36
. 6 . . 9 . . 4 5 35
4 . . . . . 9 . . 33
. . 3 . . 1 . . . 39
. . . . . 8 2 . 3 29
.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4 5 8 1 2 6 2 49
7 1 7 4 9 2 9 8 9 3 5 2 3 59
66 50 69 50 58 49 64 57 65 66 56 47 54
I need to partition a set S={1, 2, 3, … , n} consisting of consecutive numbers such that each subset has has at least 2 elements (rule 1) and it consists of consecutive numbers (rule 2).
The rules are:
Each subset has at least two elements.
All elements of all subsets are consecutive.
All elements of S are included in the partition.
Examples:
There is 1 subset for n = 2:
1 2
There is 1 subset for n = 3:
1 2 3
There are 2 subset combinations for n = 4:
1 2 3 4
1 2 - 3 4
There are 3 subset combinations for n = 5:
1 2 3 4 5
1 2 - 3 4 5
1 2 3 - 4 5
There are 5 subset combinations for n = 6:
1 2 3 4 5 6
1 2 - 3 4 5 6
1 2 3 - 4 5 6
1 2 3 4 - 5 6
1 2 - 3 4 - 5 6
There are 8 subset combinations for n = 7:
1 2 3 4 5 6 7
1 2 - 3 4 5 6 7
1 2 3 - 4 5 6 7
1 2 3 4 - 5 6 7
1 2 3 4 5 - 6 7
1 2 - 3 4 - 5 6 7
1 2 - 3 4 5 - 6 7
1 2 3 - 4 5 - 6 7
There are 13 subset combinations for n = 8:
1 2 3 4 5 6 7 8
1 2 - 3 4 5 6 7 8
1 2 3 - 4 5 6 7 8
1 2 3 4 - 5 6 7 8
1 2 3 4 5 - 6 7 8
1 2 3 4 5 6 - 7 8
1 2 - 3 4 - 5 6 7 8
1 2 - 3 4 5 - 6 7 8
1 2 - 3 4 5 6 - 7 8
1 2 3 - 4 5 - 6 7 8
1 2 3 - 4 5 6 - 7 8
1 2 3 4 - 5 6 - 7 8
1 2 - 3 4 - 5 6 - 7 8
There are 21 subset combinations for n = 9:
1 2 3 4 5 6 7 8 9
1 2 - 3 4 5 6 7 8 9
1 2 3 - 4 5 6 7 8 9
1 2 3 4 - 5 6 7 8 9
1 2 3 4 5 - 6 7 8 9
1 2 3 4 5 6 - 7 8 9
1 2 3 4 5 6 7 - 8 9
1 2 - 3 4 - 5 6 7 8 9
1 2 - 3 4 5 - 6 7 8 9
1 2 - 3 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 2 3 - 4 5 6 - 7 8 - 9 10
1 2 3 4 - 5 6 - 7 8 - 9 10
1 2 - 3 4 - 5 6 - 7 8 - 9 10
I didn't write them down here but there are 55 subset combinations for n = 11 and 89 subset combinations for n = 12.
I need to write a Visual Basic code listing all possible subset groups for n. I have been thinking on the solution for days but it seems that the solution of the problem is beyond my capacity. The number of required nested loops increases with n and I could not figure out how to program the nested loops with increasing number. Any help will be greatly appreciated.
After some research, I found out this is the problem of "compositions of n with all parts >1" and the total number of possible compositions are Fibonacci numbers (Fn-1 for n).
We already know the answer for these cases (as you wrote in your examples):
n=2
n=3
n=4
For n=5:
You can partition from 2: 1 2 - 3 4 5. This is like dividing the 5 member set into two sets, first one n=2, and second one n=3. We can now continue dividing each half, but we already know the solutions when n=2 and n=3!
You can partition from 3: 1 2 3 - 4 5. This is like dividing the 5 member set into two sets, first one n=3, and second one n=2. We can now continue dividing each half, but we already know the solution when n=2 and n=3!
For n=6:
You can partition into two sets from 2: 1 2 - 3 4 5 6. This is like dividing the 6 member set into two sets, first one n=2, and second one n=4. We can now continue dividing each half, but we already know the solution when n=2. Solve the second half by assuming n=4!
You can partition into two sets from 3: 1 2 3 - 4 5 6. This is like dividing the 6 member set into two sets, first one n=3, and second one n=3, We can now continue dividing each half, but we already know the solution when n=3 and n=3!
You can partition into two sets from 3: 1 2 3 4 - 5 6. This is like dividing the 6 member set into two sets, first one n=4, and second one n=2, We can now continue dividing each half. Solve the first half by setting n=4. For the second half, we already know the solution when n=2!
This is a simple recursion relationship. The general case:
Partition (S): (where |S|>4)
- For i from 2 to |S|-2, partition the given set into two halves: s1 and s2 from i (s1={1,...,i}, s2={i+1,...,n}), and print the two subsets as a solution.
- Recursively continue for each half by calling Partition(s1) and Partition(s2)
---
Another perhaps harder solution is to assume we are dividing the numbers 1 to n into n sections, where the length of each section can be either 0, 2, or a number greater than 2. In other words let xi be the length of each section:
x1 + x2 + ... xn = n, where the range of xi is: {0} + [2,n]
This is a system of linear non-equalities can can be solved by methods described here.
My answer to you is to try and come up with a recurrence relation of the given pattern. Think recursively. How can I break this problem down into smaller subproblems until reaching the smallest problem. Solve that smallest problem. After solving that smallest problem, think induction. Hypothesize on the what the nth step will be and how you will reach the (n+1)th step. Try to solve that (n+1)th step. Once you have come up with a recurrence relation on the pattern being given, it should not be too difficult to think about how to solve this pattern recursively. Instead of trying to use nested loops, this approach may be more intuitive.
I have records like these:
1 4 6 4 2 4 8
2 3 5 4 6 7 1
5 4 6 4 3 8 4
1 4 6 4 5 7 1
5 7 3 3 3 6 3
6 7 3 3 4 8 4
I want to sort them on columns 2,3,4, and 6 and keep just one of those identical in column 2,3,4 and having the biggest number in column 6 such as:
1 4 6 4 5 7 1
2 3 5 4 6 7 1
5 4 6 4 3 8 4
5 7 3 3 3 6 3
6 7 3 3 4 8 4
I have tried all kinds of combinations between sort and uniq but everything fails because uniq cannot be applied onto a specific column. The only thing I came up with is to change the order of the columns as to first sort as above then move records 2,3,and 4 to the end and then run uniq with -w as to focus only on the last 3 records. This seems quite inefficient to me.
Thanks for help!
You can achieve this with two passes of sort(assuming in the first place I understand your requirement correctly, seeing that the desired data snippet posted above does not match your description of it) . The first pass sorts by field 2 through 4 ascending and field 6 descending, the second pass sorts on fields 2 through 4 only but passing in the "stable sort" and unique flags in addition to pick out those rows for each combination of fields 2-4 that have the highest value from field 6
sort -k2,4n -k6,6nr file.txt | sort -k2,4n -s -u
2 3 5 4 6 7 1
5 4 6 4 3 8 4
6 7 3 3 4 8 4
I was asked to find a 11x11-grid containing the digits such that one can read the squares of 1,...,100. Here read means that you fix the starting position and direction (8 possibilities) and if you can find for example the digits 1,0,0,0,0,4 consecutively, you have found the squares of 1, 2, 10, 100 and 20. I made a program (the algorithm is not my own. I modified slightly a program which uses best-first search to find a solution but it is too slow. Does anyone know a better algorithm to solve the problem?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <vector>
#include <algorithm>
using namespace std;
int val[21][21];//number which is present on position
int vnum[21][21];//number of times the position is used - useful if you want to backtrack
//5 unit borders
int mx[4]={-1,0,1,0};//movement arrays
int my[4]={0,-1,0,1};
int check(int x,int y,int v,int m)//check if you can place number - if you can, return number of overlaps
{
int c=1;
while(v)//extract digits one by one
{
if(vnum[x][y] && (v%10)!=val[x][y])
return 0;
if(vnum[x][y])
c++;
v/=10;
x+=mx[m];
y+=my[m];
}
return c;
}
void apply(int x,int y,int v,int m)//place number - no sanity checks
{
while(v)//extract digits one by one
{
val[x][y]=v%10;
vnum[x][y]++;
v/=10;
x+=mx[m];
y+=my[m];
}
}
void deapply(int x,int y,int v,int m)//remove number - no sanity checks
{
while(v)
{
vnum[x][y]--;
v/=10;
x+=mx[m];
y+=my[m];
}
}
int best=100;
void recur(int num)//go down a semi-random path
{
if(num<best)
{
best=num;
if(best)
printf("FAILED AT %d\n",best);
else
printf("SUCCESS\n");
for(int x=5;x<16;x++) // 16 and 16
{
for(int y=5;y<16;y++)
{
if(vnum[x][y]==0)
putchar('.');
else
putchar(val[x][y]+'0');
}
putchar('\n');
}
fflush(stdout);
}
if(num==0)
return;
int s=num*num,t;
vector<int> poss;
for(int x=5;x<16;x++)
for(int y=5;y<16;y++)
for(int m=0;m<4;m++)
if(t=check(x,y,s,m))
poss.push_back((x)|(y<<8)|(m<<16)|(t<<24));//compress four numbers into an int
if(poss.size()==0)
return;
sort(poss.begin(),poss.end());//essentially sorting by t
t=poss.size()-1;
while(t>=0 && (poss[t]>>24)==(poss.back()>>24))
t--;
t++;
//t is now equal to the smallest index which has the maximal overlap
t=poss[rand()%(poss.size()-t)+t];//select random index>=t
apply(t%256,(t>>8)%256,s,(t>>16)%256);//extract random number
recur(num-1);//continue down path
}
int main()
{
srand((unsigned)time(0));//seed
while(true)
{
for(int i=0;i<21;i++)//reset board
{
memset(val[i],-1,21*sizeof(int));
memset(vnum[i],-1,21*sizeof(int));
}
for(int i=5;i<16;i++)
{
memset(val[i]+5,0,11*sizeof(int));
memset(vnum[i]+5,0,11*sizeof(int));
}
recur(100);
}
}
Using a random search so far I only got to 92 squares with one unused spot (8 missing numbers: 5041 9025 289 10000 4356 8464 3364 3249)
1 5 2 1 2 9 7 5 6 9 5
6 1 0 8 9 3 8 4 4 1 2
9 7 2 2 5 0 0 4 8 8 2
1 6 5 9 6 0 4 4 7 7 4
4 4 2 7 6 1 2 9 0 2 2
2 9 6 1 7 8 4 4 0 9 3
6 5 5 3 2 6 0 1 4 0 6
4 7 6 1 8 1 1 8 2 8 1
8 0 1 3 4 8 1 5 3 2 9
0 5 9 6 9 8 8 6 7 4 5
6 6 2 9 1 7 3 9 6 9
The algorithm basically uses as solution encoding a permutation on the input (search space is 100!) and then places each number in the "topmost" legal position. The solution value is measured as the sum of the squares of the lengths of the numbers placed (to give more importance to long numbers) and the number of "holes" remaining (IMO increasing the number of holes should raise the likehood that another number will fit in).
The code has not been optimized at all and is only able to decode a few hundred solutions per second. Current solution has been found after 196k attempts.
UPDATE
Current best solution with this approach is 93 without free holes (7 missing numbers: 676 7225 3481 10000 3364 7744 5776):
9 6 0 4 8 1 0 0 9 3 6
6 4 0 0 2 2 5 6 8 8 9
1 7 2 9 4 1 5 4 7 6 3
5 8 2 3 8 6 4 9 6 5 7
2 4 4 4 1 8 2 8 2 7 2
1 0 8 9 9 1 3 4 4 9 1
2 1 2 9 6 1 0 6 2 4 1
2 3 5 5 3 9 9 4 0 9 6
5 0 0 6 1 0 3 5 2 0 3
2 7 0 4 2 2 5 2 8 0 9
9 8 2 2 6 5 3 4 7 6 1
This is a solution (all 100 numbers placed) however using a 12x12 grid (MUCH easier)
9 4 6 8 7 7 4 4 5 5 1 7
8 3 0 5 5 9 2 9 6 7 6 4
4 4 8 3 6 2 6 0 1 7 8 4
4 8 4 2 9 1 4 0 5 6 1 4
9 1 6 9 4 8 1 5 4 2 0 1
9 4 4 7 2 2 5 2 2 5 0 0
4 6 2 2 5 8 4 2 7 4 0 2
0 3 3 3 6 4 0 0 6 3 0 9
9 8 0 1 2 1 7 9 5 5 9 1
6 8 4 2 3 5 2 6 3 2 0 6
9 9 8 2 5 2 9 9 4 2 2 7
1 1 5 6 6 1 9 3 6 1 5 4
It has been found using a truly "brute force" approach, starting from a random matrix and keeping randomly changing digits when that improved the coverage.
This solution has been found by an highly unrolled C++ program automatically generated by a Python script.
Update 2
Using an incremental approach (i.e. keeping a more complex data structure so that when changing a matrix element the number of targets covered can be updated instead than recomputed) I got a much faster search (about 15k matrices/second investigated with a Python implementation running with PyPy).
In a few minutes this version was able to find a 99 quasi-solution (a number is still missing):
7 0 5 6 5 1 1 5 7 1 6
4 6 3 3 9 8 8 6 7 6 1
3 9 0 8 2 6 1 1 4 7 8
1 1 0 8 9 9 0 0 4 4 6
3 4 9 0 4 9 0 4 6 7 1
6 4 4 6 8 6 3 2 5 2 9
9 7 8 4 1 1 4 0 5 4 2
6 2 4 1 5 2 2 1 2 9 7
9 8 2 5 2 2 7 3 6 5 0
3 1 2 5 0 0 6 3 0 5 4
7 5 6 9 2 1 6 5 3 4 6
UPDATE 3
Ok. After a some time (no idea how much) the same Python program actually found a complete solution (several ones indeed)... here is one
6 4 6 9 4 1 2 9 7 3 6
9 2 7 7 4 4 8 1 2 1 7
1 0 6 2 7 0 4 4 8 3 4
2 1 2 2 5 5 9 2 9 6 5
9 2 5 5 2 0 2 6 3 9 1
1 6 3 6 0 0 9 3 7 0 6
6 0 0 4 9 0 1 6 0 0 4
9 8 4 4 8 0 1 4 5 2 3
2 4 8 2 8 1 6 8 6 7 5
1 7 6 9 2 4 5 4 2 7 6
6 6 3 8 8 5 6 1 5 2 1
The searching program can be found here...
You've got 100 numbers and 121 cells to work with, so you'll need to be very efficient. We should try to build up the grid, so that each time we fill a cell, we attain a new number in our list.
For now, let's only worry about 68 4-digit numbers. I think a good chunk of the shorter numbers will be in our grid without any effort.
Start with a 3x3 or 4x4 set of numbers in the top-left of your grid. It can be arbitrary, or fine-tune for slightly better results. Now let's fill in the rest of the grid one square at a time.
Repeat these steps:
Fill an empty cell with a digit
Check which numbers that knocked off the list
If it didn't knock off any 4-digit numbers, try a different digit or cell
Eventually you may need to fill 2 cells or even 3 cells to achieve a new 4-digit number, but this should be uncommon, except at the end (at which point, hopefully there's a lot of empty space). Continue the process for the (few?) remaining 3-digit numbers.
There's a lot room for optimizations and tweaks, but I think this technique is fast and promising and a good starting point. If you get an answer, share it with us! :)
Update
I tried my approach and only got 87 out of the 100:
10894688943
60213136008
56252211674
61444925224
59409675697
02180334817
73260193640
.5476685202
0052034645.
...4.948156
......4671.
My guess is that both algorithms are too slow. Some optimization algorithm might work like best-first search or simulated annealing but my I don't have much experience on programming those.
Have you tried any primary research on Two-Dimensional Bin Packing (2DBP) algorithms? Google Scholars is a good start. I did this a while ago when building an application to generate mosaics.
All rectangular bin packing algorithms can be divided into 4 groups based on their support for the following constraints:
Must the resulting bin be guillotine cuttable? I.e. do you have to later slice the bin(s) in half until all the pieces are unpacked?
Can the pieces be rotated to fit into the bin? Not an issue with square pieces, so this makes more algorithms available to you.
Out of all the algorithms I looked into, the most efficient solution is an Alternate Directions (AD) algorithm with a Tabu Search optimization layer. There are dissertations which prove this. I may be able to dig-up some links if this helps.
Some ideas off the top of my head, without investing much time into thinking about details.
I would start by counting the number of occurrences of each digit in all squares 1..100. The total number of digits will be obviously larger than 121, but by analyzing individual frequencies you can deduce which digits must be grouped on a single line to form as many different squares as possible. For example, if 0 has the highest frequency, you have to try to put as many squares containing a 0 on the same line.
You could maintain a count of digits for each line, and each time you place a digit, you update the count. This lets you easily compute which square numbers have been covered by that particular line.
So, the program will still be brute-force, but it will exploit the problem structure much better.
PS: Counting digit frequencies is the easiest way to decide whether a certain permutation of digits constitutes a square.