Sphere Online Judge

SPOJ Problem Set (classical)

96. Shopping

Problem code: SHOP

Crowd in the supermarketThe old tube screen to your computer turned out to be the cause of your chronic headaches. You therefore decide to buy one of these new flat TFT monitors. At the entrance of the computer shop you see that it is quite full with customers.

In fact, the shop is rather packed with customers and moving inside involves a certain amount of elbowing. Since you want to return home quickly to complete your half finished SPOJ tasks, you want to sidestep the crowd as much as possible. You examine the situation somewhat closer and realise that the crowding is less in some parts of the shop. Thus, there is reason for hope that you can reach your goal in due time, provided that you take the shortest way. But which way is the shortest way?

You sketch the situation on a piece of paper but even so, it is still a tricky affair. You take out your notebook from your pocket and start to write a program which will find the shortest way for you.


The first line of the input specifies the width w and height h of the shop. Neither dimension exceeds 25.

The following h lines contain w characters each. A letter X symbolises a shelf, the letter S marks your starting position, and the letter D marks the destination (i.e. the square in front of the monitors). All free squares are marked with a digit from 1 to 9, meaning the number of seconds needed to pass this square.

There are many test cases separated by an empty line. Input terminates with width and height equal 0 0.


Your program is to output the minimum number of seconds needed to reach to destination square. Each test case in a separate line. Movements can only be vertical and horizontal. Of course, all movements must take place inside the grid. There will always be a way to reach the destination.


Sample input:
4 3

5 5

0 0

Sample output:

Added by:Micha≥ Czuczman
Time limit:3s
Source limit:50000B
Memory limit:256MB
Cluster: Pyramid (Intel Pentium III 733 MHz)
Resource:Swiss Olympiad in Informatics 2004

hide comments
2014-09-03 06:55:49 Sourabh Bansal
Dijkstra 0.00 sec
First Attempt :)
Nice Question
2014-08-07 18:17:23 -.-
2014-08-07 15:42:03 mohsin mohammad
Finally Dijkstra worked for me!
2014-07-14 07:48:50 Ganesh Salunke
bfs + priority queue = Dijkstra's
2014-07-13 10:37:19 Abhishek Gupta
Finally got AC... nice question

Last edit: 2014-07-13 10:38:10
2014-07-13 05:22:38 pandu ranga rao
finallyyyyyyyy AC...
2014-07-10 11:00:16 tapopadma
Dijkstra:0.00 sec , Bellman_Ford : 0.39 sec :D
2014-07-10 06:00:59 tapopadma
simple Bellmanford O((25^4)*4) works fine :D

Last edit: 2014-07-10 06:09:40
2014-06-26 09:08:02 Archit Jain
easy ac in first attempt
2014-05-24 18:59:03 Aditya Deepak
Nice Question loved it :) AC inn first try :D
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.