CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • February CookOff
    • February Long Contest
    • January CookOff
  • DISCUSS
    • Wiki
    • Forums
    • Blog
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
    • Event Calendar
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Ranks
    • Tutorials
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » March 2010 Contest » Treasure Hunting

Treasure Hunting

Problem code: N1

  • All Submissions

All submissions for this problem are available.

Treasure Hunting is a great computer game that has attracted generations of Bytelandian children.

In the game, there is a maze divided into NxN squares. Dave starts in the top-left corner, which is square (1,1), and needs to go to the bottom-right corner, which is square (n,n).

Some of the squares are blocked and some of the squares contain treasures.

Dave needs to capture all the treasures in the maze before going to square (n,n).

In each second, Dave can go to one of its four adjacent squares (if the destination is not blocked).

Find the earliest time that Dave can reach the destination(n,n) after collecting all the treasures in the maze.

Input

The first line contains t, the number of test cases (about 15). Then t test cases follow. Each test case has the following form.

The first line contains N (1 <= N <= 13), the size of the maze

The N following lines describe the maze. The meaning of the symbols is as follows:

'.' : an empty square

'*' : a treasure

'#' : a blocked square

The number of treasures in the maze does not exceed 13. Squares (1,1) and (n,n) are always empty.

Each test case's input is separated by a blank line.

Output

For each test case, print in a single line the earliest time that Dave can reach the destination after collecting all the treasures. If Dave cannot reach the destination, print -1.

Example

Input:
4

3
...
.##
*#.

3
..*
...
...

3
..*
*..
...

4
....
.#.*
.#*.
**#.

Output:
-1
4
6
16


Date: 2010-02-05
Time limit: 2s
Source limit: 50000
Languages: C C99 strict C++ 4.0.0-8 C++ 4.3.2 PAS gpc PAS fpc JAVA NICE JAR C# NEM ST ASM D FORT ADA BASH PERL PYTH RUBY LUA ICON PIKE PHP SCM guile SCM qobi LISP sbcl LISP clisp SCALA HASK ERL CAML CLPS PRLG WSPC BF ICK TEXT JS


  • Submit

Comments

  • Login or Register to post a comment.

@triplem how do u code too

vish @ 1 Mar 2010 03:24 PM

@triplem

how do u code too fast......

I see the time limit is 2s

ToX BoT @ 1 Mar 2010 04:36 PM

I see the time limit is 2s but one successful submission has a time limit of 3.8s

Is that normal or an error?

FAQ

triplem @ 1 Mar 2010 04:38 PM

FAQ

@Toxologic The time limit

Brian Drake @ 1 Mar 2010 05:15 PM

@Toxologic The time limit stated in each problem applies to each file for that problem; the time given for each submission is the sum of the times for all the files processed by that submission.

Brian Drake, I've read the

ToX BoT @ 2 Mar 2010 10:10 AM

Brian Drake, I've read the FAQ. Its clearly written over there that all the test cases are to be considered in one single time limit. I'm assuming that Java is expemted for a few seconds in time limit as stated in FAQ.

Anyways, Stephen and Brian, thanks for the reply!

It also says that if there

triplem @ 2 Mar 2010 10:22 AM

It also says that if there are multiple input files, then the reported time is the sum for each file, which can easily exceed two seconds; what Brian said was completely correct :)

The question says, under the

pragrame @ 2 Mar 2010 11:42 AM

The question says, under the Output: "If Dave cannot reach the destination, print -1." What if he cannot reach a particular treasure, but he can reach the destination?

Do we output the -1 or the shortest distance from 1,1 to n,n taking as many treasures as possible?

Everything that I believe,

rm.ggenius @ 2 Mar 2010 07:52 PM

Everything that I believe, crawls from underneath the streets. Everything I truly love, comes from somewhere high above. Everything that I believe, is wrong with you is wrong with me. Everything I truly love, I love in you I love me. Take my love, take it down Climbed a mountain and I turned around And if you see my reflection in a snow covered hill Well the landslide bring it down And if you see my reflection in a snow covered hill Well the landslide bring it down Well the landslide bring it down

 

Take my love, take it down

Climbed a mountain and I turned around

And if you see my reflection in a snow covered hill

Well the landslide bring it down

And if you see my reflection in a snow covered hill

Well the landslide bring it down

Well the landslide bring it down

hi, i get a status saying

kumaran @ 3 Mar 2010 01:52 PM

hi,

