3

Problem: we have to fill a 2D grid of size m*n with characters from the set S such that number of distinct sub-matrices in the resulting grid are close to a given number k.
This question is derived from http://www.codechef.com/JULY14/problems/GERALD09

Limits:
1<=n,m<16
1<=k <=m*n*m*n
|S|=4
time limit=0.1 sec

Assumption: Two sub-matrices are distinct if they are not having same dimensions or at least a pair of characters at their corresponding locations doesn't match.

My approach: We can start with a random grid and loop while acceptable solution is found and in each iteration, we can increase/decrease randomness depending on our current state(but we can stuck in local optimum states).

But the problem is that I don't know efficient way to calculate number of different sub-matrices in a sub-grid.I tried hashing for counting which is pretty fast ( O(n2m2)*cost of generating/searching a hash value for a sub-grid). But this approach doesn't give exact answers due to collisions of hash values and even after correcting it using the comment of @Vaughn Cato I can carry 15-25 iterations for optimum state finding and that is not enough .

Recently, I learned that Simulated annealing can be used to solve these kinds of problems.
http://www.theprojectspot.com/tutorial-post/simulated-annealing-algorithm-for-beginners/6
I am searching for any efficient approach for solving this optimization problem.

Thanks in advance.

v78
  • 2,803
  • 21
  • 44
  • 1
    Wouldn't you just use the hash table to limit the number of possibilities? That is, you keep a table listing all the sub-matrices that have a particular hash value, then you just check each new entry for duplicates within the same list. – Vaughn Cato Jul 15 '14 at 13:37
  • thanks , but even after that I wouldn't be able find the close to optimal within the time limit .Actually, I was looking for other more efficient approaches. – v78 Jul 15 '14 at 16:01
  • how can k be larger than m*n? isn't it like that having smallest matrices with size of single character will result in maximum of m*n distinct matrices? – rostok Jul 16 '14 at 13:17
  • @rostok , If we consider all distinct matrices of any dimension ,then we have m*n different options for the top left corner and m*n for diagonally opposite corner of any sub-matrix , thus giving at most m*n*m*n potentially distinct matrices in the worst case. – v78 Jul 16 '14 at 15:05

1 Answers1

1

I think they will post an editorial at some point, but here is a possible idea for this particular problem:

I generated locally all possible numbers of sub matrices possible for particular n and m. For n=m=3 I got only 11 out of 81 possibilities. For n=3,m=4 I got only 19 out of possible 144 values. What's more, when I generated the values, I obtained all 19 possible options at the very beginning - after 263000 matrices out of possible 16M I already had them. (I generated in the lexicographical order)

So, I assume, one possible solution might be to precompute as many as possible different values of K that can be achieved for given n and m, save either the seed for random generator or in some other way such that you need O(1) characters per n-m-k triplet, and for a particular test case just check two neighboring values - first k larger and smaller than given.

What's more, since number of possible K values is not large, it may be possible to generate them in other way: given all possible values of K for nxm table, along with the appropriate tables, we can only backtrack through the values in the next row, and try to obtain all possible matrices with all different values of K for nx(m+1).

maksay
  • 277
  • 3
  • 10