## a problem from TIMUS

Moderator: Board moderators

nttjuvwamsncc
New poster
Posts: 3
Joined: Fri Feb 02, 2007 6:37 am
Location: HNVN
Contact:

### a problem from TIMUS

IFormula 1
Time Limit: 1.0 second
Memory Limit: 16 MB
Background

Regardless of the fact, that Vologda could not get rights to hold the Winter Olympic games of 20**, it is well-known, that the city will conduct one of the Formula 1 events. Surely, for such an important thing a new race circuit should be built as well as hotels, restaurants, international airport - everything for Formula 1 fans, who will flood the city soon. But when all the hotels and a half of the restaurants were built, it appeared, that at the site for the future circuit a lot of gophers lived in their holes. Since we like animals very much, ecologists will never allow to build the race circuit over the holes. So now the mayor is sitting sadly in his office and looking at the map of the circuit with all the holes plotted on it.
Problem

Who will be smart enough to draw a plan of the circuit and keep the city from inevitable disgrace? Of course, only true professionals - battle-hardened programmers from the first team of local technical university!.. But our heroes were not looking for easy life and set much more difficult problem: "Certainly, our mayor will be glad, if we find how many ways of building the circuit are there!" - they said.

It should be said, that the circuit in Vologda is going to be rather simple. It will be a rectangle N*M cells in size with a single circuit segment built through each cell. Each segment should be parallel to one of rectangle's sides, so only right-angled bends may be on the circuit. At the picture below two samples are given for N = M = 4 (gray squares mean gopher holes, and the bold black line means the race circuit). There are no other ways to build the circuit here.

Input

The first line contains the integer numbers N and M (2 ≤ N, M ≤ 12). Each of the next N lines contains M characters, which are the corresponding cells of the rectangle. Character "." (full stop) means a cell, where a segment of the race circuit should be built, and character "*" (asterisk) - a cell, where a gopher hole is located.
Output

You should output the desired number of ways. It is guaranteed, that it does not exceed 2^63-1.
Samplesinput output
4 4
**..
....
....
.... 2
4 4
....
....
....
.... 6
thank you very much
-- WWSBWOOW --

misof
A great helper
Posts: 430
Joined: Wed Jun 09, 2004 1:31 pm
http://acm.timus.ru/problem.aspx?space=50&num=9

Instead of copying it you could've linked it. I still had to google to find it, without the images it was hard to understand.

As for the solution, the brute force search can be pruned as follows: Suppose that you already filled the first K rows. Now the only thing that matters is how the K-th row looks like (including the information on which pairs of edges leading from the K-th row down are connected). Thus you can memoize the results based on this information.

Note that the above solution, while probably fast enough, is really nasty to implement. Anyone with a better idea?

nttjuvwamsncc
New poster
Posts: 3
Joined: Fri Feb 02, 2007 6:37 am
Location: HNVN
Contact:
misof wrote:For other readers of this thread, this is the problem statement:
http://acm.timus.ru/problem.aspx?space=50&num=9

Instead of copying it you could've linked it. I still had to google to find it, without the images it was hard to understand.

As for the solution, the brute force search can be pruned as follows: Suppose that you already filled the first K rows. Now the only thing that matters is how the K-th row looks like (including the information on which pairs of edges leading from the K-th row down are connected). Thus you can memoize the results based on this information.

Note that the above solution, while probably fast enough, is really nasty to implement. Anyone with a better idea?
I don't understand your solution , can you tell it more clearly
-- WWSBWOOW --

Per
A great helper
Posts: 429
Joined: Fri Nov 29, 2002 11:27 pm
Location: Sweden
misof wrote:Note that the above solution, while probably fast enough, is really nasty to implement. Anyone with a better idea?
No. The problem is very similar to (but perhaps even messier than) http://acm.uva.es/p/v105/10572.html which I solved the way you described.