i get a status saying that an internal error occured in the system..

what does that mean??

They're looking into it. You

triplem @ 3 Mar 2010 03:29 PM

They're looking into it. You can see the results of your submissions by clicking on the 'My submissions' link on this page.

@Pradeep Print -1.

admin @ 3 Mar 2010 03:34 PM

@Pradeep Print -1.

Suppose we have reached (n,n)

mandark @ 3 Mar 2010 11:53 PM

Suppose we have reached (n,n) without collecting all treasures, and then, can we move out from (n,n), collect the remaining treasures and come back to (n,n)? For example in :

...

##.

#*.

Is the answer  6 or -1?

You are allowed to pass

triplem @ 4 Mar 2010 03:37 AM

You are allowed to pass through (n,n) part way through your path.

to h umesh the answer will be

snehasree @ 4 Mar 2010 07:26 AM

to h umesh

the answer will be 6.observe the example 3

@chellusnehasree None of the

Brian Drake @ 4 Mar 2010 05:20 PM

@chellusnehasree None of the examples involve Dave passing through (n, n) in the middle of his path.

@admin: my code is runnig

piyushcsiitd @ 5 Mar 2010 11:33 PM

@admin: my code is runnig well within 1 sec for 15 inputs for a test case of 13*13

Then why am i getting timeout ??

Can you give me and example case, where it timeouts?

Thanks.

@admin: I mean running within

piyushcsiitd @ 5 Mar 2010 11:34 PM

@admin: I mean running within 1 sec on my laptop.

@piyush. admin cant help you

mcsharma1990 @ 5 Mar 2010 11:42 PM

@piyush. admin cant help you till contest is over...

What ti the solution for 1 .

yashumayank @ 6 Mar 2010 11:25 AM

What ti the solution for

1

.

@admin what is memory limit

piyushcsiitd @ 6 Mar 2010 01:14 PM

@admin what is memory limit permitted ?

@Admin  I'm getting TLE with

Awake @ 7 Mar 2010 12:18 AM

@Admin  I'm getting TLE with 0k memory used.how is that possible? some memory usage must be recorded by the judge?

please clarify (submission ID 200840)

@Admin Please confirm

cjoa @ 7 Mar 2010 09:55 PM

@Admin

Please confirm Stephen's answer: "You are allowed to pass through (n,n) part way through your path."

I'm asking because the problem states "Dave needs to capture all the treasures in the maze before going to square (n,n)"

@ADMIN C#2 Solutions not

boxer @ 8 Mar 2010 05:30 AM

@ADMIN

C#2 Solutions not being accepted. Please look into the issue ASAP!

@Abhijit This is fixed. Try

admin @ 8 Mar 2010 01:18 PM

@Abhijit This is fixed. Try submitting now.

Can somebody explain this

sppraveen @ 13 Mar 2010 07:42 PM

Can somebody explain this problem. I solved using DFS , BFS , all pairs shortest path and TSP. All three solutions got TLE. I could see there is a much simpler way to do this. Pls explain

DP. There are only 15

triplem @ 14 Mar 2010 02:10 AM

DP. There are only 15 important squares (treasures + start + end). Given that you have found a certain subset (2^13) of treasures, and you are at a certain square (15), choose the optimal next treasure to get.

I remember Stephen had solved

akantev @ 14 Mar 2010 02:34 AM

I remember Stephen had solved this problem in the 1st 10-15 mins of the contest.....

@Stephen I believe doing so was a cakewalk 4 u, still....Hats off to you...

http://www.spoj.pl/problems/C

abhijith @ 14 Mar 2010 07:26 AM

http://www.spoj.pl/problems/CLEANRBT/

:P

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold
CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of computer programming. At CodeChef we work hard to revive the geek in you by hosting programming contests on a monthly basis. We also aim to have training sessions and events related to online programming for programmers around the world. Apart from providing a platform for programming competitions, CodeChef also has various tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our judge accepts solutions in over 35+ programming languages. Online programming was never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming competitions that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly programming contests and the shorter format Cook-off programming contests. Put yourself up for recognition and win great prizes. Prizes worth up to Rs.20,000 and $700 are up for grabs every month along with lots more CodeChef goodies.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be part of CodeChefs Forums and interact with all our programmers love helping out other programmers and share their ideas.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. Be a part of the CodeChef community through CodeChef meetups and techtalks. You can also host a programming contest for your institute on CodeChef and be a guest author on our blog.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com